Skip to content

Commit

Permalink
Fix catchall rewrites for _next/data routes (#39370)
Browse files Browse the repository at this point in the history
* Fix catchall rewrites for _next/data routes

* update check
  • Loading branch information
ijjk committed Aug 6, 2022
1 parent d008f65 commit a679a1e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
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

0 comments on commit a679a1e

Please sign in to comment.