Skip to content

Commit

Permalink
Fix data route ordering in dev (#54364)
Browse files Browse the repository at this point in the history
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: #53887
  • Loading branch information
ijjk authored Aug 22, 2023
1 parent 705b2ef commit c89f97e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/next/src/server/lib/router-utils/setup-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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>
}
30 changes: 30 additions & 0 deletions test/integration/dynamic-routing/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit c89f97e

Please sign in to comment.