Unverified Commit 3ae9d382 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Propagate expire time to `cache-control` header and prerender manifest (#76207)

For the `stale-while-revalidate` value in the `cache-control` response header, Next.js currently uses the configured
[`expireTime`](https://nextjs.org/docs/app/api-reference/config/next-config-js/expireTime), which applies to all routes, and defaults to one year.

With the introduction of `"use cache"` and granular [cache lifetimes](https://nextjs.org/docs/app/api-reference/functions/cacheLife), users can now set expire times per route — based on the minimum expire time of all cached functions used by that route.

This PR updates the `stale-while-revalidate` value to reflect the route's cache lifetime, using the difference between the minimum expire time and the minimum revalidate time. If no explicit expire time is defined in cache profiles, the globally configured `expireTime` is used.

Additionally, the collected expire time is added to the prerender manifest as `initialExpireSeconds` (and `fallbackExpire`), analogous to `initialRevalidateSeconds` (or `fallbackRevalidate`).

closes NAR-100
parent 9483a371
......@@ -652,5 +652,10 @@
"651": "Unexpected module type %s",
"652": "Expected cached value for cache key %s not to be a %s kind, got \"FETCH\" instead.",
"653": "Expected cached value for cache key %s to be a \"FETCH\" kind, got %s instead.",
"654": "Segment Cache experiment is not enabled. This is a bug in Next.js."
"654": "Segment Cache experiment is not enabled. This is a bug in Next.js.",
"655": "If providing both the stale and expire options, the expire option must be greater than the stale option. The expire option indicates how many seconds from the start until it can no longer be used.",
"656": "If providing both the revalidate and expire options, the expire option must be greater than the revalidate option. The expire option indicates how many seconds from the start until it can no longer be used.",
"657": "revalidate must be a number for image-cache",
"658": "Pass `Infinity` instead of `false` if you want to cache on the server forever without checking with the origin.",
"659": "SSG should not return an image cache value"
}
This diff is collapsed.
......@@ -81,6 +81,7 @@ import { collectRootParamKeys } from './segment-config/app/collect-root-param-ke
import { buildAppStaticPaths } from './static-paths/app'
import { buildPagesStaticPaths } from './static-paths/pages'
import type { PrerenderedRoute } from './static-paths/types'
import type { CacheControl } from '../server/lib/cache-control'
export type ROUTER_TYPE = 'pages' | 'app'
......@@ -346,7 +347,8 @@ export interface PageInfo {
*/
isRoutePPREnabled: boolean
ssgPageRoutes: string[] | null
initialRevalidateSeconds: number | false
// TODO: initialCacheControl should be set per prerendered route.
initialCacheControl: CacheControl | undefined
pageDuration: number | undefined
ssgPageDurations: number[] | undefined
runtime: ServerRuntime
......@@ -520,12 +522,14 @@ export async function printTreeView(
usedSymbols.add(symbol)
if (pageInfo?.initialRevalidateSeconds) usedSymbols.add('ISR')
// TODO: Rework this to be usable for app router routes.
// See https://vercel.slack.com/archives/C02CDC2ALJH/p1739552318644119?thread_ts=1739550179.439319&cid=C02CDC2ALJH
if (pageInfo?.initialCacheControl?.revalidate) usedSymbols.add('ISR')
messages.push([
`${border} ${symbol} ${
pageInfo?.initialRevalidateSeconds
? `${item} (ISR: ${pageInfo?.initialRevalidateSeconds} Seconds)`
pageInfo?.initialCacheControl?.revalidate
? `${item} (ISR: ${pageInfo?.initialCacheControl.revalidate} Seconds)`
: item
}${
totalDuration > MIN_DURATION
......
......@@ -646,8 +646,8 @@ async function exportAppImpl(
if (options.buildExport) {
// Update path info by path.
const info = collector.byPath.get(path) ?? {}
if (typeof result.revalidate !== 'undefined') {
info.revalidate = result.revalidate
if (result.cacheControl) {
info.cacheControl = result.cacheControl
}
if (typeof result.metadata !== 'undefined') {
info.metadata = result.metadata
......
......@@ -139,7 +139,7 @@ export async function exportAppPage(
const { metadata } = result
const {
flightData,
revalidate = false,
cacheControl = { revalidate: false, expire: undefined },
postponed,
fetchTags,
fetchMetrics,
......@@ -151,7 +151,7 @@ export async function exportAppPage(
throw new Error('Invariant: page postponed without PPR being enabled')
}
if (revalidate === 0) {
if (cacheControl.revalidate === 0) {
if (isDynamicError) {
throw new Error(
`Page with dynamic = "error" encountered dynamic data method on ${path}.`
......@@ -159,7 +159,7 @@ export async function exportAppPage(
}
const { staticBailoutInfo = {} } = metadata
if (revalidate === 0 && debugOutput && staticBailoutInfo?.description) {
if (debugOutput && staticBailoutInfo?.description) {
logDynamicUsageWarning({
path,
description: staticBailoutInfo.description,
......@@ -167,7 +167,7 @@ export async function exportAppPage(
})
}
return { revalidate: 0, fetchMetrics }
return { cacheControl, fetchMetrics }
}
// If page data isn't available, it means that the page couldn't be rendered
......@@ -270,7 +270,7 @@ export async function exportAppPage(
metadata: hasNextSupport ? meta : undefined,
hasEmptyPrelude: Boolean(postponed) && html === '',
hasPostponed: Boolean(postponed),
revalidate,
cacheControl,
fetchMetrics,
}
} catch (err) {
......@@ -298,7 +298,7 @@ export async function exportAppPage(
})
}
return { revalidate: 0, fetchMetrics }
return { cacheControl: { revalidate: 0, expire: undefined }, fetchMetrics }
}
}
......
......@@ -114,14 +114,14 @@ export async function exportAppRoute(
// unless specifically opted into
experimental.dynamicIO !== true
) {
return { revalidate: 0 }
return { cacheControl: { revalidate: 0, expire: undefined } }
}
const response = await module.handle(request, context)
const isValidStatus = response.status < 400 || response.status === 404
if (!isValidStatus) {
return { revalidate: 0 }
return { cacheControl: { revalidate: 0, expire: undefined } }
}
const blob = await response.blob()
......@@ -136,6 +136,12 @@ export async function exportAppRoute(
? false
: context.renderOpts.collectedRevalidate
const expire =
typeof context.renderOpts.collectedExpire === 'undefined' ||
context.renderOpts.collectedExpire >= INFINITE_CACHE
? undefined
: context.renderOpts.collectedExpire
const headers = toNodeOutgoingHttpHeaders(response.headers)
const cacheTags = context.renderOpts.collectedTags
......@@ -159,7 +165,7 @@ export async function exportAppRoute(
)
return {
revalidate: revalidate,
cacheControl: { revalidate, expire },
metadata: meta,
}
} catch (err) {
......@@ -167,6 +173,6 @@ export async function exportAppRoute(
throw err
}
return { revalidate: 0 }
return { cacheControl: { revalidate: 0, expire: undefined } }
}
}
......@@ -219,7 +219,10 @@ export async function exportPagesPage(
return {
ampValidations,
revalidate: metadata.revalidate ?? false,
cacheControl: metadata.cacheControl ?? {
revalidate: false,
expire: undefined,
},
ssgNotFound,
}
}
......@@ -4,7 +4,7 @@ import type { LoadComponentsReturnType } from '../server/load-components'
import type { OutgoingHttpHeaders } from 'http'
import type AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import type { ExportPathMap, NextConfigComplete } from '../server/config-shared'
import type { Revalidate } from '../server/lib/revalidate'
import type { CacheControl } from '../server/lib/cache-control'
import type { NextEnabledDirectories } from '../server/base-server'
import type {
SerializableTurborepoAccessTraceResult,
......@@ -68,7 +68,7 @@ export interface ExportPageInput {
export type ExportRouteResult =
| {
ampValidations?: AmpValidation[]
revalidate: Revalidate
cacheControl: CacheControl
metadata?: Partial<RouteMetadata>
ssgNotFound?: boolean
hasEmptyPrelude?: boolean
......@@ -128,9 +128,9 @@ export type ExportAppResult = {
string,
{
/**
* The revalidation time for the page in seconds.
* The cache control for the page.
*/
revalidate?: Revalidate
cacheControl?: CacheControl
/**
* The metadata for the page.
*/
......
......@@ -595,7 +595,7 @@ async function exportPage(
return {
duration: Date.now() - start,
ampValidations: result.ampValidations,
revalidate: result.revalidate,
cacheControl: result.cacheControl,
metadata: result.metadata,
ssgNotFound: result.ssgNotFound,
hasEmptyPrelude: result.hasEmptyPrelude,
......
......@@ -1422,17 +1422,23 @@ async function renderToHTMLOrFlightImpl(
// If force static is specifically set to false, we should not revalidate
// the page.
if (workStore.forceStatic === false || response.collectedRevalidate === 0) {
metadata.revalidate = 0
metadata.cacheControl = { revalidate: 0, expire: undefined }
} else {
// Copy the revalidation value onto the render result metadata.
metadata.revalidate =
response.collectedRevalidate >= INFINITE_CACHE
? false
: response.collectedRevalidate
// Copy the cache control value onto the render result metadata.
metadata.cacheControl = {
revalidate:
response.collectedRevalidate >= INFINITE_CACHE
? false
: response.collectedRevalidate,
expire:
response.collectedExpire >= INFINITE_CACHE
? undefined
: response.collectedExpire,
}
}
// provide bailout info for debugging
if (metadata.revalidate === 0) {
if (metadata.cacheControl?.revalidate === 0) {
metadata.staticBailoutInfo = {
description: workStore.dynamicUsageDescription,
stack: workStore.dynamicUsageStack,
......
......@@ -5,7 +5,6 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight
import type { NextFontManifest } from '../../build/webpack/plugins/next-font-manifest-plugin'
import type { ParsedUrlQuery } from 'querystring'
import type { AppPageModule } from '../route-modules/app-page/module'
import type { ExpireTime } from '../lib/revalidate'
import type {
HeadData,
LoadingModuleData,
......@@ -212,7 +211,7 @@ export interface RenderOptsPartial {
* prerendering.
*/
isRoutePPREnabled?: boolean
expireTime: ExpireTime | undefined
expireTime: number | undefined
clientTraceMetadata: string[] | undefined
dynamicIO: boolean
clientSegmentCache: boolean
......
This diff is collapsed.
......@@ -10,7 +10,6 @@ import type { SubresourceIntegrityAlgorithm } from '../build/webpack/plugins/sub
import type { WEB_VITALS } from '../shared/lib/utils'
import type { NextParsedUrlQuery } from './request-meta'
import type { SizeLimit } from '../types'
import type { ExpireTime } from './lib/revalidate'
import type { SupportedTestRunners } from '../cli/next-test'
import type { ExperimentalPPRConfig } from './lib/experimental/ppr'
import { INFINITE_CACHE } from '../lib/constants'
......@@ -316,7 +315,7 @@ export interface ExperimentalConfig {
/**
* @deprecated use config.expireTime instead
*/
expireTime?: ExpireTime
expireTime?: number
middlewarePrefetch?: 'strict' | 'flexible'
manualClientBasePath?: boolean
/**
......@@ -1027,7 +1026,7 @@ export interface NextConfig extends Record<string, any> {
/**
* period (in seconds) where the server allow to serve stale cache
*/
expireTime?: ExpireTime
expireTime?: number
/**
* Enable experimental features. Note that all experimental features are subject to breaking changes in the future.
......@@ -1127,7 +1126,9 @@ export const defaultConfig: NextConfig = {
keepAlive: true,
},
logging: {},
expireTime: process.env.__NEXT_TEST_MODE ? undefined : 31536000,
expireTime: process.env.NEXT_PRIVATE_CDN_CONSUMED_SWR_CACHE_CONTROL
? undefined
: 31536000, // one year
staticPageGenerationTimeout: 60,
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined,
......
......@@ -27,6 +27,8 @@ import { getContentType, getExtension } from './serve-static'
import * as Log from '../build/output/log'
import isError from '../lib/is-error'
import { parseUrl } from '../lib/url'
import type { CacheControl } from './lib/cache-control'
import { InvariantError } from '../shared/lib/invariant-error'
type XCacheHeader = 'MISS' | 'HIT' | 'STALE'
......@@ -414,7 +416,7 @@ export class ImageOptimizerCache {
revalidateAfter:
Math.max(maxAge, this.nextConfig.images.minimumCacheTTL) * 1000 +
Date.now(),
revalidate: maxAge,
cacheControl: { revalidate: maxAge, expire: undefined },
isStale: now > expireAt,
isFallback: false,
}
......@@ -428,18 +430,21 @@ export class ImageOptimizerCache {
cacheKey: string,
value: IncrementalCacheValue | null,
{
revalidate,
cacheControl,
}: {
revalidate?: number | false
cacheControl?: CacheControl
}
) {
if (value?.kind !== CachedRouteKind.IMAGE) {
throw new Error('invariant attempted to set non-image to image-cache')
}
const revalidate = cacheControl?.revalidate
if (typeof revalidate !== 'number') {
throw new Error('invariant revalidate must be a number for image-cache')
throw new InvariantError('revalidate must be a number for image-cache')
}
const expireAt =
Math.max(revalidate, this.nextConfig.images.minimumCacheTTL) * 1000 +
Date.now()
......@@ -772,7 +777,7 @@ export async function imageOptimizer(
return {
buffer: previouslyCachedImage.buffer,
contentType,
maxAge: opts?.previousCacheEntry?.revalidate || maxAge,
maxAge: opts?.previousCacheEntry?.cacheControl?.revalidate || maxAge,
etag: previouslyCachedImage.etag,
upstreamEtag: previouslyCachedImage.upstreamEtag,
}
......
......@@ -8,27 +8,28 @@ import { CACHE_ONE_YEAR } from '../../lib/constants'
* value for this option.
*/
export type Revalidate = number | false
export type ExpireTime = number
export function formatRevalidate({
revalidate,
expireTime,
}: {
export interface CacheControl {
revalidate: Revalidate
expireTime?: ExpireTime
}): string {
expire: number | undefined
}
export function getCacheControlHeader({
revalidate,
expire,
}: CacheControl): string {
const swrHeader =
typeof revalidate === 'number' && expireTime !== undefined
? revalidate >= expireTime
? ''
: `stale-while-revalidate=${expireTime - revalidate}`
: 'stale-while-revalidate'
typeof revalidate === 'number' &&
expire !== undefined &&
revalidate < expire
? `, stale-while-revalidate=${expire - revalidate}`
: ''
if (revalidate === 0) {
return 'private, no-cache, no-store, max-age=0, must-revalidate'
} else if (typeof revalidate === 'number') {
return `s-maxage=${revalidate}, ${swrHeader}`
return `s-maxage=${revalidate}${swrHeader}`
}
return `s-maxage=${CACHE_ONE_YEAR}, ${swrHeader}`
return `s-maxage=${CACHE_ONE_YEAR}${swrHeader}`
}
......@@ -14,7 +14,6 @@ import {
type SetIncrementalFetchCacheContext,
type SetIncrementalResponseCacheContext,
} from '../../response-cache'
import type { Revalidate } from '../revalidate'
import type { DeepReadonly } from '../../../shared/lib/deep-readonly'
import FileSystemCache from './file-system-cache'
......@@ -27,7 +26,7 @@ import {
PRERENDER_REVALIDATE_HEADER,
} from '../../../lib/constants'
import { toRoute } from '../to-route'
import { SharedRevalidateTimings } from './shared-revalidate-timings'
import { SharedCacheControls } from './shared-cache-controls'
import { workUnitAsyncStorageInstance } from '../../app-render/work-unit-async-storage-instance'
import {
getPrerenderResumeDataCache,
......@@ -35,6 +34,7 @@ import {
} from '../../app-render/work-unit-async-storage.external'
import { getCacheHandlers } from '../../use-cache/handlers'
import { InvariantError } from '../../../shared/lib/invariant-error'
import type { Revalidate } from '../cache-control'
export interface CacheHandlerContext {
fs?: CacheFs
......@@ -96,10 +96,10 @@ export class IncrementalCache implements IncrementalCacheType {
private readonly locks = new Map<string, Promise<void>>()
/**
* The revalidate timings for routes. This will source the timings from the
* prerender manifest until the in-memory cache is updated with new timings.
* The cache controls for routes. This will source the values from the
* prerender manifest until the in-memory cache is updated with new values.
*/
private readonly revalidateTimings: SharedRevalidateTimings
private readonly cacheControls: SharedCacheControls
constructor({
fs,
......@@ -170,7 +170,7 @@ export class IncrementalCache implements IncrementalCacheType {
this.requestProtocol = requestProtocol
this.allowedRevalidateHeaderKeys = allowedRevalidateHeaderKeys
this.prerenderManifest = getPrerenderManifest()
this.revalidateTimings = new SharedRevalidateTimings(this.prerenderManifest)
this.cacheControls = new SharedCacheControls(this.prerenderManifest)
this.fetchCacheKeyPrefix = fetchCacheKeyPrefix
let revalidatedTags: string[] = []
......@@ -216,10 +216,15 @@ export class IncrementalCache implements IncrementalCacheType {
if (dev)
return Math.floor(performance.timeOrigin + performance.now() - 1000)
const cacheControl = this.cacheControls.get(toRoute(pathname))
// if an entry isn't present in routes we fallback to a default
// of revalidating after 1 second unless it's a fallback request.
const initialRevalidateSeconds =
this.revalidateTimings.get(toRoute(pathname)) ?? (isFallback ? false : 1)
const initialRevalidateSeconds = cacheControl
? cacheControl.revalidate
: isFallback
? false
: 1
const revalidateAfter =
typeof initialRevalidateSeconds === 'number'
......@@ -488,8 +493,7 @@ export class IncrementalCache implements IncrementalCacheType {
let entry: IncrementalResponseCacheEntry | null = null
const { isFallback } = ctx
const revalidate = this.revalidateTimings.get(toRoute(cacheKey))
const cacheControl = this.cacheControls.get(toRoute(cacheKey))
let isStale: boolean | -1 | undefined
let revalidateAfter: Revalidate
......@@ -514,7 +518,7 @@ export class IncrementalCache implements IncrementalCacheType {
if (cacheData) {
entry = {
isStale,
revalidate,
cacheControl,
revalidateAfter,
value: cacheData.value,
isFallback,
......@@ -533,11 +537,11 @@ export class IncrementalCache implements IncrementalCacheType {
entry = {
isStale,
value: null,
revalidate,
cacheControl,
revalidateAfter,
isFallback,
}
this.set(cacheKey, entry.value, { ...ctx, revalidate })
this.set(cacheKey, entry.value, { ...ctx, cacheControl })
}
return entry
}
......@@ -594,10 +598,8 @@ export class IncrementalCache implements IncrementalCacheType {
}
try {
// Set the value for the revalidate seconds so if it changes we can
// update the cache with the new value.
if (!ctx.fetchCache && typeof ctx.revalidate !== 'undefined') {
this.revalidateTimings.set(toRoute(pathname), ctx.revalidate)
if (!ctx.fetchCache && ctx.cacheControl) {
this.cacheControls.set(toRoute(pathname), ctx.cacheControl)
}
await this.cacheHandler?.set(pathname, data, ctx)
......
import type {
DynamicPrerenderManifestRoute,
PrerenderManifestRoute,
} from '../../../build'
import { RenderingMode } from '../../../build/rendering-mode'
import { SharedCacheControls } from './shared-cache-controls'
describe('SharedCacheControls', () => {
let sharedCacheControls: SharedCacheControls
let prerenderManifest
beforeEach(() => {
prerenderManifest = {
routes: {
'/route1': {
initialRevalidateSeconds: 10,
initialExpireSeconds: undefined,
dataRoute: null,
srcRoute: null,
prefetchDataRoute: null,
experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
allowHeader: [],
} satisfies PrerenderManifestRoute,
'/route2': {
initialRevalidateSeconds: 20,
initialExpireSeconds: 40,
dataRoute: null,
srcRoute: null,
prefetchDataRoute: null,
experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
allowHeader: [],
} satisfies PrerenderManifestRoute,
},
dynamicRoutes: {
'/route4': {
fallbackRevalidate: 30,
fallbackExpire: 50,
fallback: true,
fallbackRootParams: undefined,
fallbackSourceRoute: undefined,
dataRoute: null,
dataRouteRegex: null,
prefetchDataRoute: null,
prefetchDataRouteRegex: null,
routeRegex: '',
experimentalPPR: undefined,
renderingMode: RenderingMode.PARTIALLY_STATIC,
allowHeader: [],
} satisfies DynamicPrerenderManifestRoute,
},
}
sharedCacheControls = new SharedCacheControls(prerenderManifest)
})
afterEach(() => {
sharedCacheControls.clear()
})
it('should get cache control from in-memory cache', () => {
sharedCacheControls.set('/route1', { revalidate: 15, expire: undefined })
const cacheControl = sharedCacheControls.get('/route1')
expect(cacheControl).toEqual({ revalidate: 15 })
})
it('should get cache control from prerender manifest if not in cache', () => {
const cacheControl = sharedCacheControls.get('/route2')
expect(cacheControl).toEqual({ revalidate: 20, expire: 40 })
})
it('should return undefined if cache control not found', () => {
const cacheControl = sharedCacheControls.get('/route3')
expect(cacheControl).toBeUndefined()
})
it('should set cache control in cache', () => {
sharedCacheControls.set('/route3', { revalidate: 30, expire: undefined })
const cacheControl = sharedCacheControls.get('/route3')
expect(cacheControl).toEqual({ revalidate: 30 })
})
it('should clear the in-memory cache', () => {
sharedCacheControls.set('/route3', { revalidate: 30, expire: undefined })
sharedCacheControls.clear()
const cacheControl = sharedCacheControls.get('/route3')
expect(cacheControl).toBeUndefined()
})
it('should get cache control from prerender manifest for dynamic route with fallback', () => {
const cacheControl = sharedCacheControls.get('/route4')
expect(cacheControl).toEqual({ revalidate: 30, expire: 50 })
})
})
import type { PrerenderManifest } from '../../../build'
import type { DeepReadonly } from '../../../shared/lib/deep-readonly'
import type { CacheControl } from '../cache-control'
/**
* A shared cache of cache controls for routes. This cache is used so we don't
* have to modify the prerender manifest when we want to update the cache
* control for a route.
*/
export class SharedCacheControls {
/**
* The in-memory cache of cache lives for routes. This cache is populated when
* the cache is updated with new cache lives.
*/
private static readonly cacheControls = new Map<string, CacheControl>()
constructor(
/**
* The prerender manifest that contains the initial cache controls for
* routes.
*/
private readonly prerenderManifest: DeepReadonly<
Pick<PrerenderManifest, 'routes' | 'dynamicRoutes'>
>
) {}
/**
* Try to get the cache control value for a route. This will first try to get
* the value from the in-memory cache. If the value is not present in the
* in-memory cache, it will be sourced from the prerender manifest.
*
* @param route the route to get the cache control for
* @returns the cache control for the route, or undefined if the values
* are not present in the in-memory cache or the prerender manifest
*/
public get(route: string): CacheControl | undefined {
// This is a copy on write cache that is updated when the cache is updated.
// If the cache is never written to, then the values will be sourced from
// the prerender manifest.
let cacheControl = SharedCacheControls.cacheControls.get(route)
if (cacheControl) return cacheControl
let prerenderData = this.prerenderManifest.routes[route]
if (prerenderData) {
const { initialRevalidateSeconds, initialExpireSeconds } = prerenderData
if (typeof initialRevalidateSeconds !== 'undefined') {
return {
revalidate: initialRevalidateSeconds,
expire: initialExpireSeconds,
}
}
}
const dynamicPrerenderData = this.prerenderManifest.dynamicRoutes[route]
if (dynamicPrerenderData) {
const { fallbackRevalidate, fallbackExpire } = dynamicPrerenderData
if (typeof fallbackRevalidate !== 'undefined') {
return { revalidate: fallbackRevalidate, expire: fallbackExpire }
}
}
return undefined
}
/**
* Set the cache control for a route.
*
* @param route the route to set the cache control for
* @param cacheControl the cache control for the route
*/
public set(route: string, cacheControl: CacheControl) {
SharedCacheControls.cacheControls.set(route, cacheControl)
}
/**
* Clear the in-memory cache of cache controls for routes.
*/
public clear() {
SharedCacheControls.cacheControls.clear()
}
}
import { RenderingMode } from '../../../build/rendering-mode'
import { SharedRevalidateTimings } from './shared-revalidate-timings'
describe('SharedRevalidateTimings', () => {
let sharedRevalidateTimings: SharedRevalidateTimings
let prerenderManifest
beforeEach(() => {
prerenderManifest = {
routes: {
'/route1': {
initialRevalidateSeconds: 10,
dataRoute: null,
srcRoute: null,
prefetchDataRoute: null,
experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
allowHeader: [],
},
'/route2': {
initialRevalidateSeconds: 20,
dataRoute: null,
srcRoute: null,
prefetchDataRoute: null,
experimentalPPR: undefined,
renderingMode: RenderingMode.STATIC,
allowHeader: [],
},
},
dynamicRoutes: {},
}
sharedRevalidateTimings = new SharedRevalidateTimings(prerenderManifest)
})
afterEach(() => {
sharedRevalidateTimings.clear()
})
it('should get revalidate timing from in-memory cache', () => {
sharedRevalidateTimings.set('/route1', 15)
const revalidate = sharedRevalidateTimings.get('/route1')
expect(revalidate).toBe(15)
})
it('should get revalidate timing from prerender manifest if not in cache', () => {
const revalidate = sharedRevalidateTimings.get('/route2')
expect(revalidate).toBe(20)
})
it('should return undefined if revalidate timing not found', () => {
const revalidate = sharedRevalidateTimings.get('/route3')
expect(revalidate).toBeUndefined()
})
it('should set revalidate timing in cache', () => {
sharedRevalidateTimings.set('/route3', 30)
const revalidate = sharedRevalidateTimings.get('/route3')
expect(revalidate).toBe(30)
})
it('should clear the in-memory cache', () => {
sharedRevalidateTimings.set('/route3', 30)
sharedRevalidateTimings.clear()
const revalidate = sharedRevalidateTimings.get('/route3')
expect(revalidate).toBeUndefined()
})
})
import type { PrerenderManifest } from '../../../build'
import type { DeepReadonly } from '../../../shared/lib/deep-readonly'
import type { Revalidate } from '../revalidate'
/**
* A shared cache of revalidate timings for routes. This cache is used so we
* don't have to modify the prerender manifest when we want to update the
* revalidate timings for a route.
*/
export class SharedRevalidateTimings {
/**
* The in-memory cache of revalidate timings for routes. This cache is
* populated when the cache is updated with new timings.
*/
private static readonly timings = new Map<string, Revalidate>()
constructor(
/**
* The prerender manifest that contains the initial revalidate timings for
* routes.
*/
private readonly prerenderManifest: DeepReadonly<
Pick<PrerenderManifest, 'routes' | 'dynamicRoutes'>
>
) {}
/**
* Try to get the revalidate timings for a route. This will first try to get
* the timings from the in-memory cache. If the timings are not present in the
* in-memory cache, then the timings will be sourced from the prerender
* manifest.
*
* @param route the route to get the revalidate timings for
* @returns the revalidate timings for the route, or undefined if the timings
* are not present in the in-memory cache or the prerender manifest
*/
public get(route: string): Revalidate | undefined {
// This is a copy on write cache that is updated when the cache is updated.
// If the cache is never written to, then the timings will be sourced from
// the prerender manifest.
let revalidate = SharedRevalidateTimings.timings.get(route)
if (typeof revalidate !== 'undefined') return revalidate
revalidate = this.prerenderManifest.routes[route]?.initialRevalidateSeconds
if (typeof revalidate !== 'undefined') return revalidate
revalidate = this.prerenderManifest.dynamicRoutes[route]?.fallbackRevalidate
if (typeof revalidate !== 'undefined') return revalidate
return undefined
}
/**
* Set the revalidate timings for a route.
*
* @param route the route to set the revalidate timings for
* @param revalidate the revalidate timings for the route
*/
public set(route: string, revalidate: Revalidate) {
SharedRevalidateTimings.timings.set(route, revalidate)
}
/**
* Clear the in-memory cache of revalidate timings for routes.
*/
public clear() {
SharedRevalidateTimings.timings.clear()
}
}
......@@ -22,7 +22,7 @@ import type { PagesAPIRouteModule } from './route-modules/pages-api/module'
import type { UrlWithParsedQuery } from 'url'
import type { ParsedUrlQuery } from 'querystring'
import type { ParsedUrl } from '../shared/lib/router/utils/parse-url'
import type { Revalidate, ExpireTime } from './lib/revalidate'
import type { CacheControl } from './lib/cache-control'
import type { WaitUntil } from './after/builtin-request-context'
import fs from 'fs'
......@@ -520,8 +520,7 @@ export default class NextNodeServer extends BaseServer<
type: 'html' | 'json' | 'rsc'
generateEtags: boolean
poweredByHeader: boolean
revalidate: Revalidate | undefined
expireTime: ExpireTime | undefined
cacheControl: CacheControl | undefined
}
): Promise<void> {
return sendRenderResult({
......@@ -531,8 +530,7 @@ export default class NextNodeServer extends BaseServer<
type: options.type,
generateEtags: options.generateEtags,
poweredByHeader: options.poweredByHeader,
revalidate: options.revalidate,
expireTime: options.expireTime,
cacheControl: options.cacheControl,
})
}
......@@ -959,7 +957,7 @@ export default class NextNodeServer extends BaseServer<
upstreamEtag,
},
isFallback: false,
revalidate: maxAge,
cacheControl: { revalidate: maxAge, expire: undefined },
}
},
{
......@@ -985,7 +983,7 @@ export default class NextNodeServer extends BaseServer<
paramsResult.isStatic,
cacheEntry.isMiss ? 'MISS' : cacheEntry.isStale ? 'STALE' : 'HIT',
imagesConfig,
cacheEntry.revalidate || 0,
cacheEntry.cacheControl?.revalidate || 0,
Boolean(this.renderOpts.dev)
)
return true
......
import type { OutgoingHttpHeaders, ServerResponse } from 'http'
import type { Revalidate } from './lib/revalidate'
import type { CacheControl } from './lib/cache-control'
import type { FetchMetrics } from './base-http'
import {
......@@ -16,7 +16,7 @@ type ContentTypeOption = string | undefined
export type AppPageRenderResultMetadata = {
flightData?: Buffer
revalidate?: Revalidate
cacheControl?: CacheControl
staticBailoutInfo?: {
stack?: string
description?: string
......@@ -45,7 +45,7 @@ export type AppPageRenderResultMetadata = {
export type PagesRenderResultMetadata = {
pageData?: any
revalidate?: Revalidate
cacheControl?: CacheControl
assetQueryString?: string
isNotFound?: boolean
isRedirect?: boolean
......
......@@ -36,7 +36,7 @@ import type { NextFontManifest } from '../build/webpack/plugins/next-font-manife
import type { PagesModule } from './route-modules/pages/module'
import type { ComponentsEnhancer } from '../shared/lib/utils'
import type { NextParsedUrlQuery } from './request-meta'
import type { Revalidate, ExpireTime } from './lib/revalidate'
import type { Revalidate } from './lib/cache-control'
import type { COMPILER_NAMES } from '../shared/lib/constants'
import React, { type JSX } from 'react'
......@@ -100,7 +100,7 @@ import {
import { getTracer } from './lib/trace/tracer'
import { RenderSpan } from './lib/trace/constants'
import { ReflectAdapter } from './web/spec-extension/adapters/reflect'
import { formatRevalidate } from './lib/revalidate'
import { getCacheControlHeader } from './lib/cache-control'
import { getErrorSource } from '../shared/lib/error-source'
import type { DeepReadonly } from '../shared/lib/deep-readonly'
import type { PagesDevOverlayType } from '../client/components/react-dev-overlay/pages/pages-dev-overlay'
......@@ -283,7 +283,7 @@ export type RenderOptsPartial = {
isServerAction?: boolean
isExperimentalCompile?: boolean
isPrefetch?: boolean
expireTime?: ExpireTime
expireTime?: number
experimental: {
clientTraceMetadata?: string[]
}
......@@ -557,10 +557,7 @@ export async function renderToHTMLImpl(
if (isAutoExport && !dev && isExperimentalCompile) {
res.setHeader(
'Cache-Control',
formatRevalidate({
revalidate: false,
expireTime,
})
getCacheControlHeader({ revalidate: false, expire: expireTime })
)
isAutoExport = false
}
......@@ -1052,8 +1049,8 @@ export async function renderToHTMLImpl(
'props' in data ? data.props : undefined
)
// pass up revalidate and props for export
metadata.revalidate = revalidate
// pass up cache control and props for export
metadata.cacheControl = { revalidate, expire: undefined }
metadata.pageData = props
// this must come after revalidate is added to renderResultMeta
......@@ -1126,7 +1123,7 @@ export async function renderToHTMLImpl(
})
)
canAccessRes = false
metadata.revalidate = 0
metadata.cacheControl = { revalidate: 0, expire: undefined }
} catch (serverSidePropsError: any) {
// remove not found error code to prevent triggering legacy
// 404 rendering
......
......@@ -142,9 +142,9 @@ export default class ResponseCache implements ResponseCacheBase {
resolved = true
}
// We want to persist the result only if it has a revalidate value
// We want to persist the result only if it has a cache control value
// defined.
if (typeof resolveValue.revalidate !== 'undefined') {
if (resolveValue.cacheControl) {
if (this.minimalMode) {
this.previousCacheItem = {
key: cacheKey,
......@@ -153,7 +153,7 @@ export default class ResponseCache implements ResponseCacheBase {
}
} else {
await incrementalCache.set(key, resolveValue.value, {
revalidate: resolveValue.revalidate,
cacheControl: resolveValue.cacheControl,
isRoutePPREnabled,
isFallback,
})
......@@ -162,14 +162,24 @@ export default class ResponseCache implements ResponseCacheBase {
return resolveValue
} catch (err) {
// When a getStaticProps path is erroring we automatically re-set the
// existing cache under a new expiration to prevent non-stop retrying.
if (cachedResponse) {
// When a path is erroring we automatically re-set the existing cache
// with new revalidate and expire times to prevent non-stop retrying.
if (cachedResponse?.cacheControl) {
const newRevalidate = Math.min(
Math.max(cachedResponse.cacheControl.revalidate || 3, 3),
30
)
const newExpire =
cachedResponse.cacheControl.expire === undefined
? undefined
: Math.max(
newRevalidate + 3,
cachedResponse.cacheControl.expire
)
await incrementalCache.set(key, cachedResponse.value, {
revalidate: Math.min(
Math.max(cachedResponse.revalidate || 3, 3),
30
),
cacheControl: { revalidate: newRevalidate, expire: newExpire },
isRoutePPREnabled,
isFallback,
})
......
import type { OutgoingHttpHeaders } from 'http'
import type RenderResult from '../render-result'
import type { Revalidate } from '../lib/revalidate'
import type { CacheControl, Revalidate } from '../lib/cache-control'
import type { RouteKind } from '../route-kind'
export interface ResponseCacheBase {
......@@ -132,7 +132,7 @@ export interface IncrementalCachedPageValue {
}
export interface IncrementalResponseCacheEntry {
revalidate?: Revalidate
cacheControl?: CacheControl
/**
* timestamp in milliseconds to revalidate after
*/
......@@ -174,7 +174,7 @@ export type ResponseCacheValue =
| CachedRouteValue
export type ResponseCacheEntry = {
revalidate?: Revalidate
cacheControl?: CacheControl
value: ResponseCacheValue | null
isStale?: boolean | -1
isMiss?: boolean
......@@ -210,6 +210,7 @@ export interface GetIncrementalFetchCacheContext {
export interface GetIncrementalResponseCacheContext {
kind: Exclude<IncrementalCacheKind, IncrementalCacheKind.FETCH>
/**
* True if the route is enabled for PPR.
*/
......@@ -230,7 +231,8 @@ export interface SetIncrementalFetchCacheContext {
export interface SetIncrementalResponseCacheContext {
fetchCache?: false
revalidate?: Revalidate
cacheControl?: CacheControl
/**
* True if the route is enabled for PPR.
*/
......
......@@ -46,7 +46,7 @@ export async function toResponseCacheEntry(
return {
isMiss: response.isMiss,
isStale: response.isStale,
revalidate: response.revalidate,
cacheControl: response.cacheControl,
isFallback: response.isFallback,
value:
response.value?.kind === CachedRouteKind.PAGES
......
......@@ -98,7 +98,7 @@ export default class WebResponseCache {
resolve(resolveValue)
}
if (key && cacheEntry && typeof cacheEntry.revalidate !== 'undefined') {
if (key && cacheEntry && cacheEntry.cacheControl) {
this.previousCacheItem = {
key: pendingResponseKey || key,
entry: cacheEntry,
......
import type { IncomingMessage, ServerResponse } from 'http'
import type RenderResult from './render-result'
import type { Revalidate, ExpireTime } from './lib/revalidate'
import type { CacheControl } from './lib/cache-control'
import { isResSent } from '../shared/lib/utils'
import { generateETag } from './lib/etag'
import fresh from 'next/dist/compiled/fresh'
import { formatRevalidate } from './lib/revalidate'
import { getCacheControlHeader } from './lib/cache-control'
import { RSC_CONTENT_TYPE_HEADER } from '../client/components/app-router-headers'
export function sendEtagResponse(
......@@ -39,8 +39,7 @@ export async function sendRenderResult({
type,
generateEtags,
poweredByHeader,
revalidate,
expireTime,
cacheControl,
}: {
req: IncomingMessage
res: ServerResponse
......@@ -48,8 +47,7 @@ export async function sendRenderResult({
type: 'html' | 'json' | 'rsc'
generateEtags: boolean
poweredByHeader: boolean
revalidate: Revalidate | undefined
expireTime: ExpireTime | undefined
cacheControl: CacheControl | undefined
}): Promise<void> {
if (isResSent(res)) {
return
......@@ -61,15 +59,10 @@ export async function sendRenderResult({
// If cache control is already set on the response we don't
// override it to allow users to customize it via next.config
if (typeof revalidate !== 'undefined' && !res.getHeader('Cache-Control')) {
res.setHeader(
'Cache-Control',
formatRevalidate({
revalidate,
expireTime,
})
)
if (cacheControl && !res.getHeader('Cache-Control')) {
res.setHeader('Cache-Control', getCacheControlHeader(cacheControl))
}
const payload = result.isDynamic ? null : result.toUnchunkedString()
if (generateEtags && payload !== null) {
......
......@@ -53,8 +53,8 @@ function validateCacheLife(profile: CacheLife) {
if (profile.expire !== undefined) {
if ((profile.expire as any) === false) {
throw new Error(
'Pass `Infinity` instead of `false` if you want to cache on the client forever ' +
'without checking with the server.'
'Pass `Infinity` instead of `false` if you want to cache on the server forever ' +
'without checking with the origin.'
)
} else if (typeof profile.expire !== 'number') {
throw new Error('The expire option must be a number of seconds.')
......@@ -65,7 +65,7 @@ function validateCacheLife(profile: CacheLife) {
if (profile.revalidate > profile.expire) {
throw new Error(
'If providing both the revalidate and expire options, ' +
'the expire option must be greater than the revalidate option.' +
'the expire option must be greater than the revalidate option. ' +
'The expire option indicates how many seconds from the start ' +
'until it can no longer be used.'
)
......@@ -76,7 +76,7 @@ function validateCacheLife(profile: CacheLife) {
if (profile.stale > profile.expire) {
throw new Error(
'If providing both the stale and expire options, ' +
'the expire option must be greater than the stale option.' +
'the expire option must be greater than the stale option. ' +
'The expire option indicates how many seconds from the start ' +
'until it can no longer be used.'
)
......
......@@ -10,7 +10,7 @@ import type {
Options,
RouteHandler,
} from './base-server'
import type { Revalidate, ExpireTime } from './lib/revalidate'
import type { CacheControl } from './lib/cache-control'
import { byteLength } from './api-utils/web'
import BaseServer, { NoFallbackError } from './base-server'
......@@ -261,8 +261,7 @@ export default class NextWebServer extends BaseServer<
type: 'html' | 'json'
generateEtags: boolean
poweredByHeader: boolean
revalidate: Revalidate | undefined
expireTime: ExpireTime | undefined
cacheControl: CacheControl | undefined
}
): Promise<void> {
res.setHeader('X-Edge-Runtime', '1')
......
......@@ -1018,6 +1018,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 1,
"srcRoute": "/articles/[slug]",
},
......@@ -1042,6 +1043,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 10,
"srcRoute": "/blog/[author]",
},
......@@ -1090,6 +1092,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 10,
"srcRoute": "/blog/[author]",
},
......@@ -1162,6 +1165,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 10,
"srcRoute": "/blog/[author]",
},
......@@ -1234,6 +1238,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/force-cache",
},
......@@ -1330,6 +1335,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/gen-params-dynamic-revalidate/[slug]",
},
......@@ -1426,6 +1432,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/isr-error-handling",
},
......@@ -1834,6 +1841,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/prerendered-not-found/segment-revalidate",
},
......@@ -1858,6 +1866,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialHeaders": {
"content-type": "application/json",
"x-next-cache-tags": "_N_T_/layout,_N_T_/route-handler/layout,_N_T_/route-handler/no-store-force-static/layout,_N_T_/route-handler/no-store-force-static/route,_N_T_/route-handler/no-store-force-static",
......@@ -1886,6 +1895,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialHeaders": {
"content-type": "application/json",
"x-next-cache-tags": "_N_T_/layout,_N_T_/route-handler/layout,_N_T_/route-handler/revalidate-360-isr/layout,_N_T_/route-handler/revalidate-360-isr/route,_N_T_/route-handler/revalidate-360-isr,thankyounext",
......@@ -2014,6 +2024,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 50,
"srcRoute": "/strip-w3c-trace-context-headers",
},
......@@ -2086,6 +2097,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/variable-config-revalidate/revalidate-3",
},
......@@ -2110,6 +2122,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/variable-revalidate-stable/revalidate-3",
},
......@@ -2134,6 +2147,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 10,
"srcRoute": "/variable-revalidate/authorization",
},
......@@ -2158,6 +2172,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/variable-revalidate/cookie",
},
......@@ -2182,6 +2197,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/variable-revalidate/encoding",
},
......@@ -2206,6 +2222,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 10,
"srcRoute": "/variable-revalidate/headers-instance",
},
......@@ -2230,6 +2247,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 10,
"srcRoute": "/variable-revalidate/post-method",
},
......@@ -2254,6 +2272,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 3,
"srcRoute": "/variable-revalidate/revalidate-3",
},
......@@ -2278,6 +2297,7 @@ describe('app-dir static/dynamic handling', () => {
"value": "multipart/form-data;.*",
},
],
"initialExpireSeconds": 31536000,
"initialRevalidateSeconds": 10,
"srcRoute": "/variable-revalidate/revalidate-360-isr",
},
......
......@@ -33,7 +33,7 @@ describe('custom-cache-control', () => {
expect(res.headers.get('cache-control')).toBe(
isNextDev
? 'no-store, must-revalidate'
: 's-maxage=120, stale-while-revalidate'
: 's-maxage=120, stale-while-revalidate=31535880'
)
}
)
......@@ -71,7 +71,7 @@ describe('custom-cache-control', () => {
expect(res.headers.get('cache-control')).toBe(
isNextDev
? 'no-store, must-revalidate'
: 's-maxage=120, stale-while-revalidate'
: 's-maxage=120, stale-while-revalidate=31535880'
)
})
......
......@@ -132,7 +132,9 @@ describe('ppr-full', () => {
expect(cacheControl).toEqual('no-store, must-revalidate')
} else if (dynamic === false || dynamic === 'force-static') {
expect(cacheControl).toEqual(
`s-maxage=${revalidate || '31536000'}, stale-while-revalidate`
revalidate === undefined
? `s-maxage=31536000`
: `s-maxage=${revalidate}, stale-while-revalidate=${31536000 - revalidate}`
)
} else {
expect(cacheControl).toEqual(
......@@ -596,7 +598,7 @@ describe('ppr-full', () => {
if (isNextStart) {
expect(res.headers.get('cache-control')).toEqual(
's-maxage=31536000, stale-while-revalidate'
's-maxage=31536000'
)
}
......@@ -665,7 +667,9 @@ describe('ppr-full', () => {
)
} else {
expect(res.headers.get('cache-control')).toEqual(
`s-maxage=${revalidate || '31536000'}, stale-while-revalidate`
revalidate === undefined
? `s-maxage=31536000`
: `s-maxage=${revalidate}, stale-while-revalidate=${31536000 - revalidate}`
)
}
......
import { unstable_cacheLife } from 'next/cache'
async function getCachedRandom(n: number) {
'use cache'
unstable_cacheLife('weeks')
return String(Math.ceil(Math.random() * n))
}
......@@ -11,5 +14,7 @@ export async function generateStaticParams() {
}
export default async function Page() {
return 'hit'
const value = getCachedRandom(1)
return <p>{value}</p>
}
......@@ -9,6 +9,7 @@ const nextConfig = {
frequent: {
stale: 19,
revalidate: 100,
expire: 250,
},
},
cacheHandlers: {
......
......@@ -7,6 +7,7 @@ import {
createRenderResumeDataCache,
RenderResumeDataCache,
} from 'next/dist/server/resume-data-cache/resume-data-cache'
import { PrerenderManifest } from 'next/dist/build'
const GENERIC_RSC_ERROR =
'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.'
......@@ -370,21 +371,29 @@ describe('use-cache', () => {
])
})
it('should match the expected revalidate config on the prerender manifest', async () => {
const prerenderManifest = JSON.parse(
it('should match the expected revalidate and expire configs on the prerender manifest', async () => {
const { version, routes, dynamicRoutes } = JSON.parse(
await next.readFile('.next/prerender-manifest.json')
)
) as PrerenderManifest
expect(version).toBe(4)
// custom cache life profile "frequent"
expect(routes['/cache-life'].initialRevalidateSeconds).toBe(100)
expect(routes['/cache-life'].initialExpireSeconds).toBe(250)
expect(prerenderManifest.version).toBe(4)
expect(
prerenderManifest.routes['/cache-life'].initialRevalidateSeconds
).toBe(100)
// default expireTime
expect(routes['/cache-fetch'].initialExpireSeconds).toBe(31536000)
// The revalidate config from the fetch call should lower the revalidate
// config for the page.
expect(
prerenderManifest.routes['/cache-tag'].initialRevalidateSeconds
).toBe(42)
expect(routes['/cache-tag'].initialRevalidateSeconds).toBe(42)
if (process.env.__NEXT_EXPERIMENTAL_PPR === 'true') {
// cache life profile "weeks"
expect(dynamicRoutes['/[id]'].fallbackRevalidate).toBe(604800)
expect(dynamicRoutes['/[id]'].fallbackExpire).toBe(2592000)
}
})
it('should match the expected stale config in the page header', async () => {
......@@ -394,6 +403,23 @@ describe('use-cache', () => {
expect(meta.headers['x-nextjs-stale-time']).toBe('19')
})
it('should send an SWR cache-control header based on the revalidate and expire values', async () => {
let response = await next.fetch('/cache-life')
expect(response.headers.get('cache-control')).toBe(
// revalidate is set to 100, expire is set to 250 => SWR 150
's-maxage=100, stale-while-revalidate=150'
)
response = await next.fetch('/cache-fetch')
expect(response.headers.get('cache-control')).toBe(
// revalidate is set to 900, expire is one year (31536000, default
// expireTime) => SWR 31535100
's-maxage=900, stale-while-revalidate=31535100'
)
})
it('should propagate unstable_cache tags correctly', async () => {
const meta = JSON.parse(
await next.readFile('.next/server/app/cache-tag.meta')
......
......@@ -106,84 +106,98 @@ describe('Prerender', () => {
'/': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/index.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 2,
srcRoute: null,
},
'/blog/[post3]': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blog/[post3].json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]',
},
'/blog/post-1': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blog/post-1.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]',
},
'/blog/post-2': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blog/post-2.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]',
},
'/blog/post-4': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blog/post-4.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]',
},
'/blog/post-1/comment-1': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blog/post-1/comment-1.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 2,
srcRoute: '/blog/[post]/[comment]',
},
'/blog/post-2/comment-2': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blog/post-2/comment-2.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 2,
srcRoute: '/blog/[post]/[comment]',
},
'/blog/post.1': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blog/post.1.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 10,
srcRoute: '/blog/[post]',
},
'/catchall-explicit/another/value': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall-explicit/another/value.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall-explicit/[...slug]',
},
'/catchall-explicit/first': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall-explicit/first.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall-explicit/[...slug]',
},
'/catchall-explicit/hello/another': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall-explicit/hello/another.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall-explicit/[...slug]',
},
'/catchall-explicit/second': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall-explicit/second.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall-explicit/[...slug]',
},
'/catchall-explicit/[first]/[second]': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall-explicit/[first]/[second].json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall-explicit/[...slug]',
},
'/catchall-explicit/[third]/[fourth]': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall-explicit/[third]/[fourth].json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall-explicit/[...slug]',
},
......@@ -208,6 +222,7 @@ describe('Prerender', () => {
'/another': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/another.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: null,
},
......@@ -232,12 +247,14 @@ describe('Prerender', () => {
'/blocking-fallback-some/a': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blocking-fallback-some/a.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/blocking-fallback-some/[slug]',
},
'/blocking-fallback-some/b': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blocking-fallback-some/b.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/blocking-fallback-some/[slug]',
},
......@@ -250,12 +267,14 @@ describe('Prerender', () => {
'/blocking-fallback/test-errors-1': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blocking-fallback/test-errors-1.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/blocking-fallback/[slug]',
},
'/blog': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/blog.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 10,
srcRoute: null,
},
......@@ -316,24 +335,28 @@ describe('Prerender', () => {
'/catchall/another/value': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall/another/value.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall/[...slug]',
},
'/catchall/first': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall/first.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall/[...slug]',
},
'/catchall/second': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall/second.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall/[...slug]',
},
'/catchall/hello/another': {
allowHeader,
dataRoute: `/_next/data/${next.buildId}/catchall/hello/another.json`,
initialExpireSeconds: 31536000,
initialRevalidateSeconds: 1,
srcRoute: '/catchall/[...slug]',
},
......@@ -650,7 +673,7 @@ describe('Prerender', () => {
expect(initialRes.headers.get('cache-control')).toBe(
isDeploy
? 'public, max-age=0, must-revalidate'
: 's-maxage=2, stale-while-revalidate'
: 's-maxage=2, stale-while-revalidate=31535998'
)
})
}
......@@ -1301,9 +1324,7 @@ describe('Prerender', () => {
it('should use correct caching headers for a no-revalidate page', async () => {
const initialRes = await fetchViaHTTP(next.url, '/something')
expect(initialRes.headers.get('cache-control')).toBe(
isDeploy
? 'public, max-age=0, must-revalidate'
: 's-maxage=31536000, stale-while-revalidate'
isDeploy ? 'public, max-age=0, must-revalidate' : 's-maxage=31536000'
)
const initialHtml = await initialRes.text()
expect(initialHtml).toMatch(/hello.*?world/)
......
......@@ -82,7 +82,7 @@ const runTests = () => {
let $ = cheerio.load(await res.text())
expect(res.headers.get('cache-control')).toBe(
`s-maxage=1, stale-while-revalidate`
`s-maxage=1, stale-while-revalidate=31535999`
)
expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true)
......@@ -92,7 +92,7 @@ const runTests = () => {
$ = cheerio.load(await res.text())
expect(res.headers.get('cache-control')).toBe(
`s-maxage=1, stale-while-revalidate`
`s-maxage=1, stale-while-revalidate=31535999`
)
expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true)
......@@ -103,7 +103,7 @@ const runTests = () => {
const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
expect(props.found).toBe(true)
......@@ -116,7 +116,7 @@ const runTests = () => {
const props2 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
expect(props2.found).toBe(true)
......@@ -129,7 +129,7 @@ const runTests = () => {
const props3 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
expect(props3.found).toBe(true)
......@@ -148,7 +148,7 @@ const runTests = () => {
let $ = cheerio.load(await res.text())
expect(res.headers.get('cache-control')).toBe(
`s-maxage=1, stale-while-revalidate`
`s-maxage=1, stale-while-revalidate=31535999`
)
expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true)
......@@ -159,7 +159,7 @@ const runTests = () => {
const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
expect(props.found).toBe(true)
......@@ -172,7 +172,7 @@ const runTests = () => {
const props2 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
expect(props2.found).toBe(true)
......@@ -185,7 +185,7 @@ const runTests = () => {
const props3 = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
expect(props3.found).toBe(true)
......
......@@ -300,6 +300,13 @@ describe('Prerender prefetch', () => {
pages: new FileRef(join(__dirname, 'app/pages')),
},
dependencies: {},
env: {
// Simulate that a CDN has consumed the SWR cache-control header,
// otherwise the browser will cache responses and which messes with
// the expectations in this test.
// See https://github.com/vercel/next.js/pull/70674 for context.
NEXT_PRIVATE_CDN_CONSUMED_SWR_CACHE_CONTROL: '1',
},
})
})
afterAll(() => next.destroy())
......@@ -319,6 +326,13 @@ describe('Prerender prefetch', () => {
},
},
dependencies: {},
env: {
// Simulate that a CDN has consumed the SWR cache-control header,
// otherwise the browser will cache responses and which messes with
// the expectations in this test.
// See https://github.com/vercel/next.js/pull/70674 for context.
NEXT_PRIVATE_CDN_CONSUMED_SWR_CACHE_CONTROL: '1',
},
})
})
afterAll(() => next.destroy())
......
......@@ -5,7 +5,7 @@ import next from 'next' // force a warning during `next build`
import { useRouter } from 'next/router'
export async function getServerSideProps({ res }) {
res.setHeader('cache-control', 's-maxage=1, stale-while-revalidate')
res.setHeader('cache-control', 's-maxage=1, stale-while-revalidate=31535999')
const data = await fs.promises.readFile(
path.join(process.cwd(), 'data.txt'),
......
......@@ -114,9 +114,7 @@ describe('required server files app router', () => {
},
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=31536000, stale-while-revalidate'
)
expect(res.headers.get('cache-control')).toBe('s-maxage=31536000')
})
it('should handle optional catchall', async () => {
......@@ -176,7 +174,7 @@ describe('required server files app router', () => {
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=3600, stale-while-revalidate'
's-maxage=3600, stale-while-revalidate=31532400'
)
})
......
......@@ -178,7 +178,7 @@ describe('required server files i18n', () => {
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
await waitFor(2000)
......@@ -189,7 +189,7 @@ describe('required server files i18n', () => {
})
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
})
......@@ -201,7 +201,7 @@ describe('required server files i18n', () => {
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
await next.patchFile('standalone/data.txt', 'hide')
......@@ -213,7 +213,7 @@ describe('required server files i18n', () => {
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
})
......
......@@ -218,13 +218,13 @@ describe('required server files', () => {
case: 'redirect no revalidate',
path: '/optional-ssg/redirect-1',
dest: '/somewhere',
cacheControl: 's-maxage=31536000, stale-while-revalidate',
cacheControl: 's-maxage=31536000',
},
{
case: 'redirect with revalidate',
path: '/optional-ssg/redirect-2',
dest: '/somewhere-else',
cacheControl: 's-maxage=5, stale-while-revalidate',
cacheControl: 's-maxage=5, stale-while-revalidate=31535995',
},
])(
`should have correct cache-control for $case`,
......@@ -295,13 +295,13 @@ describe('required server files', () => {
case: 'notFound no revalidate',
path: '/optional-ssg/not-found-1',
dest: '/somewhere',
cacheControl: 's-maxage=31536000, stale-while-revalidate',
cacheControl: 's-maxage=31536000',
},
{
case: 'notFound with revalidate',
path: '/optional-ssg/not-found-2',
dest: '/somewhere-else',
cacheControl: 's-maxage=5, stale-while-revalidate',
cacheControl: 's-maxage=5, stale-while-revalidate=31535995',
},
])(
`should have correct cache-control for $case`,
......@@ -327,9 +327,7 @@ describe('required server files', () => {
it('should have the correct cache-control for props with no revalidate', async () => {
const res = await fetchViaHTTP(appPort, '/optional-ssg/props-no-revalidate')
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=31536000, stale-while-revalidate'
)
expect(res.headers.get('cache-control')).toBe('s-maxage=31536000')
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#props').text()).params).toEqual({
rest: ['props-no-revalidate'],
......@@ -341,9 +339,7 @@ describe('required server files', () => {
undefined
)
expect(dataRes.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=31536000, stale-while-revalidate'
)
expect(res.headers.get('cache-control')).toBe('s-maxage=31536000')
expect((await dataRes.json()).pageProps.params).toEqual({
rest: ['props-no-revalidate'],
})
......@@ -525,7 +521,7 @@ describe('required server files', () => {
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
await waitFor(2000)
......@@ -536,7 +532,7 @@ describe('required server files', () => {
})
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
})
......@@ -548,7 +544,7 @@ describe('required server files', () => {
})
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
await next.patchFile('standalone/data.txt', 'hide')
......@@ -560,7 +556,7 @@ describe('required server files', () => {
expect(res2.status).toBe(404)
expect(res2.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate'
's-maxage=1, stale-while-revalidate=31535999'
)
})
......
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