Skip to content

Conversation

Srajal1311
Copy link

…r-pages rule

@ijjk ijjk added the type: next label Oct 8, 2025
@ijjk
Copy link
Member

ijjk commented Oct 8, 2025

Allow CI Workflow Run

  • approve CI run for commit: bfff7d1

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

1 similar comment
@ijjk
Copy link
Member

ijjk commented Oct 8, 2025

Allow CI Workflow Run

  • approve CI run for commit: bfff7d1

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

}

// --- NEW CODE: skip internal links with allowed extensions ---
if (allowedExtRegex.test(hrefPath)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new file extension check will never match because it tests the normalized URL (which has a trailing slash) against a regex that expects the string to end with a file extension.

View Details
📝 Patch Details
diff --git a/packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts b/packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts
index f38216066d..4b849d660c 100644
--- a/packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts
+++ b/packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts
@@ -162,14 +162,15 @@ export default defineRule({
           return
         }
 
-        const hrefPath = normalizeURL(href.value.value)
-        // Outgoing links are ignored
-        if (/^(https?:\/\/|\/\/)/.test(hrefPath)) {
+        // --- NEW CODE: skip internal links with allowed extensions ---
+        // Check before normalization to avoid the trailing slash added by normalizeURL
+        if (allowedExtRegex.test(href.value.value)) {
           return
         }
 
-        // --- NEW CODE: skip internal links with allowed extensions ---
-        if (allowedExtRegex.test(hrefPath)) {
+        const hrefPath = normalizeURL(href.value.value)
+        // Outgoing links are ignored
+        if (/^(https?:\/\/|\/\/)/.test(hrefPath)) {
           return
         }
 

Analysis

File extension check never matches due to trailing slash from URL normalization

What fails: In packages/eslint-plugin-next/src/rules/no-html-link-for-pages.ts, the allowedExtRegex check at line 172 never matches links with file extensions (.js, .jsx, .ts, .tsx) because it tests against the normalized URL which has a trailing slash appended.

How to reproduce:

// In the ESLint rule, with href="/public/script.js"
const hrefPath = normalizeURL("/public/script.js");  // Returns "/public/script.js/"
const allowedExtRegex = /\.(js|jsx|ts|tsx)$/;
allowedExtRegex.test(hrefPath);  // Returns false (expects to end with extension, but ends with "/")

Result: Links to static files like <a href="/public/script.js"> are incorrectly flagged by the ESLint rule, even though they should be skipped.

Expected: The regex should match and skip these links, as they point to static resources rather than Next.js pages. Testing against the original href.value.value before normalization correctly identifies file extensions.

Fix: Check for file extensions before URL normalization, since normalizeURL() appends a trailing slash that breaks the end-of-string anchor ($) in the regex pattern.

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.

2 participants