Unverified Commit 81f87e08 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure x-nextjs-data header is only set during resolve (#92752)

Makes sure we set this header during route resolving so user request
doesn't need to.
parent ff89de45
...@@ -200,6 +200,11 @@ export function getResolveRoutes( ...@@ -200,6 +200,11 @@ export function getResolveRoutes(
return pathname return pathname
} }
const setIsNextDataRequest = () => {
addRequestMeta(req, 'isNextDataReq', true)
req.headers['x-nextjs-data'] = '1'
}
let domainLocale: ReturnType<typeof detectDomainLocale> | undefined let domainLocale: ReturnType<typeof detectDomainLocale> | undefined
let defaultLocale: string | undefined let defaultLocale: string | undefined
let initialLocaleResult: let initialLocaleResult:
...@@ -328,7 +333,7 @@ export function getResolveRoutes( ...@@ -328,7 +333,7 @@ export function getResolveRoutes(
} }
if (pageOutput && curPathname?.startsWith('/_next/data')) { if (pageOutput && curPathname?.startsWith('/_next/data')) {
addRequestMeta(req, 'isNextDataReq', true) setIsNextDataRequest()
} }
if (config.useFileSystemPublicRoutes || didRewrite) { if (config.useFileSystemPublicRoutes || didRewrite) {
...@@ -424,12 +429,19 @@ export function getResolveRoutes( ...@@ -424,12 +429,19 @@ export function getResolveRoutes(
normalized = normalizers.basePath.normalize(normalized, true) normalized = normalizers.basePath.normalize(normalized, true)
} }
const isNextDataPath =
pathHasPrefix(normalized, '/_next/data') &&
normalized.endsWith('.json')
const hasCurrentBuildIdDataPath = normalizers.data.match(normalized)
let updated = false let updated = false
if (normalizers.data.match(normalized)) { if (hasCurrentBuildIdDataPath) {
updated = true updated = true
addRequestMeta(req, 'isNextDataReq', true)
normalized = normalizers.data.normalize(normalized, true) normalized = normalizers.data.normalize(normalized, true)
} }
if (isNextDataPath) {
setIsNextDataRequest()
}
if (config.i18n) { if (config.i18n) {
const curLocaleResult = normalizeLocalePath( const curLocaleResult = normalizeLocalePath(
......
...@@ -48,6 +48,7 @@ const INTERNAL_HEADERS = [ ...@@ -48,6 +48,7 @@ const INTERNAL_HEADERS = [
'x-middleware-next', 'x-middleware-next',
'x-now-route-matches', 'x-now-route-matches',
'x-matched-path', 'x-matched-path',
'x-nextjs-data',
'x-next-resume-state-length', 'x-next-resume-state-length',
] ]
......
...@@ -108,6 +108,19 @@ describe('Middleware Runtime', () => { ...@@ -108,6 +108,19 @@ describe('Middleware Runtime', () => {
} }
function runTests({ i18n }: { i18n?: boolean }) { function runTests({ i18n }: { i18n?: boolean }) {
it('should not treat as _next/data request with just header', async () => {
const res = await next.fetch('/redirect-to-somewhere', {
redirect: 'manual',
headers: {
'x-nextjs-data': '1',
},
})
expect(res.status).toBe(307)
expect(res.headers.get('Location')).toContain('/somewhere')
expect(res.headers.get('x-nextjs-redirect')).toBe(null)
})
if (isNodeMiddleware) { if (isNodeMiddleware) {
it('should be able to use node builtins with node runtime', async () => { it('should be able to use node builtins with node runtime', async () => {
const res = await next.fetch('/test-node-fs') const res = await next.fetch('/test-node-fs')
......
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