Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions packages/eslint-plugin-next/src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ const fsReadDirSyncCache = {}
* Recursively parse directory for page URLs.
*/
function parseUrlForPages(urlprefix: string, directory: string) {
const extensions = ['js', 'jsx', 'ts', 'tsx']
Copy link
Contributor

Choose a reason for hiding this comment

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

The no-html-link-for-pages ESLint rule doesn't respect custom pageExtensions from Next.js config, causing files with compound extensions like test.page.tsx to have incorrect URLs (e.g., /test.page/ instead of /test/)

Fix on Vercel

const pageExtRegex = new RegExp(`\\.(${extensions.join('|')})$`)
fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {
withFileTypes: true,
})
const res = []
fsReadDirSyncCache[directory].forEach((dirent) => {
// TODO: this should account for all page extensions
// not just js(x) and ts(x)
if (/(\.(j|t)sx?)$/.test(dirent.name)) {
if (pageExtRegex.test(dirent.name)) {
if (/^index(\.(j|t)sx?)$/.test(dirent.name)) {
res.push(
`${urlprefix}${dirent.name.replace(/^index(\.(j|t)sx?)$/, '')}`
)
res.push(`${urlprefix}${dirent.name.replace(pageExtRegex, '')}`)
}
res.push(`${urlprefix}${dirent.name.replace(/(\.(j|t)sx?)$/, '')}`)
res.push(`${urlprefix}${dirent.name.replace(pageExtRegex, '')}`)
} else {
const dirPath = path.join(directory, dirent.name)
if (dirent.isDirectory() && !dirent.isSymbolicLink()) {
Expand All @@ -37,18 +37,21 @@ function parseUrlForPages(urlprefix: string, directory: string) {
* Recursively parse app directory for URLs.
*/
function parseUrlForAppDir(urlprefix: string, directory: string) {
const extensions = ['js', 'jsx', 'ts', 'tsx']
const pageExtRegex = new RegExp(`\\.(${extensions.join('|')})$`)

fsReadDirSyncCache[directory] ??= fs.readdirSync(directory, {
withFileTypes: true,
})
const res = []
fsReadDirSyncCache[directory].forEach((dirent) => {
// TODO: this should account for all page extensions
// not just js(x) and ts(x)
if (/(\.(j|t)sx?)$/.test(dirent.name)) {
if (pageExtRegex.test(dirent.name)) {
if (/^page(\.(j|t)sx?)$/.test(dirent.name)) {
res.push(`${urlprefix}${dirent.name.replace(/^page(\.(j|t)sx?)$/, '')}`)
res.push(`${urlprefix}${dirent.name.replace(pageExtRegex, '')}`)
} else if (!/^layout(\.(j|t)sx?)$/.test(dirent.name)) {
res.push(`${urlprefix}${dirent.name.replace(/(\.(j|t)sx?)$/, '')}`)
res.push(`${urlprefix}${dirent.name.replace(pageExtRegex, '')}`)
}
} else {
const dirPath = path.join(directory, dirent.name)
Expand Down
32 changes: 32 additions & 0 deletions test/unit/eslint-plugin-next/no-html-link-for-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,38 @@ describe('no-html-link-for-pages', function () {
'Do not use an `<a>` element to navigate to `/`. Use `<Link />` from `next/link` instead. See: https://nextjs.org/docs/messages/no-html-link-for-pages'
)
})
it('invalid static route with pageExtensions (.page.tsx)', function () {
const code = `
import Link from 'next/link';

export default function Test() {
return (
<div>
<a href='/test'>Test</a>
</div>
);
}
`

const [report] = linters.withCustomPages.verify(
code,
linterConfigWithCustomDirectory,
{
filename: path.join(
withCustomPagesDir,
'custom-pages',
'test.page.tsx'
),
}
)

assert.notEqual(report, undefined, 'No lint errors found.')
assert.equal(
report.message,
'Do not use an `<a>` element to navigate to `/test/`. Use `<Link />` from `next/link` instead. See: https://nextjs.org/docs/messages/no-html-link-for-pages'
)
})

it('invalid dynamic route', function () {
const [report] = linters.withCustomPages.verify(
invalidDynamicCode,
Expand Down
Loading