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

Fix catchall rewrites for _next/data routes (#39370)

* Fix catchall rewrites for _next/data routes

* update check
parent d008f655
......@@ -734,6 +734,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
match: getPathMatch('/_next/data/:path*'),
type: 'route',
name: '_next/data catchall',
check: true,
fn: async (req, res, params, _parsedUrl) => {
// Make sure to 404 for /_next/data/ itself and
// we also want to 404 if the buildId isn't correct
......
......@@ -222,7 +222,14 @@ export default class Router {
this.catchAllMiddleware
const allRoutes = [
...(middlewareCatchAllRoute
? this.fsRoutes.filter((r) => r.name === '_next/data catchall')
? this.fsRoutes
.filter((r) => r.name === '_next/data catchall')
.map((r) => {
return {
...r,
check: false,
}
})
: []),
...this.headers,
...this.redirects,
......
......@@ -204,6 +204,10 @@ module.exports = {
source: '/blog/about',
destination: '/hello',
},
{
source: '/overridden/:path*',
destination: '/overridden',
},
],
beforeFiles: [
{
......
export default function Page() {
return <p>/overriden/[slug]</p>
}
export function getStaticProps({ params }) {
return {
props: {
params,
},
}
}
export function getStaticPaths() {
return {
paths: [],
fallback: 'blocking',
}
}
......@@ -39,6 +39,22 @@ let appPort
let app
const runTests = (isDev = false) => {
it('should not rewrite for _next/data route when a match is found', async () => {
const initial = await fetchViaHTTP(appPort, '/overridden/first')
expect(initial.status).toBe(200)
expect(await initial.text()).toContain('this page is overridden')
const nextData = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/overridden/first.json`
)
expect(nextData.status).toBe(200)
expect(await nextData.json()).toEqual({
pageProps: { params: { slug: 'first' } },
__N_SSG: true,
})
})
it('should handle has query encoding correctly', async () => {
for (const expected of [
{
......@@ -1271,6 +1287,18 @@ const runTests = (isDev = false) => {
slug: 'slug',
},
},
{
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/overridden\\/([^\\/]+?)\\.json$`,
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/overridden/(?<slug>[^/]+?)\\.json$`,
page: '/overridden/[slug]',
routeKeys: {
slug: 'slug',
},
},
],
redirects: [
{
......@@ -2028,6 +2056,12 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/blog\\/about(?:\\/)?$'),
source: '/blog/about',
},
{
destination: '/overridden',
regex:
'^\\/overridden(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))?(?:\\/)?$',
source: '/overridden/:path*',
},
],
fallback: [],
},
......@@ -2088,6 +2122,14 @@ const runTests = (isDev = false) => {
slug: 'slug',
},
},
{
namedRegex: '^/overridden/(?<slug>[^/]+?)(?:/)?$',
page: '/overridden/[slug]',
regex: '^\\/overridden\\/([^\\/]+?)(?:\\/)?$',
routeKeys: {
slug: 'slug',
},
},
],
staticRoutes: [
{
......
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