Unverified Commit 01b9603e authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Revert "Ensure dynamic routes dont match _next/static unexpectedly" (#62691)

Reverting temporarily to allow investigation into separate issue
eliminating this as also an issue.

Reverts vercel/next.js#62559
parent 5b204be0
......@@ -181,7 +181,6 @@ 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[]
......@@ -287,17 +286,13 @@ export type RoutesManifest = {
caseSensitive?: boolean
}
function pageToRoute(page: string, restrictedPaths?: string[]) {
function pageToRoute(page: string) {
const routeRegex = getNamedRouteRegex(page, true)
return {
page,
regex: normalizeRouteRegex(
restrictedPaths?.length
? modifyRouteRegex(routeRegex.re.source, restrictedPaths)
: routeRegex.re.source
),
regex: normalizeRouteRegex(routeRegex.re.source),
routeKeys: routeRegex.routeKeys,
namedRegex: modifyRouteRegex(routeRegex.namedRegex, restrictedPaths),
namedRegex: routeRegex.namedRegex,
}
}
......@@ -1094,9 +1089,6 @@ 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
......@@ -1111,7 +1103,7 @@ export default async function build(
for (const route of sortedRoutes) {
if (isDynamicRoute(route)) {
dynamicRoutes.push(pageToRoute(route, restrictedDynamicPaths))
dynamicRoutes.push(pageToRoute(route))
} else if (!isReservedPage(route)) {
staticRoutes.push(pageToRoute(route))
}
......
......@@ -7,7 +7,6 @@ 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[]>
......@@ -25,13 +24,7 @@ export class RouteMatcher<D extends RouteDefinition = RouteDefinition> {
constructor(public readonly definition: D) {
if (isDynamicRoute(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)
this.dynamic = getRouteMatcher(getRouteRegex(definition.pathname))
}
}
......
......@@ -3,12 +3,7 @@ import cheerio from 'cheerio'
import { promisify } from 'util'
import { join } from 'path'
import { createNextDescribe } from 'e2e-utils'
import {
check,
fetchViaHTTP,
normalizeRouteRegExes,
waitFor,
} from 'next-test-utils'
import { check, fetchViaHTTP, normalizeRegEx, waitFor } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
const glob = promisify(globOrig)
......@@ -733,7 +728,13 @@ createNextDescribe(
for (const key of Object.keys(curManifest.dynamicRoutes)) {
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)) {
......@@ -1505,7 +1506,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/articles\\/([^\\/]+?)?$",
"routeRegex": "^\\/articles\\/([^\\/]+?)(?:\\/)?$",
},
"/blog/[author]": {
"dataRoute": "/blog/[author].rsc",
......@@ -1522,7 +1523,7 @@ createNextDescribe(
},
],
"fallback": false,
"routeRegex": "^\\/blog\\/([^\\/]+?)?$",
"routeRegex": "^\\/blog\\/([^\\/]+?)(?:\\/)?$",
},
"/blog/[author]/[slug]": {
"dataRoute": "/blog/[author]/[slug].rsc",
......@@ -1539,7 +1540,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)?$",
"routeRegex": "^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$",
},
"/dynamic-error/[id]": {
"dataRoute": "/dynamic-error/[id].rsc",
......@@ -1556,7 +1557,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/dynamic\\-error\\/([^\\/]+?)?$",
"routeRegex": "^\\/dynamic\\-error\\/([^\\/]+?)(?:\\/)?$",
},
"/force-static/[slug]": {
"dataRoute": "/force-static/[slug].rsc",
......@@ -1573,7 +1574,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/force\\-static\\/([^\\/]+?)?$",
"routeRegex": "^\\/force\\-static\\/([^\\/]+?)(?:\\/)?$",
},
"/gen-params-dynamic-revalidate/[slug]": {
"dataRoute": "/gen-params-dynamic-revalidate/[slug].rsc",
......@@ -1590,7 +1591,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/gen\\-params\\-dynamic\\-revalidate\\/([^\\/]+?)?$",
"routeRegex": "^\\/gen\\-params\\-dynamic\\-revalidate\\/([^\\/]+?)(?:\\/)?$",
},
"/hooks/use-pathname/[slug]": {
"dataRoute": "/hooks/use-pathname/[slug].rsc",
......@@ -1607,7 +1608,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",
......@@ -1624,7 +1625,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",
......@@ -1641,7 +1642,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",
......@@ -1658,7 +1659,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",
......@@ -1675,7 +1676,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)?$",
"routeRegex": "^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)(?:\\/)?$",
},
}
`)
......
import { createNextDescribe } from 'e2e-utils'
import { fetchViaHTTP, normalizeRouteRegExes } from 'next-test-utils'
import { fetchViaHTTP, normalizeRegEx } 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) {
normalizeRouteRegExes(route)
route.dataRouteRegex = normalizeRegEx(route.dataRouteRegex)
}
expect(manifest.dataRoutes).toEqual(
[
{
dataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/index.json$`,
page: '/',
},
{
dataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/([^/]+?)\\.json$`,
namedDataRouteRegex: `^/_next/data/${escapeStringRegexp(
expect(manifest.dataRoutes).toEqual([
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(next.buildId)}/index.json$`
),
page: '/',
},
{
dataRouteRegex: normalizeRegEx(
`^/_next/data/${escapeStringRegexp(
next.buildId
)}/(?<nxtPid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nxtPid: 'nxtPid',
},
)}/([^/]+?)\\.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({ req }) {
if (req.url.startsWith('/_next/static')) {
return {
props: {
now: Date.now(),
},
}
}
export function getServerSideProps() {
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,7 +767,9 @@ export function runTests(ctx) {
for (const key of Object.keys(prerenderManifest.dynamicRoutes).sort()) {
const item = prerenderManifest.dynamicRoutes[key]
dynamicRoutes[key] = normalizeRouteRegExes(item)
item.routeRegex = normalizeRegEx(item.routeRegex)
item.dataRouteRegex = normalizeRegEx(item.dataRouteRegex)
dynamicRoutes[key] = item
}
expect(
......@@ -1165,25 +1167,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$"
......
......@@ -860,32 +860,7 @@ export function getBrowserBodyText(browser: BrowserInterface) {
}
export function normalizeRegEx(src: string) {
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
return new RegExp(src).source.replace(/\^\//g, '^\\/')
}
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