Unverified Commit 92fdcc75 authored by Adrian Hartanto's avatar Adrian Hartanto Committed by GitHub

update: allow providing stale-while-revalidate delta (#61330)

<!-- 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 #

-->

Closes: https://github.com/vercel/next.js/pull/52251
Closes: https://github.com/vercel/next.js/issues/51823

### Adding a feature

Added `stale-while-revalidate` delta amount to indicate that the cache
could reuse a stale response while it revalidates,
[reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control).
It's also already supported by
[CloudFront](https://aws.amazon.com/about-aws/whats-new/2023/05/amazon-cloudfront-stale-while-revalidate-stale-if-error-cache-control-directives/).

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 544444b9
...@@ -511,6 +511,7 @@ export async function exportAppImpl( ...@@ -511,6 +511,7 @@ export async function exportAppImpl(
ppr: nextConfig.experimental.ppr === true, ppr: nextConfig.experimental.ppr === true,
missingSuspenseWithCSRBailout: missingSuspenseWithCSRBailout:
nextConfig.experimental.missingSuspenseWithCSRBailout === true, nextConfig.experimental.missingSuspenseWithCSRBailout === true,
swrDelta: nextConfig.experimental.swrDelta,
}, },
} }
......
...@@ -5,6 +5,7 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight ...@@ -5,6 +5,7 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight
import type { NextFontManifest } from '../../build/webpack/plugins/next-font-manifest-plugin' import type { NextFontManifest } from '../../build/webpack/plugins/next-font-manifest-plugin'
import type { ParsedUrlQuery } from 'querystring' import type { ParsedUrlQuery } from 'querystring'
import type { AppPageModule } from '../future/route-modules/app-page/module' import type { AppPageModule } from '../future/route-modules/app-page/module'
import type { SwrDelta } from '../lib/revalidate'
import s from 'next/dist/compiled/superstruct' import s from 'next/dist/compiled/superstruct'
...@@ -143,7 +144,11 @@ export interface RenderOptsPartial { ...@@ -143,7 +144,11 @@ export interface RenderOptsPartial {
} }
params?: ParsedUrlQuery params?: ParsedUrlQuery
isPrefetch?: boolean isPrefetch?: boolean
experimental: { ppr: boolean; missingSuspenseWithCSRBailout: boolean } experimental: {
ppr: boolean
missingSuspenseWithCSRBailout: boolean
swrDelta: SwrDelta | undefined
}
postponed?: string postponed?: string
} }
......
...@@ -54,7 +54,11 @@ import { RedirectStatusCode } from '../client/components/redirect-status-code' ...@@ -54,7 +54,11 @@ import { RedirectStatusCode } from '../client/components/redirect-status-code'
import { isDynamicRoute } from '../shared/lib/router/utils' import { isDynamicRoute } from '../shared/lib/router/utils'
import { checkIsOnDemandRevalidate } from './api-utils' import { checkIsOnDemandRevalidate } from './api-utils'
import { setConfig } from '../shared/lib/runtime-config.external' import { setConfig } from '../shared/lib/runtime-config.external'
import { formatRevalidate, type Revalidate } from './lib/revalidate' import {
formatRevalidate,
type Revalidate,
type SwrDelta,
} from './lib/revalidate'
import { execOnce } from '../shared/lib/utils' import { execOnce } from '../shared/lib/utils'
import { isBlockedPage } from './utils' import { isBlockedPage } from './utils'
import { isBot } from '../shared/lib/router/utils/is-bot' import { isBot } from '../shared/lib/router/utils/is-bot'
...@@ -325,6 +329,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -325,6 +329,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
generateEtags: boolean generateEtags: boolean
poweredByHeader: boolean poweredByHeader: boolean
revalidate?: Revalidate revalidate?: Revalidate
swrDelta?: SwrDelta
} }
): Promise<void> ): Promise<void>
...@@ -513,6 +518,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -513,6 +518,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
this.nextConfig.experimental.ppr === true, this.nextConfig.experimental.ppr === true,
missingSuspenseWithCSRBailout: missingSuspenseWithCSRBailout:
this.nextConfig.experimental.missingSuspenseWithCSRBailout === true, this.nextConfig.experimental.missingSuspenseWithCSRBailout === true,
swrDelta: this.nextConfig.experimental.swrDelta,
}, },
} }
...@@ -1555,6 +1561,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1555,6 +1561,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
generateEtags, generateEtags,
poweredByHeader, poweredByHeader,
revalidate, revalidate,
swrDelta: this.nextConfig.experimental.swrDelta,
}) })
res.statusCode = originalStatus res.statusCode = originalStatus
} }
...@@ -2709,7 +2716,13 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2709,7 +2716,13 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (!cachedData) { if (!cachedData) {
if (cacheEntry.revalidate) { if (cacheEntry.revalidate) {
res.setHeader('Cache-Control', formatRevalidate(cacheEntry.revalidate)) res.setHeader(
'Cache-Control',
formatRevalidate({
revalidate: cacheEntry.revalidate,
swrDelta: this.nextConfig.experimental.swrDelta,
})
)
} }
if (isDataReq) { if (isDataReq) {
res.statusCode = 404 res.statusCode = 404
...@@ -2725,7 +2738,13 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2725,7 +2738,13 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return null return null
} else if (cachedData.kind === 'REDIRECT') { } else if (cachedData.kind === 'REDIRECT') {
if (cacheEntry.revalidate) { if (cacheEntry.revalidate) {
res.setHeader('Cache-Control', formatRevalidate(cacheEntry.revalidate)) res.setHeader(
'Cache-Control',
formatRevalidate({
revalidate: cacheEntry.revalidate,
swrDelta: this.nextConfig.experimental.swrDelta,
})
)
} }
if (isDataReq) { if (isDataReq) {
......
...@@ -267,6 +267,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -267,6 +267,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
externalMiddlewareRewritesResolve: z.boolean().optional(), externalMiddlewareRewritesResolve: z.boolean().optional(),
fallbackNodePolyfills: z.literal(false).optional(), fallbackNodePolyfills: z.literal(false).optional(),
fetchCacheKeyPrefix: z.string().optional(), fetchCacheKeyPrefix: z.string().optional(),
swrDelta: z.number().optional(),
forceSwcTransforms: z.boolean().optional(), forceSwcTransforms: z.boolean().optional(),
fullySpecified: z.boolean().optional(), fullySpecified: z.boolean().optional(),
gzipSize: z.boolean().optional(), gzipSize: z.boolean().optional(),
......
...@@ -10,6 +10,7 @@ import type { SubresourceIntegrityAlgorithm } from '../build/webpack/plugins/sub ...@@ -10,6 +10,7 @@ import type { SubresourceIntegrityAlgorithm } from '../build/webpack/plugins/sub
import type { WEB_VITALS } from '../shared/lib/utils' import type { WEB_VITALS } from '../shared/lib/utils'
import type { NextParsedUrlQuery } from './request-meta' import type { NextParsedUrlQuery } from './request-meta'
import type { SizeLimit } from '../../types' import type { SizeLimit } from '../../types'
import type { SwrDelta } from './lib/revalidate'
export type NextConfigComplete = Required<NextConfig> & { export type NextConfigComplete = Required<NextConfig> & {
images: Required<ImageConfigComplete> images: Required<ImageConfigComplete>
...@@ -184,6 +185,10 @@ export interface ExperimentalConfig { ...@@ -184,6 +185,10 @@ export interface ExperimentalConfig {
allowedRevalidateHeaderKeys?: string[] allowedRevalidateHeaderKeys?: string[]
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
optimisticClientCache?: boolean optimisticClientCache?: boolean
/**
* period (in seconds) where the server allow to serve stale cache
*/
swrDelta?: SwrDelta
middlewarePrefetch?: 'strict' | 'flexible' middlewarePrefetch?: 'strict' | 'flexible'
manualClientBasePath?: boolean manualClientBasePath?: boolean
/** /**
...@@ -840,6 +845,7 @@ export const defaultConfig: NextConfig = { ...@@ -840,6 +845,7 @@ export const defaultConfig: NextConfig = {
fetchCacheKeyPrefix: '', fetchCacheKeyPrefix: '',
middlewarePrefetch: 'flexible', middlewarePrefetch: 'flexible',
optimisticClientCache: true, optimisticClientCache: true,
swrDelta: undefined,
manualClientBasePath: false, manualClientBasePath: false,
cpus: Math.max( cpus: Math.max(
1, 1,
......
...@@ -8,13 +8,24 @@ import { CACHE_ONE_YEAR } from '../../lib/constants' ...@@ -8,13 +8,24 @@ import { CACHE_ONE_YEAR } from '../../lib/constants'
* value for this option. * value for this option.
*/ */
export type Revalidate = number | false export type Revalidate = number | false
export type SwrDelta = number
export function formatRevalidate({
revalidate,
swrDelta,
}: {
revalidate: Revalidate
swrDelta?: SwrDelta
}): string {
const swrHeader = swrDelta
? `stale-while-revalidate=${swrDelta}`
: 'stale-while-revalidate'
export function formatRevalidate(revalidate: Revalidate): string {
if (revalidate === 0) { if (revalidate === 0) {
return 'private, no-cache, no-store, max-age=0, must-revalidate' return 'private, no-cache, no-store, max-age=0, must-revalidate'
} else if (typeof revalidate === 'number') { } else if (typeof revalidate === 'number') {
return `s-maxage=${revalidate}, stale-while-revalidate` return `s-maxage=${revalidate}, ${swrHeader}`
} }
return `s-maxage=${CACHE_ONE_YEAR}, stale-while-revalidate` return `s-maxage=${CACHE_ONE_YEAR}, ${swrHeader}`
} }
...@@ -23,7 +23,7 @@ import type { PagesAPIRouteModule } from './future/route-modules/pages-api/modul ...@@ -23,7 +23,7 @@ import type { PagesAPIRouteModule } from './future/route-modules/pages-api/modul
import type { UrlWithParsedQuery } from 'url' import type { UrlWithParsedQuery } from 'url'
import type { ParsedUrlQuery } from 'querystring' import type { ParsedUrlQuery } from 'querystring'
import type { ParsedUrl } from '../shared/lib/router/utils/parse-url' import type { ParsedUrl } from '../shared/lib/router/utils/parse-url'
import type { Revalidate } from './lib/revalidate' import type { Revalidate, SwrDelta } from './lib/revalidate'
import fs from 'fs' import fs from 'fs'
import { join, resolve } from 'path' import { join, resolve } from 'path'
...@@ -425,6 +425,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -425,6 +425,7 @@ export default class NextNodeServer extends BaseServer {
generateEtags: boolean generateEtags: boolean
poweredByHeader: boolean poweredByHeader: boolean
revalidate: Revalidate | undefined revalidate: Revalidate | undefined
swrDelta: SwrDelta | undefined
} }
): Promise<void> { ): Promise<void> {
return sendRenderResult({ return sendRenderResult({
...@@ -435,6 +436,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -435,6 +436,7 @@ export default class NextNodeServer extends BaseServer {
generateEtags: options.generateEtags, generateEtags: options.generateEtags,
poweredByHeader: options.poweredByHeader, poweredByHeader: options.poweredByHeader,
revalidate: options.revalidate, revalidate: options.revalidate,
swrDelta: options.swrDelta,
}) })
} }
......
...@@ -37,7 +37,7 @@ import type { NextFontManifest } from '../build/webpack/plugins/next-font-manife ...@@ -37,7 +37,7 @@ import type { NextFontManifest } from '../build/webpack/plugins/next-font-manife
import type { PagesModule } from './future/route-modules/pages/module' import type { PagesModule } from './future/route-modules/pages/module'
import type { ComponentsEnhancer } from '../shared/lib/utils' import type { ComponentsEnhancer } from '../shared/lib/utils'
import type { NextParsedUrlQuery } from './request-meta' import type { NextParsedUrlQuery } from './request-meta'
import type { Revalidate } from './lib/revalidate' import type { Revalidate, SwrDelta } from './lib/revalidate'
import type { COMPILER_NAMES } from '../shared/lib/constants' import type { COMPILER_NAMES } from '../shared/lib/constants'
import React from 'react' import React from 'react'
...@@ -289,6 +289,7 @@ export type RenderOptsPartial = { ...@@ -289,6 +289,7 @@ export type RenderOptsPartial = {
isServerAction?: boolean isServerAction?: boolean
isExperimentalCompile?: boolean isExperimentalCompile?: boolean
isPrefetch?: boolean isPrefetch?: boolean
swrDelta?: SwrDelta
} }
export type RenderOpts = LoadComponentsReturnType<PagesModule> & export type RenderOpts = LoadComponentsReturnType<PagesModule> &
...@@ -457,6 +458,7 @@ export async function renderToHTMLImpl( ...@@ -457,6 +458,7 @@ export async function renderToHTMLImpl(
images, images,
runtime: globalRuntime, runtime: globalRuntime,
isExperimentalCompile, isExperimentalCompile,
swrDelta,
} = renderOpts } = renderOpts
const { App } = extra const { App } = extra
...@@ -518,7 +520,13 @@ export async function renderToHTMLImpl( ...@@ -518,7 +520,13 @@ export async function renderToHTMLImpl(
// ensure we set cache header so it's not rendered on-demand // ensure we set cache header so it's not rendered on-demand
// every request // every request
if (isAutoExport && !dev && isExperimentalCompile) { if (isAutoExport && !dev && isExperimentalCompile) {
res.setHeader('Cache-Control', formatRevalidate(false)) res.setHeader(
'Cache-Control',
formatRevalidate({
revalidate: false,
swrDelta,
})
)
isAutoExport = false isAutoExport = false
} }
......
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import type RenderResult from './render-result' import type RenderResult from './render-result'
import type { Revalidate } from './lib/revalidate' import type { Revalidate, SwrDelta } from './lib/revalidate'
import { isResSent } from '../shared/lib/utils' import { isResSent } from '../shared/lib/utils'
import { generateETag } from './lib/etag' import { generateETag } from './lib/etag'
...@@ -40,6 +40,7 @@ export async function sendRenderResult({ ...@@ -40,6 +40,7 @@ export async function sendRenderResult({
generateEtags, generateEtags,
poweredByHeader, poweredByHeader,
revalidate, revalidate,
swrDelta,
}: { }: {
req: IncomingMessage req: IncomingMessage
res: ServerResponse res: ServerResponse
...@@ -48,6 +49,7 @@ export async function sendRenderResult({ ...@@ -48,6 +49,7 @@ export async function sendRenderResult({
generateEtags: boolean generateEtags: boolean
poweredByHeader: boolean poweredByHeader: boolean
revalidate: Revalidate | undefined revalidate: Revalidate | undefined
swrDelta: SwrDelta | undefined
}): Promise<void> { }): Promise<void> {
if (isResSent(res)) { if (isResSent(res)) {
return return
...@@ -58,7 +60,13 @@ export async function sendRenderResult({ ...@@ -58,7 +60,13 @@ export async function sendRenderResult({
} }
if (typeof revalidate !== 'undefined') { if (typeof revalidate !== 'undefined') {
res.setHeader('Cache-Control', formatRevalidate(revalidate)) res.setHeader(
'Cache-Control',
formatRevalidate({
revalidate,
swrDelta,
})
)
} }
const payload = result.isDynamic ? null : result.toUnchunkedString() const payload = result.isDynamic ? null : result.toUnchunkedString()
......
...@@ -11,7 +11,7 @@ import type { ...@@ -11,7 +11,7 @@ import type {
Options, Options,
RouteHandler, RouteHandler,
} from './base-server' } from './base-server'
import type { Revalidate } from './lib/revalidate' import type { Revalidate, SwrDelta } from './lib/revalidate'
import { byteLength } from './api-utils/web' import { byteLength } from './api-utils/web'
import BaseServer, { NoFallbackError } from './base-server' import BaseServer, { NoFallbackError } from './base-server'
...@@ -266,6 +266,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> { ...@@ -266,6 +266,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
generateEtags: boolean generateEtags: boolean
poweredByHeader: boolean poweredByHeader: boolean
revalidate: Revalidate | undefined revalidate: Revalidate | undefined
swrDelta: SwrDelta | undefined
} }
): Promise<void> { ): Promise<void> {
res.setHeader('X-Edge-Runtime', '1') res.setHeader('X-Edge-Runtime', '1')
......
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