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 middleware rewrite for _next/data #41341

Merged
merged 2 commits into from Oct 11, 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
8 changes: 8 additions & 0 deletions packages/next/server/next-server.ts
Expand Up @@ -1062,9 +1062,17 @@ export default class NextNodeServer extends BaseServer {
name: '_next/data catchall',
check: true,
fn: async (req, res, params, _parsedUrl) => {
const isNextDataNormalizing = getRequestMeta(
req,
'_nextDataNormalizing'
)

// Make sure to 404 for /_next/data/ itself and
// we also want to 404 if the buildId isn't correct
if (!params.path || params.path[0] !== this.buildId) {
if (isNextDataNormalizing) {
return { finished: false }
}
await this.render404(req, res, _parsedUrl)
return {
finished: true,
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/request-meta.ts
Expand Up @@ -23,6 +23,7 @@ export interface RequestMeta {
_nextRewroteUrl?: string
_nextMiddlewareCookie?: string[]
_protocol?: string
_nextDataNormalizing?: boolean
}

export function getRequestMeta(
Expand Down
21 changes: 19 additions & 2 deletions packages/next/server/router.ts
Expand Up @@ -7,7 +7,11 @@ import type {
} from '../shared/lib/router/utils/route-matcher'
import type { RouteHas } from '../lib/load-custom-routes'

import { getNextInternalQuery, NextUrlWithParsedQuery } from './request-meta'
import {
addRequestMeta,
getNextInternalQuery,
NextUrlWithParsedQuery,
} from './request-meta'
import { getPathMatch } from '../shared/lib/router/utils/path-match'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
Expand Down Expand Up @@ -189,7 +193,11 @@ export default class Router {
...(middlewareCatchAllRoute
? this.fsRoutes
.filter((route) => route.name === '_next/data catchall')
.map((route) => ({ ...route, check: false }))
.map((route) => ({
...route,
name: '_next/data normalizing',
check: false,
}))
: []),
...this.headers,
...this.redirects,
Expand Down Expand Up @@ -433,6 +441,11 @@ export default class Router {
}

if (params) {
const isNextDataNormalizing = route.name === '_next/data normalizing'

if (isNextDataNormalizing) {
addRequestMeta(req, '_nextDataNormalizing', true)
}
parsedUrlUpdated.pathname = matchPathname
const result = await route.fn(
req,
Expand All @@ -441,6 +454,10 @@ export default class Router {
parsedUrlUpdated,
upgradeHead
)

if (isNextDataNormalizing) {
addRequestMeta(req, '_nextDataNormalizing', false)
}
if (result.finished) {
return true
}
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/middleware-rewrites/app/middleware.js
Expand Up @@ -13,6 +13,11 @@ export async function middleware(request) {
return NextResponse.next()
}

if (url.pathname.startsWith('/_next/data/missing-id')) {
console.log(`missing-id rewrite: ${url.toString()}`)
return NextResponse.rewrite('https://example.vercel.sh')
}

if (url.pathname.includes('/to/some/404/path')) {
return NextResponse.next({
'x-matched-path': '/404',
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/middleware-rewrites/test/index.test.ts
Expand Up @@ -23,6 +23,23 @@ describe('Middleware Rewrite', () => {
})

function tests() {
it('should allow rewriting invalid buildId correctly', async () => {
const res = await fetchViaHTTP(
next.url,
'/_next/data/missing-id/hello.json',
undefined,
{
headers: {
'x-nextjs-data': '1',
},
}
)
expect(res.status).toBe(200)
expect(await res.text()).toContain('Example Domain')
await check(() => next.cliOutput, /missing-id rewrite/)
expect(next.cliOutput).toContain('/_next/data/missing-id/hello.json')
})

it('should not have un-necessary data request on rewrite', async () => {
const browser = await webdriver(next.url, '/to-blog/first', {
waitHydration: false,
Expand Down