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
import type { Entrypoints } from '../server/dev/turbopack/types'
import { buildCustomRoute } from '../lib/build-custom-route'
import { createProgress } from './progress'
import { modifyRouteRegex } from '../lib/redirect-status'
interface ExperimentalBypassForInfo {
experimentalBypassFor?: RouteHas[]
......@@ -284,13 +285,17 @@ export type RoutesManifest = {
caseSensitive?: boolean
}
function pageToRoute(page: string) {
function pageToRoute(page: string, restrictedPaths?: string[]) {
const routeRegex = getNamedRouteRegex(page, true)
return {
page,
regex: normalizeRouteRegex(routeRegex.re.source),
regex: normalizeRouteRegex(
restrictedPaths?.length
? modifyRouteRegex(routeRegex.re.source, restrictedPaths)
: routeRegex.re.source
),
routeKeys: routeRegex.routeKeys,
namedRegex: routeRegex.namedRegex,
namedRegex: modifyRouteRegex(routeRegex.namedRegex, restrictedPaths),
}
}
......@@ -1087,6 +1092,9 @@ export default async function build(
const restrictedRedirectPaths = ['/_next'].map((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 routesManifest: RoutesManifest = nextBuildSpan
......@@ -1101,7 +1109,7 @@ export default async function build(
for (const route of sortedRoutes) {
if (isDynamicRoute(route)) {
dynamicRoutes.push(pageToRoute(route))
dynamicRoutes.push(pageToRoute(route, restrictedDynamicPaths))
} else if (!isReservedPage(route)) {
staticRoutes.push(pageToRoute(route))
}
......
......@@ -7,6 +7,7 @@ import {
type RouteMatchFn,
} from '../../../shared/lib/router/utils/route-matcher'
import { getRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import { modifyRouteRegex } from '../../../lib/redirect-status'
type RouteMatchResult = {
params?: Record<string, string | string[]>
......@@ -24,7 +25,13 @@ export class RouteMatcher<D extends RouteDefinition = RouteDefinition> {
constructor(public readonly definition: D) {
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'
import { promisify } from 'util'
import { join } from 'path'
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'
const glob = promisify(globOrig)
......@@ -728,13 +733,7 @@ createNextDescribe(
for (const key of Object.keys(curManifest.dynamicRoutes)) {
const item = curManifest.dynamicRoutes[key]
if (item.dataRouteRegex) {
item.dataRouteRegex = normalizeRegEx(item.dataRouteRegex)
}
if (item.routeRegex) {
item.routeRegex = normalizeRegEx(item.routeRegex)
}
normalizeRouteRegExes(item)
}
for (const key of Object.keys(curManifest.routes)) {
......@@ -1506,7 +1505,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/articles\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/articles\\/([^\\/]+?)?$",
},
"/blog/[author]": {
"dataRoute": "/blog/[author].rsc",
......@@ -1523,7 +1522,7 @@ createNextDescribe(
},
],
"fallback": false,
"routeRegex": "^\\/blog\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/blog\\/([^\\/]+?)?$",
},
"/blog/[author]/[slug]": {
"dataRoute": "/blog/[author]/[slug].rsc",
......@@ -1540,7 +1539,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)?$",
},
"/dynamic-error/[id]": {
"dataRoute": "/dynamic-error/[id].rsc",
......@@ -1557,7 +1556,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/dynamic\\-error\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/dynamic\\-error\\/([^\\/]+?)?$",
},
"/force-static/[slug]": {
"dataRoute": "/force-static/[slug].rsc",
......@@ -1574,7 +1573,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/force\\-static\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/force\\-static\\/([^\\/]+?)?$",
},
"/gen-params-dynamic-revalidate/[slug]": {
"dataRoute": "/gen-params-dynamic-revalidate/[slug].rsc",
......@@ -1591,7 +1590,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/gen\\-params\\-dynamic\\-revalidate\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/gen\\-params\\-dynamic\\-revalidate\\/([^\\/]+?)?$",
},
"/hooks/use-pathname/[slug]": {
"dataRoute": "/hooks/use-pathname/[slug].rsc",
......@@ -1608,7 +1607,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/hooks\\/use\\-pathname\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/hooks\\/use\\-pathname\\/([^\\/]+?)?$",
},
"/partial-gen-params-no-additional-lang/[lang]/[slug]": {
"dataRoute": "/partial-gen-params-no-additional-lang/[lang]/[slug].rsc",
......@@ -1625,7 +1624,7 @@ createNextDescribe(
},
],
"fallback": false,
"routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-lang\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-lang\\/([^\\/]+?)\\/([^\\/]+?)?$",
},
"/partial-gen-params-no-additional-slug/[lang]/[slug]": {
"dataRoute": "/partial-gen-params-no-additional-slug/[lang]/[slug].rsc",
......@@ -1642,7 +1641,7 @@ createNextDescribe(
},
],
"fallback": false,
"routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-slug\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-slug\\/([^\\/]+?)\\/([^\\/]+?)?$",
},
"/ssg-draft-mode/[[...route]]": {
"dataRoute": "/ssg-draft-mode/[[...route]].rsc",
......@@ -1659,7 +1658,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/ssg\\-draft\\-mode(?:\\/(.+?))?(?:\\/)?$",
"routeRegex": "^\\/ssg\\-draft\\-mode(?:\\/(.+?))?$",
},
"/static-to-dynamic-error-forced/[id]": {
"dataRoute": "/static-to-dynamic-error-forced/[id].rsc",
......@@ -1676,7 +1675,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)?$",
},
}
`)
......
import { createNextDescribe } from 'e2e-utils'
import { fetchViaHTTP, normalizeRegEx } from 'next-test-utils'
import { fetchViaHTTP, normalizeRouteRegExes } from 'next-test-utils'
import cheerio from 'cheerio'
import { join } from 'path'
import escapeStringRegexp from 'escape-string-regexp'
......@@ -151,31 +151,31 @@ createNextDescribe(
)
for (const route of manifest.dataRoutes) {
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
normalizeRouteRegExes(route)
}
expect(manifest.dataRoutes).toEqual([
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(next.buildId)}/index.json$`
),
page: '/',
},
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(
expect(manifest.dataRoutes).toEqual(
[
{
dataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/([^/]+?)\\.json$`
),
namedDataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/(?<nxtPid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nxtPid: 'nxtPid',
)}/index.json$`,
page: '/',
},
},
])
{
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
// 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 {
notFound: true,
}
......
......@@ -12,8 +12,8 @@ import {
fetchViaHTTP,
renderViaHTTP,
waitFor,
normalizeRegEx,
check,
normalizeRouteRegExes,
} from 'next-test-utils'
const domainLocales = ['go', 'go-BE', 'do', 'do-BE']
......@@ -767,9 +767,7 @@ export function runTests(ctx) {
for (const key of Object.keys(prerenderManifest.dynamicRoutes).sort()) {
const item = prerenderManifest.dynamicRoutes[key]
item.routeRegex = normalizeRegEx(item.routeRegex)
item.dataRouteRegex = normalizeRegEx(item.dataRouteRegex)
dynamicRoutes[key] = item
dynamicRoutes[key] = normalizeRouteRegExes(item)
}
expect(
......@@ -1167,25 +1165,25 @@ export function runTests(ctx) {
).toMatchInlineSnapshot(`
"{
"/gsp/fallback/[slug]": {
"routeRegex": "^\\/gsp\\/fallback\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/gsp\\/fallback\\/([^\\/]+?)?$",
"dataRoute": "/_next/data/BUILD_ID/gsp/fallback/[slug].json",
"fallback": "/gsp/fallback/[slug].html",
"dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/gsp\\/fallback\\/([^\\/]+?)\\.json$"
},
"/gsp/no-fallback/[slug]": {
"routeRegex": "^\\/gsp\\/no\\-fallback\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/gsp\\/no\\-fallback\\/([^\\/]+?)?$",
"dataRoute": "/_next/data/BUILD_ID/gsp/no-fallback/[slug].json",
"fallback": false,
"dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/gsp\\/no\\-fallback\\/([^\\/]+?)\\.json$"
},
"/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",
"fallback": null,
"dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/not\\-found\\/blocking\\-fallback\\/([^\\/]+?)\\.json$"
},
"/not-found/fallback/[slug]": {
"routeRegex": "^\\/not\\-found\\/fallback\\/([^\\/]+?)(?:\\/)?$",
"routeRegex": "^\\/not\\-found\\/fallback\\/([^\\/]+?)?$",
"dataRoute": "/_next/data/BUILD_ID/not-found/fallback/[slug].json",
"fallback": "/not-found/fallback/[slug].html",
"dataRouteRegex": "^\\/_next\\/data\\/BUILD_ID\\/not\\-found\\/fallback\\/([^\\/]+?)\\.json$"
......
......@@ -839,7 +839,32 @@ export function getBrowserBodyText(browser: BrowserInterface) {
}
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) {
......
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