diff --git a/packages/next/src/server/lib/router-utils/setup-dev.ts b/packages/next/src/server/lib/router-utils/setup-dev.ts index a87ae086c4507..1a2f5f782de75 100644 --- a/packages/next/src/server/lib/router-utils/setup-dev.ts +++ b/packages/next/src/server/lib/router-utils/setup-dev.ts @@ -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 diff --git a/test/integration/dynamic-routing/pages/[name]/[comment]/[...rest].js b/test/integration/dynamic-routing/pages/[name]/[comment]/[...rest].js new file mode 100644 index 0000000000000..74e30e4905d55 --- /dev/null +++ b/test/integration/dynamic-routing/pages/[name]/[comment]/[...rest].js @@ -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
nested catch-all
+} diff --git a/test/integration/dynamic-routing/test/index.test.js b/test/integration/dynamic-routing/test/index.test.js index 417e5317b36db..0f9530cce5388 100644 --- a/test/integration/dynamic-routing/test/index.test.js +++ b/test/integration/dynamic-routing/test/index.test.js @@ -1318,6 +1318,22 @@ function runTests({ dev }) { nxtPrest: 'nxtPrest', }, }, + { + dataRouteRegex: normalizeRegEx( + `^\\/_next\\/data\\/${escapeRegex( + buildId + )}\\/([^\\/]+?)\\/([^\\/]+?)\\/(.+?)\\.json$` + ), + namedDataRouteRegex: `^/_next/data/${escapeRegex( + buildId + )}/(?