Unverified Commit e84b65b0 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Expose allowHeader (#72033)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

Adds a new `allowHeader` to allow Next.js to specify which headers
should be included during revalidations that are used internally by
Next.js.
parent 43353ede
......@@ -29,6 +29,11 @@ import {
RSC_PREFETCH_SUFFIX,
RSC_SUFFIX,
NEXT_RESUME_HEADER,
PRERENDER_REVALIDATE_HEADER,
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER,
NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER,
NEXT_CACHE_REVALIDATED_TAGS_HEADER,
MATCHED_PATH_HEADER,
} from '../lib/constants'
import { FileType, fileExists } from '../lib/file-exists'
import { findPagesDir } from '../lib/find-pages-dir'
......@@ -255,6 +260,12 @@ export interface SsgRoute {
* route.
*/
renderingMode: RenderingMode | undefined
/**
* The headers that are allowed to be used when revalidating this route. These
* are used internally by Next.js to revalidate routes.
*/
allowHeader: string[]
}
export interface DynamicSsgRoute {
......@@ -292,8 +303,26 @@ export interface DynamicSsgRoute {
* route.
*/
renderingMode: RenderingMode | undefined
/**
* The headers that are allowed to be used when revalidating this route. These
* are used internally by Next.js to revalidate routes.
*/
allowHeader: string[]
}
/**
* The headers that are allowed to be used when revalidating routes. Currently
* this includes both headers used by the pages and app routers.
*/
const ALLOWED_HEADERS: string[] = [
MATCHED_PATH_HEADER,
PRERENDER_REVALIDATE_HEADER,
PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER,
NEXT_CACHE_REVALIDATED_TAGS_HEADER,
NEXT_CACHE_REVALIDATE_TAG_TOKEN_HEADER,
]
export type PrerenderManifest = {
version: 4
routes: { [route: string]: SsgRoute }
......@@ -3220,6 +3249,7 @@ export default async function build(
srcRoute: page,
dataRoute,
prefetchDataRoute,
allowHeader: ALLOWED_HEADERS,
}
} else {
hasRevalidateZero = true
......@@ -3326,6 +3356,7 @@ export default async function build(
'\\.prefetch\\.rsc$'
)
),
allowHeader: ALLOWED_HEADERS,
}
}
}
......@@ -3557,6 +3588,7 @@ export default async function build(
`${file}.json`
),
prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS,
}
}
} else {
......@@ -3573,6 +3605,7 @@ export default async function build(
),
// Pages does not have a prefetch data route.
prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS,
}
}
// Set Page Revalidation Interval
......@@ -3643,6 +3676,7 @@ export default async function build(
),
// Pages does not have a prefetch data route.
prefetchDataRoute: undefined,
allowHeader: ALLOWED_HEADERS,
}
// Set route Revalidation Interval
......@@ -3738,6 +3772,7 @@ export default async function build(
// Pages does not have a prefetch data route.
prefetchDataRoute: undefined,
prefetchDataRouteRegex: undefined,
allowHeader: ALLOWED_HEADERS,
}
})
......
......@@ -3,6 +3,7 @@ import type { ServerRuntime } from '../types'
export const NEXT_QUERY_PARAM_PREFIX = 'nxtP'
export const NEXT_INTERCEPTION_MARKER_PREFIX = 'nxtI'
export const MATCHED_PATH_HEADER = 'x-matched-path'
export const PRERENDER_REVALIDATE_HEADER = 'x-prerender-revalidate'
export const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER =
'x-prerender-revalidate-if-generated'
......
......@@ -132,6 +132,7 @@ import {
import {
CACHE_ONE_YEAR,
INFINITE_CACHE,
MATCHED_PATH_HEADER,
NEXT_CACHE_REVALIDATED_TAGS_HEADER,
NEXT_CACHE_TAGS_HEADER,
NEXT_RESUME_HEADER,
......@@ -1050,7 +1051,7 @@ export default abstract class Server<
}
const useMatchedPathHeader =
this.minimalMode && typeof req.headers['x-matched-path'] === 'string'
this.minimalMode && typeof req.headers[MATCHED_PATH_HEADER] === 'string'
// TODO: merge handling with invokePath
if (useMatchedPathHeader) {
......@@ -1068,7 +1069,7 @@ export default abstract class Server<
// x-matched-path is the source of truth, it tells what page
// should be rendered because we don't process rewrites in minimalMode
let { pathname: matchedPath } = new URL(
req.headers['x-matched-path'] as string,
req.headers[MATCHED_PATH_HEADER] as string,
'http://localhost'
)
......@@ -2072,7 +2073,7 @@ export default abstract class Server<
req.headers['x-middleware-prefetch'] &&
!(is404Page || pathname === '/_error')
) {
res.setHeader('x-matched-path', pathname)
res.setHeader(MATCHED_PATH_HEADER, pathname)
res.setHeader('x-middleware-skip', '1')
res.setHeader(
'cache-control',
......@@ -2089,7 +2090,7 @@ export default abstract class Server<
if (
isSSG &&
this.minimalMode &&
req.headers['x-matched-path'] &&
req.headers[MATCHED_PATH_HEADER] &&
req.url.startsWith('/_next/data')
) {
req.url = this.stripNextDataPath(req.url)
......@@ -2290,7 +2291,7 @@ export default abstract class Server<
checkIsOnDemandRevalidate(req, this.renderOpts.previewProps))
}
if (isSSG && this.minimalMode && req.headers['x-matched-path']) {
if (isSSG && this.minimalMode && req.headers[MATCHED_PATH_HEADER]) {
// the url value is already correct when the matched-path header is set
resolvedUrlPathname = urlPathname
}
......@@ -3770,7 +3771,7 @@ export default abstract class Server<
{
page,
url: ctx.req.url,
matchedPath: ctx.req.headers['x-matched-path'],
matchedPath: ctx.req.headers[MATCHED_PATH_HEADER],
initUrl: getRequestMeta(ctx.req, 'initURL'),
didRewrite: !!getRequestMeta(ctx.req, 'rewroteURL'),
rewroteUrl: getRequestMeta(ctx.req, 'rewroteURL'),
......
......@@ -15,6 +15,7 @@ describe('SharedRevalidateTimings', () => {
prefetchDataRoute: null,
experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
allowHeader: [],
},
'/route2': {
initialRevalidateSeconds: 20,
......@@ -23,6 +24,7 @@ describe('SharedRevalidateTimings', () => {
prefetchDataRoute: null,
experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
allowHeader: [],
},
},
dynamicRoutes: {},
......
......@@ -44,6 +44,7 @@ import { omit } from './utils/omit'
import { interpolateAs } from './utils/interpolate-as'
import { handleSmoothScroll } from './utils/handle-smooth-scroll'
import type { Params } from '../../../server/request/params'
import { MATCHED_PATH_HEADER } from '../../../lib/constants'
declare global {
interface Window {
......@@ -174,7 +175,7 @@ function getMiddlewareData<T extends FetchDataOutput>(
let rewriteTarget =
rewriteHeader || response.headers.get('x-nextjs-matched-path')
const matchedPath = response.headers.get('x-matched-path')
const matchedPath = response.headers.get(MATCHED_PATH_HEADER)
if (
matchedPath &&
......
This diff is collapsed.
......@@ -67,6 +67,13 @@ const runTests = (isDev) => {
join(appDir, '.next/prerender-manifest.json')
)
expect(data.routes['/404']).toEqual({
allowHeader: [
'x-matched-path',
'x-prerender-revalidate',
'x-prerender-revalidate-if-generated',
'x-next-revalidated-tags',
'x-next-revalidate-tag-token',
],
initialRevalidateSeconds: false,
srcRoute: null,
dataRoute: `/_next/data/${buildId}/404.json`,
......
......@@ -53,7 +53,7 @@ describe('server-side dev errors', () => {
const err = stderr.slice(stderrIdx)
return err.includes('pages/gsp.js') &&
err.includes('6:2') &&
err.includes('6:3') &&
err.includes('getStaticProps') &&
err.includes('missingVar')
? 'success'
......
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