Unverified Commit d35c7366 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix: staleTimes.static should consistently enforce a 30s minimum (#85479)

When the tree response sends back a static staleTime, we had handling to
ensure that it was at minimum 30s, because anything less would mean we'd
be discarding static cache entries more frequently than necessary.
Currently a value of 0 would result in a prefetch ping loop, because the
cache value would be immediately invalidated.

This updates our config validation to warn about values lower than 30s
and ensures the `getStaleTimeMs` function applies to the value read from
env, too.
parent 2757ae5f
...@@ -15,6 +15,7 @@ import { ...@@ -15,6 +15,7 @@ import {
navigate as navigateUsingSegmentCache, navigate as navigateUsingSegmentCache,
NavigationResultTag, NavigationResultTag,
type NavigationResult, type NavigationResult,
getStaleTimeMs,
} from '../../segment-cache' } from '../../segment-cache'
// These values are set by `define-env-plugin` (based on `nextConfig.experimental.staleTimes`) // These values are set by `define-env-plugin` (based on `nextConfig.experimental.staleTimes`)
...@@ -22,8 +23,9 @@ import { ...@@ -22,8 +23,9 @@ import {
export const DYNAMIC_STALETIME_MS = export const DYNAMIC_STALETIME_MS =
Number(process.env.__NEXT_CLIENT_ROUTER_DYNAMIC_STALETIME) * 1000 Number(process.env.__NEXT_CLIENT_ROUTER_DYNAMIC_STALETIME) * 1000
export const STATIC_STALETIME_MS = export const STATIC_STALETIME_MS = getStaleTimeMs(
Number(process.env.__NEXT_CLIENT_ROUTER_STATIC_STALETIME) * 1000 Number(process.env.__NEXT_CLIENT_ROUTER_STATIC_STALETIME)
)
export function handleExternalUrl( export function handleExternalUrl(
state: ReadonlyReducerState, state: ReadonlyReducerState,
......
...@@ -93,7 +93,7 @@ import { ...@@ -93,7 +93,7 @@ import {
DOC_PREFETCH_RANGE_HEADER_VALUE, DOC_PREFETCH_RANGE_HEADER_VALUE,
doesExportedHtmlMatchBuildId, doesExportedHtmlMatchBuildId,
} from '../../../shared/lib/segment-cache/output-export-prefetch-encoding' } from '../../../shared/lib/segment-cache/output-export-prefetch-encoding'
import { FetchStrategy } from '../segment-cache' import { FetchStrategy, getStaleTimeMs } from '../segment-cache'
import { createPromiseWithResolvers } from '../../../shared/lib/promise-with-resolvers' import { createPromiseWithResolvers } from '../../../shared/lib/promise-with-resolvers'
// A note on async/await when working in the prefetch cache: // A note on async/await when working in the prefetch cache:
...@@ -267,14 +267,6 @@ const isOutputExportMode = ...@@ -267,14 +267,6 @@ const isOutputExportMode =
process.env.NODE_ENV === 'production' && process.env.NODE_ENV === 'production' &&
process.env.__NEXT_CONFIG_OUTPUT === 'export' process.env.__NEXT_CONFIG_OUTPUT === 'export'
/**
* Ensures a minimum stale time of 30s to avoid issues where the server sends a too
* short-lived stale time, which would prevent anything from being prefetched.
*/
function getStaleTimeMs(staleTimeSeconds: number): number {
return Math.max(staleTimeSeconds, 30) * 1000
}
// Route cache entries vary on multiple keys: the href and the Next-Url. Each of // Route cache entries vary on multiple keys: the href and the Next-Url. Each of
// these parts needs to be included in the internal cache key. Rather than // these parts needs to be included in the internal cache key. Rather than
// concatenate the keys into a single key, we use a multi-level map, where the // concatenate the keys into a single key, we use a multi-level map, where the
......
...@@ -162,3 +162,11 @@ export type PrefetchTaskFetchStrategy = ...@@ -162,3 +162,11 @@ export type PrefetchTaskFetchStrategy =
| FetchStrategy.PPR | FetchStrategy.PPR
| FetchStrategy.PPRRuntime | FetchStrategy.PPRRuntime
| FetchStrategy.Full | FetchStrategy.Full
/**
* Ensures a minimum stale time of 30s to avoid issues where the server sends a too
* short-lived stale time, which would prevent anything from being prefetched.
*/
export function getStaleTimeMs(staleTimeSeconds: number): number {
return Math.max(staleTimeSeconds, 30) * 1000
}
...@@ -172,7 +172,7 @@ export const experimentalSchema = { ...@@ -172,7 +172,7 @@ export const experimentalSchema = {
staleTimes: z staleTimes: z
.object({ .object({
dynamic: z.number().optional(), dynamic: z.number().optional(),
static: z.number().optional(), static: z.number().gte(30).optional(),
}) })
.optional(), .optional(),
cacheLife: z cacheLife: z
......
...@@ -299,6 +299,7 @@ export interface ExperimentalConfig { ...@@ -299,6 +299,7 @@ export interface ExperimentalConfig {
*/ */
staleTimes?: { staleTimes?: {
dynamic?: number dynamic?: number
/** Must be greater than or equal to 30 seconds, to ensure prefetching is not completely wasteful */
static?: number static?: number
} }
/** /**
......
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