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

Fix data route ordering in dev (#54364)

Previously we were appending the data routes to the dynamic routes array
which didn't ensure the data routes come before the normal dynamic
routes allowing a catch-all to override the data route.

Fixes: https://github.com/vercel/next.js/issues/53887
parent 705b2ef2
......@@ -1246,10 +1246,12 @@ async function startWatcher(opts: SetupOpts) {
})
.filter(Boolean) as any
const dataRoutes: typeof opts.fsChecker.dynamicRoutes = []
for (const page of sortedRoutes) {
const route = buildDataRoute(page, 'development')
const routeRegex = getRouteRegex(route.page)
opts.fsChecker.dynamicRoutes.push({
dataRoutes.push({
...route,
regex: routeRegex.re.toString(),
match: getRouteMatcher({
......@@ -1267,6 +1269,7 @@ async function startWatcher(opts: SetupOpts) {
}),
})
}
opts.fsChecker.dynamicRoutes.unshift(...dataRoutes)
if (!prevSortedRoutes?.every((val, idx) => val === sortedRoutes[idx])) {
// emit the change so clients fetch the update
......
// this checks priority issues with catch-all routes that
// can match `_next/data/build-id/path.json
export function getServerSideProps() {
return {
notFound: true,
}
}
export default function Page() {
return <p>nested catch-all</p>
}
......@@ -1318,6 +1318,22 @@ function runTests({ dev }) {
nxtPrest: 'nxtPrest',
},
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/([^\\/]+?)\\/([^\\/]+?)\\/(.+?)\\.json$`
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/(?<nxtPname>[^/]+?)/(?<nxtPcomment>[^/]+?)/(?<nxtPrest>.+?)\\.json$`,
page: '/[name]/[comment]/[...rest]',
routeKeys: {
nxtPcomment: 'nxtPcomment',
nxtPname: 'nxtPname',
nxtPrest: 'nxtPrest',
},
},
],
dynamicRoutes: [
{
......@@ -1458,6 +1474,19 @@ function runTests({ dev }) {
nxtPcomment: 'nxtPcomment',
},
},
{
namedRegex:
'^/(?<nxtPname>[^/]+?)/(?<nxtPcomment>[^/]+?)/(?<nxtPrest>.+?)(?:/)?$',
page: '/[name]/[comment]/[...rest]',
regex: normalizeRegEx(
'^\\/([^\\/]+?)\\/([^\\/]+?)\\/(.+?)(?:\\/)?$'
),
routeKeys: {
nxtPcomment: 'nxtPcomment',
nxtPname: 'nxtPname',
nxtPrest: 'nxtPrest',
},
},
],
rsc: {
header: 'RSC',
......@@ -1475,6 +1504,7 @@ function runTests({ dev }) {
expect(manifest).toEqual({
'/[name]/[comment]': 'pages/[name]/[comment].js',
'/[name]/[comment]/[...rest]': 'pages/[name]/[comment]/[...rest].js',
'/[name]/comments': 'pages/[name]/comments.js',
'/[name]': 'pages/[name].js',
'/[name]/on-mount-redir': 'pages/[name]/on-mount-redir.html',
......
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