Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix catchall rewrites for _next/data routes #39370

Merged
merged 2 commits into from Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/server/base-server.ts
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion packages/next/server/router.ts
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions test/integration/custom-routes/next.config.js
Expand Up @@ -204,6 +204,10 @@ module.exports = {
source: '/blog/about',
destination: '/hello',
},
{
source: '/overridden/:path*',
destination: '/overridden',
},
],
beforeFiles: [
{
Expand Down
18 changes: 18 additions & 0 deletions test/integration/custom-routes/pages/overridden/[slug].js
@@ -0,0 +1,18 @@
export default function Page() {
return <p>/overriden/[slug]</p>
}

export function getStaticProps({ params }) {
return {
props: {
params,
},
}
}

export function getStaticPaths() {
return {
paths: [],
fallback: 'blocking',
}
}
42 changes: 42 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Expand Up @@ -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 [
{
Expand Down Expand Up @@ -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: [
{
Expand Down Expand Up @@ -2028,6 +2056,12 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/blog\\/about(?:\\/)?$'),
source: '/blog/about',
},
{
destination: '/overridden',
regex:
'^\\/overridden(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))?(?:\\/)?$',
source: '/overridden/:path*',
},
],
fallback: [],
},
Expand Down Expand Up @@ -2088,6 +2122,14 @@ const runTests = (isDev = false) => {
slug: 'slug',
},
},
{
namedRegex: '^/overridden/(?<slug>[^/]+?)(?:/)?$',
page: '/overridden/[slug]',
regex: '^\\/overridden\\/([^\\/]+?)(?:\\/)?$',
routeKeys: {
slug: 'slug',
},
},
],
staticRoutes: [
{
Expand Down