next.js icon indicating copy to clipboard operation
next.js copied to clipboard

feat(eslint): Add pageExtensions support to no-html-link-for-pages rule

Open harikapadia999 opened this issue 2 weeks ago • 1 comments

What?

This PR adds support for custom pageExtensions configuration to the @next/next/no-html-link-for-pages ESLint rule.

Why?

Currently, the ESLint rule only recognizes default Next.js page extensions (.js, .jsx, .ts, .tsx) and ignores custom extensions configured via pageExtensions in next.config.js. This causes the linting rule to fail when projects use custom file extensions.

Fixes #53473

How?

Changes Made:

  1. packages/eslint-plugin-next/src/utils/url.ts:

    • Added buildPageExtensionRegex() helper to generate dynamic regex from extensions array
    • Updated parseUrlForPages() to accept pageExtensions parameter with default fallback
    • Updated parseUrlForAppDir() to accept pageExtensions parameter with default fallback
    • Modified getUrlFromPagesDirectories() and getUrlFromAppDirectory() signatures to support custom extensions
    • Removed hardcoded regex patterns and TODO comments
  2. packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts:

    • Added getPageExtensions() function to read pageExtensions from next.config.js
    • Integrated pageExtensions reading with support for .js, .mjs, and .ts config files
    • Passed custom extensions to URL parsing functions
    • Maintained backward compatibility with default extensions

Features:

  • ✅ Reads pageExtensions from next.config.js, next.config.mjs, or next.config.ts
  • ✅ Falls back to default extensions if config not found or parsing fails
  • ✅ Works with both Pages Router and App Router
  • ✅ Maintains backward compatibility
  • ✅ Uses caching to avoid performance impact

Testing

The implementation:

  • Maintains existing functionality for default extensions
  • Adds support for custom extensions like .page.tsx, .mdx, etc.
  • Gracefully handles missing or malformed config files
  • Uses memoization to prevent performance degradation

Note: This is my first contribution to Next.js. I'd appreciate any feedback on the implementation approach!

harikapadia999 avatar Dec 07 '25 16:12 harikapadia999

Allow CI Workflow Run

  • [ ] approve CI run for commit: 705060439641925fe46272aae3d656e4899416ac

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

nextjs-bot avatar Dec 07 '25 16:12 nextjs-bot