Unverified Commit e1e6a073 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure dynamic routes dont match _next/static unexpectedly (#62559)

This ensures our dynamic routes that have the same specificity as
`_next/static/:path*` don't get matched unexpectedly when the
`_next/static` asset doesn't exist. We were holding off on making this
change explicit due to compatibility concerns but these are no longer a
concern and the unexpected matching is more of a concern.

Closes: CSM-11
Fixes: https://github.com/vercel/next.js/issues/19270

Closes NEXT-2613
parent f052fa8d
...@@ -179,6 +179,7 @@ import { TurbopackManifestLoader } from '../server/dev/turbopack/manifest-loader ...@@ -179,6 +179,7 @@ import { TurbopackManifestLoader } from '../server/dev/turbopack/manifest-loader
import type { Entrypoints } from '../server/dev/turbopack/types' import type { Entrypoints } from '../server/dev/turbopack/types'
import { buildCustomRoute } from '../lib/build-custom-route' import { buildCustomRoute } from '../lib/build-custom-route'
import { createProgress } from './progress' import { createProgress } from './progress'
import { modifyRouteRegex } from '../lib/redirect-status'
interface ExperimentalBypassForInfo { interface ExperimentalBypassForInfo {
experimentalBypassFor?: RouteHas[] experimentalBypassFor?: RouteHas[]
...@@ -284,13 +285,17 @@ export type RoutesManifest = { ...@@ -284,13 +285,17 @@ export type RoutesManifest = {
caseSensitive?: boolean caseSensitive?: boolean
} }
function pageToRoute(page: string) { function pageToRoute(page: string, restrictedPaths?: string[]) {
const routeRegex = getNamedRouteRegex(page, true) const routeRegex = getNamedRouteRegex(page, true)
return { return {
page, page,
regex: normalizeRouteRegex(routeRegex.re.source), regex: normalizeRouteRegex(
restrictedPaths?.length
? modifyRouteRegex(routeRegex.re.source, restrictedPaths)
: routeRegex.re.source
),
routeKeys: routeRegex.routeKeys, routeKeys: routeRegex.routeKeys,
namedRegex: routeRegex.namedRegex, namedRegex: modifyRouteRegex(routeRegex.namedRegex, restrictedPaths),
} }
} }
...@@ -1087,6 +1092,9 @@ export default async function build( ...@@ -1087,6 +1092,9 @@ export default async function build(
const restrictedRedirectPaths = ['/_next'].map((p) => const restrictedRedirectPaths = ['/_next'].map((p) =>
config.basePath ? `${config.basePath}${p}` : p config.basePath ? `${config.basePath}${p}` : p
) )
const restrictedDynamicPaths = ['/_next/static'].map((p) =>
config.basePath ? `${config.basePath}${p}` : p
)
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST) const routesManifestPath = path.join(distDir, ROUTES_MANIFEST)
const routesManifest: RoutesManifest = nextBuildSpan const routesManifest: RoutesManifest = nextBuildSpan
...@@ -1101,7 +1109,7 @@ export default async function build( ...@@ -1101,7 +1109,7 @@ export default async function build(
for (const route of sortedRoutes) { for (const route of sortedRoutes) {
if (isDynamicRoute(route)) { if (isDynamicRoute(route)) {
dynamicRoutes.push(pageToRoute(route)) dynamicRoutes.push(pageToRoute(route, restrictedDynamicPaths))
} else if (!isReservedPage(route)) { } else if (!isReservedPage(route)) {
staticRoutes.push(pageToRoute(route)) staticRoutes.push(pageToRoute(route))
} }
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
type RouteMatchFn, type RouteMatchFn,
} from '../../../shared/lib/router/utils/route-matcher' } from '../../../shared/lib/router/utils/route-matcher'
import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex' import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import { modifyRouteRegex } from '../../../lib/redirect-status'
type RouteMatchResult = { type RouteMatchResult = {
params?: Record<string, string | string[]> params?: Record<string, string | string[]>
...@@ -24,7 +25,13 @@ export class RouteMatcher<D extends RouteDefinition = RouteDefinition> { ...@@ -24,7 +25,13 @@ export class RouteMatcher<D extends RouteDefinition = RouteDefinition> {
constructor(public readonly definition: D) { constructor(public readonly definition: D) {
if (isDynamicRoute(definition.pathname)) { if (isDynamicRoute(definition.pathname)) {
this.dynamic = getRouteMatcher(getRouteRegex(definition.pathname)) const routeRegex = getRouteRegex(definition.pathname)
const origRegex = routeRegex.re.toString()
const modifiedRegex = modifyRouteRegex(origRegex, ['/_next/static'])
routeRegex.re = new RegExp(
modifiedRegex.substring(1, modifiedRegex.length - 1)
)
this.dynamic = getRouteMatcher(routeRegex)
} }
} }
......
...@@ -3,7 +3,12 @@ import cheerio from 'cheerio' ...@@ -3,7 +3,12 @@ import cheerio from 'cheerio'
import { promisify } from 'util' import { promisify } from 'util'
import { join } from 'path' import { join } from 'path'
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { check, fetchViaHTTP, normalizeRegEx, waitFor } from 'next-test-utils' import {
check,
fetchViaHTTP,
normalizeRouteRegExes,
waitFor,
} from 'next-test-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
const glob = promisify(globOrig) const glob = promisify(globOrig)
...@@ -728,13 +733,7 @@ createNextDescribe( ...@@ -728,13 +733,7 @@ createNextDescribe(
for (const key of Object.keys(curManifest.dynamicRoutes)) { for (const key of Object.keys(curManifest.dynamicRoutes)) {
const item = curManifest.dynamicRoutes[key] const item = curManifest.dynamicRoutes[key]
normalizeRouteRegExes(item)
if (item.dataRouteRegex) {
item.dataRouteRegex = normalizeRegEx(item.dataRouteRegex)
}
if (item.routeRegex) {
item.routeRegex = normalizeRegEx(item.routeRegex)
}
} }
for (const key of Object.keys(curManifest.routes)) { for (const key of Object.keys(curManifest.routes)) {
...@@ -1506,7 +1505,7 @@ createNextDescribe( ...@@ -1506,7 +1505,7 @@ createNextDescribe(
}, },
], ],
"fallback": null, "fallback": null,
"routeRegex": "^\\/articles\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/articles\\/([^\\/]+?)?$",
}, },
"/blog/[author]": { "/blog/[author]": {
"dataRoute": "/blog/[author].rsc", "dataRoute": "/blog/[author].rsc",
...@@ -1523,7 +1522,7 @@ createNextDescribe( ...@@ -1523,7 +1522,7 @@ createNextDescribe(
}, },
], ],
"fallback": false, "fallback": false,
"routeRegex": "^\\/blog\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/blog\\/([^\\/]+?)?$",
}, },
"/blog/[author]/[slug]": { "/blog/[author]/[slug]": {
"dataRoute": "/blog/[author]/[slug].rsc", "dataRoute": "/blog/[author]/[slug].rsc",
...@@ -1540,7 +1539,7 @@ createNextDescribe( ...@@ -1540,7 +1539,7 @@ createNextDescribe(
}, },
], ],
"fallback": null, "fallback": null,
"routeRegex": "^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)?$",
}, },
"/dynamic-error/[id]": { "/dynamic-error/[id]": {
"dataRoute": "/dynamic-error/[id].rsc", "dataRoute": "/dynamic-error/[id].rsc",
...@@ -1557,7 +1556,7 @@ createNextDescribe( ...@@ -1557,7 +1556,7 @@ createNextDescribe(
}, },
], ],
"fallback": null, "fallback": null,
"routeRegex": "^\\/dynamic\\-error\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/dynamic\\-error\\/([^\\/]+?)?$",
}, },
"/force-static/[slug]": { "/force-static/[slug]": {
"dataRoute": "/force-static/[slug].rsc", "dataRoute": "/force-static/[slug].rsc",
...@@ -1574,7 +1573,7 @@ createNextDescribe( ...@@ -1574,7 +1573,7 @@ createNextDescribe(
}, },
], ],
"fallback": null, "fallback": null,
"routeRegex": "^\\/force\\-static\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/force\\-static\\/([^\\/]+?)?$",
}, },
"/gen-params-dynamic-revalidate/[slug]": { "/gen-params-dynamic-revalidate/[slug]": {
"dataRoute": "/gen-params-dynamic-revalidate/[slug].rsc", "dataRoute": "/gen-params-dynamic-revalidate/[slug].rsc",
...@@ -1591,7 +1590,7 @@ createNextDescribe( ...@@ -1591,7 +1590,7 @@ createNextDescribe(
}, },
], ],
"fallback": null, "fallback": null,
"routeRegex": "^\\/gen\\-params\\-dynamic\\-revalidate\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/gen\\-params\\-dynamic\\-revalidate\\/([^\\/]+?)?$",
}, },
"/hooks/use-pathname/[slug]": { "/hooks/use-pathname/[slug]": {
"dataRoute": "/hooks/use-pathname/[slug].rsc", "dataRoute": "/hooks/use-pathname/[slug].rsc",
...@@ -1608,7 +1607,7 @@ createNextDescribe( ...@@ -1608,7 +1607,7 @@ createNextDescribe(
}, },
], ],
"fallback": null, "fallback": null,
"routeRegex": "^\\/hooks\\/use\\-pathname\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/hooks\\/use\\-pathname\\/([^\\/]+?)?$",
}, },
"/partial-gen-params-no-additional-lang/[lang]/[slug]": { "/partial-gen-params-no-additional-lang/[lang]/[slug]": {
"dataRoute": "/partial-gen-params-no-additional-lang/[lang]/[slug].rsc", "dataRoute": "/partial-gen-params-no-additional-lang/[lang]/[slug].rsc",
...@@ -1625,7 +1624,7 @@ createNextDescribe( ...@@ -1625,7 +1624,7 @@ createNextDescribe(
}, },
], ],
"fallback": false, "fallback": false,
"routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-lang\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-lang\\/([^\\/]+?)\\/([^\\/]+?)?$",
}, },
"/partial-gen-params-no-additional-slug/[lang]/[slug]": { "/partial-gen-params-no-additional-slug/[lang]/[slug]": {
"dataRoute": "/partial-gen-params-no-additional-slug/[lang]/[slug].rsc", "dataRoute": "/partial-gen-params-no-additional-slug/[lang]/[slug].rsc",
...@@ -1642,7 +1641,7 @@ createNextDescribe( ...@@ -1642,7 +1641,7 @@ createNextDescribe(
}, },
], ],
"fallback": false, "fallback": false,
"routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-slug\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-slug\\/([^\\/]+?)\\/([^\\/]+?)?$",
}, },
"/ssg-draft-mode/[[...route]]": { "/ssg-draft-mode/[[...route]]": {
"dataRoute": "/ssg-draft-mode/[[...route]].rsc", "dataRoute": "/ssg-draft-mode/[[...route]].rsc",
...@@ -1659,7 +1658,7 @@ createNextDescribe( ...@@ -1659,7 +1658,7 @@ createNextDescribe(
}, },
], ],
"fallback": null, "fallback": null,
"routeRegex": "^\\/ssg\\-draft\\-mode(?:\\/(.+?))?(?:\\/)?$", "routeRegex": "^\\/ssg\\-draft\\-mode(?:\\/(.+?))?$",
}, },
"/static-to-dynamic-error-forced/[id]": { "/static-to-dynamic-error-forced/[id]": {
"dataRoute": "/static-to-dynamic-error-forced/[id].rsc", "dataRoute": "/static-to-dynamic-error-forced/[id].rsc",
...@@ -1676,7 +1675,7 @@ createNextDescribe( ...@@ -1676,7 +1675,7 @@ createNextDescribe(
}, },
], ],
"fallback": null, "fallback": null,
"routeRegex": "^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)?$",
}, },
} }
`) `)
......
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { fetchViaHTTP, normalizeRegEx } from 'next-test-utils' import { fetchViaHTTP, normalizeRouteRegExes } from 'next-test-utils'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
import escapeStringRegexp from 'escape-string-regexp' import escapeStringRegexp from 'escape-string-regexp'
...@@ -151,31 +151,31 @@ createNextDescribe( ...@@ -151,31 +151,31 @@ createNextDescribe(
) )
for (const route of manifest.dataRoutes) { for (const route of manifest.dataRoutes) {
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex) normalizeRouteRegExes(route)
} }
expect(manifest.dataRoutes).toEqual([ expect(manifest.dataRoutes).toEqual(
{ [
dataRouteRegex: normalizeRegEx( {
`^/_next/data/${escapeStringRegexp(next.buildId)}/index.json$` dataRouteRegex: `^/_next/data/${escapeStringRegexp(
),
page: '/',
},
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(
next.buildId next.buildId
)}/([^/]+?)\\.json$` )}/index.json$`,
), page: '/',
namedDataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/(?<nxtPid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nxtPid: 'nxtPid',
}, },
}, {
]) dataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/([^/]+?)\\.json$`,
namedDataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/(?<nxtPid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nxtPid: 'nxtPid',
},
},
].map((item) => normalizeRouteRegExes(item))
)
}) })
} }
} }
......
This diff is collapsed.
// this checks priority issues with catch-all routes that // this checks priority issues with catch-all routes that
// can match `_next/data/build-id/path.json // can match `_next/data/build-id/path.json
export function getServerSideProps() { export function getServerSideProps({ req }) {
if (req.url.startsWith('/_next/static')) {
return {
props: {
now: Date.now(),
},
}
}
return { return {
notFound: true, notFound: true,
} }
......
...@@ -12,8 +12,8 @@ import { ...@@ -12,8 +12,8 @@ import {
fetchViaHTTP, fetchViaHTTP,
renderViaHTTP, renderViaHTTP,
waitFor, waitFor,
normalizeRegEx,
check, check,
normalizeRouteRegExes,
} from 'next-test-utils' } from 'next-test-utils'
const domainLocales = ['go', 'go-BE', 'do', 'do-BE'] const domainLocales = ['go', 'go-BE', 'do', 'do-BE']
...@@ -767,9 +767,7 @@ export function runTests(ctx) { ...@@ -767,9 +767,7 @@ export function runTests(ctx) {
for (const key of Object.keys(prerenderManifest.dynamicRoutes).sort()) { for (const key of Object.keys(prerenderManifest.dynamicRoutes).sort()) {
const item = prerenderManifest.dynamicRoutes[key] const item = prerenderManifest.dynamicRoutes[key]
item.routeRegex = normalizeRegEx(item.routeRegex) dynamicRoutes[key] = normalizeRouteRegExes(item)
item.dataRouteRegex = normalizeRegEx(item.dataRouteRegex)
dynamicRoutes[key] = item
} }
expect( expect(
...@@ -1167,25 +1165,25 @@ export function runTests(ctx) { ...@@ -1167,25 +1165,25 @@ export function runTests(ctx) {
).toMatchInlineSnapshot(` ).toMatchInlineSnapshot(`
"{ "{
"/gsp/fallback/[slug]": { "/gsp/fallback/[slug]": {
"routeRegex": "^\\/gsp\\/fallback\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/gsp\\/fallback\\/([^\\/]+?)?$",
"dataRoute": "/_next/data/BUILD_ID/gsp/fallback/[slug].json", "dataRoute": "/_next/data/BUILD_ID/gsp/fallback/[slug].json",
"fallback": "/gsp/fallback/[slug].html", "fallback": "/gsp/fallback/[slug].html",
"dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/gsp\\/fallback\\/([^\\/]+?)\\.json$" "dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/gsp\\/fallback\\/([^\\/]+?)\\.json$"
}, },
"/gsp/no-fallback/[slug]": { "/gsp/no-fallback/[slug]": {
"routeRegex": "^\\/gsp\\/no\\-fallback\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/gsp\\/no\\-fallback\\/([^\\/]+?)?$",
"dataRoute": "/_next/data/BUILD_ID/gsp/no-fallback/[slug].json", "dataRoute": "/_next/data/BUILD_ID/gsp/no-fallback/[slug].json",
"fallback": false, "fallback": false,
"dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/gsp\\/no\\-fallback\\/([^\\/]+?)\\.json$" "dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/gsp\\/no\\-fallback\\/([^\\/]+?)\\.json$"
}, },
"/not-found/blocking-fallback/[slug]": { "/not-found/blocking-fallback/[slug]": {
"routeRegex": "^\\/not\\-found\\/blocking\\-fallback\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/not\\-found\\/blocking\\-fallback\\/([^\\/]+?)?$",
"dataRoute": "/_next/data/BUILD_ID/not-found/blocking-fallback/[slug].json", "dataRoute": "/_next/data/BUILD_ID/not-found/blocking-fallback/[slug].json",
"fallback": null, "fallback": null,
"dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/not\\-found\\/blocking\\-fallback\\/([^\\/]+?)\\.json$" "dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/not\\-found\\/blocking\\-fallback\\/([^\\/]+?)\\.json$"
}, },
"/not-found/fallback/[slug]": { "/not-found/fallback/[slug]": {
"routeRegex": "^\\/not\\-found\\/fallback\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/not\\-found\\/fallback\\/([^\\/]+?)?$",
"dataRoute": "/_next/data/BUILD_ID/not-found/fallback/[slug].json", "dataRoute": "/_next/data/BUILD_ID/not-found/fallback/[slug].json",
"fallback": "/not-found/fallback/[slug].html", "fallback": "/not-found/fallback/[slug].html",
"dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/not\\-found\\/fallback\\/([^\\/]+?)\\.json$" "dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/not\\-found\\/fallback\\/([^\\/]+?)\\.json$"
......
...@@ -839,7 +839,32 @@ export function getBrowserBodyText(browser: BrowserInterface) { ...@@ -839,7 +839,32 @@ export function getBrowserBodyText(browser: BrowserInterface) {
} }
export function normalizeRegEx(src: string) { export function normalizeRegEx(src: string) {
return new RegExp(src).source.replace(/\^\//g, '^\\/') return (
new RegExp(src).source
.replace(/\^\//g, '^\\/')
// normalize our ignores at the top-level so each
// snapshot doesn't need to be updated each time
.replace('(?!\\/_next\\/static)', '')
.replace('?(?:\\/)?$', '?$')
.replace('(?:\\/)?$', '?$')
)
}
export const normalizeRouteRegExes = (item: any) => {
const fields = [
'regex',
'routeRegex',
'namedRegex',
'namedDataRouteRegex',
'dataRouteRegex',
]
for (const field of fields) {
if (typeof item[field] === 'string') {
item[field] = normalizeRegEx(item[field])
}
}
return item
} }
function readJson(path: string) { function readJson(path: string) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment