1// Detects time/randomness primitives that destabilize `'use cache'` cache keys,2// which manifests as ISR write amplification when the cached output embeds a timestamp3// that changes per request.4//5// Triggers when a file contains the `'use cache'` directive AND uses `new Date(`,6// `Date.now(`, or `Math.random(` outside client-only hooks (useEffect / useCallback /7// useMemo). Replacing module-scope `new Date().getFullYear()` with a build-time8// `buildYear` constant, and removing dates passed as `'use cache'` function9// arguments, prevents repeated writes when the rendered output is otherwise stable.1011import { lineOf } from '../util.mjs';1213export const
15 title: "new Date() / Date.now() / Math.random() inside a 'use cache' file",
16 severity: 'high',
17 billingDimension: 'isr',
18 trafficIndependent: false,
19 description:
20 "`'use cache'` memoizes by argument identity AND prerender output. A timestamp baked into the cached output (`new Date().getFullYear()` in a footer, `Date.now()` in a payload field) forces a fresh ISR write on every regeneration even when the underlying data is unchanged. Random values have the same failure mode.",
21 fix:
22 "Replace module-scope `new Date()` with a build-time constant (`const buildYear = new Date().getFullYear()`) or move per-request timestamps into a client component inside `useEffect`. Do not pass dates as arguments to `'use cache'` functions — they invalidate the cache every call.",