Skip to content

Conversation

@harikapadia999
Copy link

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!

- Add support for custom pageExtensions configuration
- Create buildPageExtensionRegex helper to generate dynamic regex
- Update parseUrlForPages and parseUrlForAppDir to accept pageExtensions
- Modify getUrlFromPagesDirectories and getUrlFromAppDirectory signatures
- Resolves TODO comments about hardcoded extensions
- Fixes issue vercel#53473
- Read pageExtensions from next.config.js
- Pass pageExtensions to URL parsing functions
- Support custom file extensions in linting
- Maintain backward compatibility with default extensions
@nextjs-bot
Copy link
Collaborator

Allow CI Workflow Run

  • approve CI run for commit: 9f1c1b6

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@next/next/no-html-link-for-pages rule does not work with pageExtensions

2 participants