Skip to content

Commit

Permalink
Revert "Ensure dynamic routes dont match _next/static unexpectedly (#…
Browse files Browse the repository at this point in the history
…62559)"

This reverts commit e1e6a07.
  • Loading branch information
ijjk authored Feb 29, 2024
1 parent c262e61 commit c8357c4
Show file tree
Hide file tree
Showing 11 changed files with 1,035 additions and 920 deletions.
16 changes: 4 additions & 12 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]>
Expand All @@ -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))
}
}

Expand Down
37 changes: 19 additions & 18 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -1505,7 +1506,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/articles\\/([^\\/]+?)?$",
"routeRegex": "^\\/articles\\/([^\\/]+?)(?:\\/)?$",
},
"/blog/[author]": {
"dataRoute": "/blog/[author].rsc",
Expand All @@ -1522,7 +1523,7 @@ createNextDescribe(
},
],
"fallback": false,
"routeRegex": "^\\/blog\\/([^\\/]+?)?$",
"routeRegex": "^\\/blog\\/([^\\/]+?)(?:\\/)?$",
},
"/blog/[author]/[slug]": {
"dataRoute": "/blog/[author]/[slug].rsc",
Expand All @@ -1539,7 +1540,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)?$",
"routeRegex": "^\\/blog\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$",
},
"/dynamic-error/[id]": {
"dataRoute": "/dynamic-error/[id].rsc",
Expand All @@ -1556,7 +1557,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/dynamic\\-error\\/([^\\/]+?)?$",
"routeRegex": "^\\/dynamic\\-error\\/([^\\/]+?)(?:\\/)?$",
},
"/force-static/[slug]": {
"dataRoute": "/force-static/[slug].rsc",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -1675,7 +1676,7 @@ createNextDescribe(
},
],
"fallback": null,
"routeRegex": "^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)?$",
"routeRegex": "^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)(?:\\/)?$",
},
}
`)
Expand Down
44 changes: 22 additions & 22 deletions test/e2e/edge-pages-support/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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'
Expand Down Expand Up @@ -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))
)
},
])
})
}
}
Expand Down
Loading

0 comments on commit c8357c4

Please sign in to comment.