Unverified Commit e3790aa9 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

More granular cache keys for use-cache entries (#95233)

Caveats:
- I had to skip `react[-dom][/*]` and
`private-next-rsc-server-reference` and `private-next-rsc-cache-wrapper`
imports in the code hash and env-var tracking. Because those all end up
pulling in app-page-turbo.runtime.prod.js which reads many env vars and
would cause constant deopting. But this should still be correct. The
code of these imports is included via the Next.js version, and no env
vars should change the semantics of any of those imports.
- Static env var reads are collected, but too dynamic accesses are
silently ignored and don't lead to deopts (same goes for env var reads
in native NAPI addons). This means that this cache reuse is not
guaranteed to be 100% guaranteed to never lead to stale caches.
- Code hashing and env var collection happens on a per-module basis. So
if you put all use-cache functions into a single file and/or together
with react components, then you will see extraneous invalidations. This
will be fixed by either generally enabling module splitting for all of
Turbopack, or by adding a special transform that does it for use-cache
functions.

Followups:
- There are some env var static analysis gaps that will be immediate
followups before broader testing. These are the various TODOs added in
https://github.com/vercel/next.js/pull/95310
- Client components invalidation is very coarse grained right now
(statically imports any client component anywhere -> deopt completely).
Followup for the future. But the current setup is correct. This would
just improve effectiveness further
- Do this in dev as well. Currently there is no NFT at all in dev (for
performance reasons)
- Module splitting for more granular tracking
- Include entropy when serializing server reference arguments for cache
key


Todo:

- [x] Use implementation code hash (includes inlined env vars): from
#94234
- [x] Include non-inlined runtime env vars: from #95310
- [x] Include client reference manifest (very coarse for now)
- [x] Include Next.js version (for wire format, etc)
- [x] This is now done for all use-cache entries now. Not just for `use
cache: remote`. Is that the intended behavior? Yes
- [ ] ~~if `NEXT_DEPLOYMENT_ID` is in the env vars. just deopt and don't
care about stringifing and hashing the env vars~~
- [x] Is cache key size a problem? Currently you can get this: (values
are always hashed)
`CustomCacheHandler::get
["80e6f6560092f0078775e7e787c1c10ecf6dea0bc4",[],["d984fbaa996274737f3b59345a300a20","16.4.0-canary.5","__NEXT_NO_MIDDLEWARE_URL_NORMALIZE=undefined","NEXT_OTEL_PERFORMANCE_PREFIX=undefined","__NEXT_PRIVATE_ORIGIN=a04f4b9d6a8f42724740a480e9a2bc67c053dc04377be831c0e2c407a1422004","NEXT_PRIVATE_RESPONSE_CACHE_TTL=undefined","NEXT_PRIVATE_RESPONSE_CACHE_MAX_SIZE=undefined","__NEXT_CACHE_COMPONENTS=b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b","__NEXT_ROUTER_BASEPATH=undefined","__NEXT_MANUAL_CLIENT_BASE_PATH=undefined","__NEXT_INSTRUMENTATION_CLIENT_ROUTER_TRANSITION_EVENTS=undefined","__NEXT_APP_NAV_FAIL_HANDLING=undefined","__NEXT_GESTURE_TRANSITION=undefined","__NEXT_USE_OFFLINE=undefined","NEXT_DEBUG_BUILD=undefined","__NEXT_VERBOSE_LOGGING=undefined...]]
[["_N_T_/layout","_N_T_/page","_N_T_/","_N_T_/index"]]`
parent 28195e9b
...@@ -426,7 +426,7 @@ impl AppProject { ...@@ -426,7 +426,7 @@ impl AppProject {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn rsc_module_context(self: Vc<Self>) -> Result<Vc<ModuleAssetContext>> { pub(crate) fn rsc_module_context(self: Vc<Self>) -> Result<Vc<ModuleAssetContext>> {
Ok(ModuleAssetContext::new( Ok(ModuleAssetContext::new(
self.get_rsc_transitions( self.get_rsc_transitions(
self.ecmascript_client_reference_transition(), self.ecmascript_client_reference_transition(),
...@@ -459,7 +459,7 @@ impl AppProject { ...@@ -459,7 +459,7 @@ impl AppProject {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn route_module_context(self: Vc<Self>) -> Result<Vc<ModuleAssetContext>> { pub(crate) async fn route_module_context(self: Vc<Self>) -> Result<Vc<ModuleAssetContext>> {
let transitions = [ let transitions = [
( (
AppProject::client_transition_name(), AppProject::client_transition_name(),
......
This diff is collapsed.
...@@ -459,13 +459,22 @@ pub struct ActionManifestEntry<'a> { ...@@ -459,13 +459,22 @@ pub struct ActionManifestEntry<'a> {
} }
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ActionManifestWorkerEntry<'a> { pub struct ActionManifestWorkerEntry<'a> {
#[serde(rename = "moduleId")]
pub module_id: ActionManifestModuleId<'a>, pub module_id: ActionManifestModuleId<'a>,
#[serde(rename = "async")] #[serde(rename = "async")]
pub is_async: bool, pub is_async: bool,
#[serde(rename = "codeHash")] #[serde(skip_serializing_if = "Option::is_none")]
pub code_hash: Option<&'a str>, pub durability: Option<ActionManifestWorkerEntryDurability<'a>>,
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ActionManifestWorkerEntryDurability<'a> {
pub code_hash: &'a str,
pub runtime_env_vars: &'a [RcStr],
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub references_client_component: bool,
} }
#[derive(Serialize, Debug, Clone)] #[derive(Serialize, Debug, Clone)]
......
...@@ -72,6 +72,11 @@ type Actions = { ...@@ -72,6 +72,11 @@ type Actions = {
[name: string]: { [name: string]: {
moduleId: string | number moduleId: string | number
async: boolean async: boolean
durability?: {
codeHash: string
runtimeEnvVars: string[]
referencesClientComponent?: boolean
}
} }
} }
// Record which layer the action is in (rsc or sc_action), in the specific entry // Record which layer the action is in (rsc or sc_action), in the specific entry
......
...@@ -261,7 +261,17 @@ function createServerModuleMap(): ServerModuleMap { ...@@ -261,7 +261,17 @@ function createServerModuleMap(): ServerModuleMap {
const workStore = workAsyncStorage.getStore() const workStore = workAsyncStorage.getStore()
let workerEntry: { moduleId: string | number; async: boolean } | undefined let workerEntry:
| {
moduleId: string | number
async: boolean
durability?: {
codeHash: string
runtimeEnvVars: readonly string[]
referencesClientComponent?: boolean
}
}
| undefined
if (workStore) { if (workStore) {
workerEntry = workers[normalizeWorkerPageName(workStore.page)] workerEntry = workers[normalizeWorkerPageName(workStore.page)]
...@@ -280,9 +290,15 @@ function createServerModuleMap(): ServerModuleMap { ...@@ -280,9 +290,15 @@ function createServerModuleMap(): ServerModuleMap {
throw getActionNotFoundError(id) throw getActionNotFoundError(id)
} }
const { moduleId, async } = workerEntry const { moduleId, async, durability } = workerEntry
return { id: moduleId, name: id, chunks: [], async } return {
id: moduleId,
name: id,
chunks: [],
async,
durability,
}
}, },
}) })
} }
...@@ -291,7 +307,7 @@ function createServerModuleMap(): ServerModuleMap { ...@@ -291,7 +307,7 @@ function createServerModuleMap(): ServerModuleMap {
* The flight entry loader keys actions by bundlePath. bundlePath corresponds * The flight entry loader keys actions by bundlePath. bundlePath corresponds
* with the relative path (including 'app') to the page entrypoint. * with the relative path (including 'app') to the page entrypoint.
*/ */
function normalizeWorkerPageName(pageName: string) { export function normalizeWorkerPageName(pageName: string) {
if (pathHasPrefix(pageName, 'app')) { if (pathHasPrefix(pageName, 'app')) {
return pageName return pageName
} }
......
...@@ -148,7 +148,7 @@ export async function probeUseCache(msg: ProbeMessage): Promise<boolean> { ...@@ -148,7 +148,7 @@ export async function probeUseCache(msg: ProbeMessage): Promise<boolean> {
) )
} }
const args = decoded[2] const args = decoded[1]
const workStore: WorkStore = buildProbeWorkStore(msg) const workStore: WorkStore = buildProbeWorkStore(msg)
// The outer store is `'request'`-typed and built from the forwarded // The outer store is `'request'`-typed and built from the forwarded
......
...@@ -51,7 +51,9 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight ...@@ -51,7 +51,9 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight
import { import {
getClientReferenceManifest, getClientReferenceManifest,
getServerActionsManifest,
getServerModuleMap, getServerModuleMap,
normalizeWorkerPageName,
} from '../app-render/manifests-singleton' } from '../app-render/manifests-singleton'
import type { CacheEntry } from '../lib/cache-handlers/types' import type { CacheEntry } from '../lib/cache-handlers/types'
import type { CacheSignal } from '../app-render/cache-signal' import type { CacheSignal } from '../app-render/cache-signal'
...@@ -153,9 +155,13 @@ interface PublicCacheContext { ...@@ -153,9 +155,13 @@ interface PublicCacheContext {
type CacheContext = PrivateCacheContext | PublicCacheContext type CacheContext = PrivateCacheContext | PublicCacheContext
export type CacheKeyParts = const nextVersion = process.env.__NEXT_VERSION as string
| [buildId: string, id: string, args: unknown[]]
| [buildId: string, id: string, args: unknown[], hmrRefreshHash: string] export type CacheKeyParts = [
id: string,
args: unknown[],
implementationHash: unknown,
]
interface UseCachePageInnerProps { interface UseCachePageInnerProps {
params: Promise<Params> params: Promise<Params>
...@@ -1285,7 +1291,7 @@ async function generateCacheEntryImpl( ...@@ -1285,7 +1291,7 @@ async function generateCacheEntryImpl(
const temporaryReferences = createServerTemporaryReferenceSet() const temporaryReferences = createServerTemporaryReferenceSet()
const outerWorkUnitStore = cacheContext.outerWorkUnitStore const outerWorkUnitStore = cacheContext.outerWorkUnitStore
const [, , args] = const [, args] =
typeof encodedArguments === 'string' typeof encodedArguments === 'string'
? await decodeReply<CacheKeyParts>( ? await decodeReply<CacheKeyParts>(
encodedArguments, encodedArguments,
...@@ -1982,19 +1988,6 @@ export async function cache( ...@@ -1982,19 +1988,6 @@ export async function cache(
// In case getClientReferenceManifestSingleton is implemented using AsyncLocalStorage. // In case getClientReferenceManifestSingleton is implemented using AsyncLocalStorage.
const clientReferenceManifest = getClientReferenceManifest() const clientReferenceManifest = getClientReferenceManifest()
// Because the Action ID is not yet unique per implementation of that Action we can't
// safely reuse the results across builds yet. In the meantime we add the buildId to the
// arguments as a seed to ensure they're not reused. Remove this once Action IDs hash
// the implementation.
const buildId = workStore.deploymentId || workStore.buildId
// In dev mode, when the HMR refresh hash is set, we include it in the
// cache key. This ensures that cache entries are not reused when server
// components have been edited. This is a very coarse approach. But it's
// also only a temporary solution until Action IDs are unique per
// implementation. Remove this once Action IDs hash the implementation.
const hmrRefreshHash = getHmrRefreshHash(workUnitStore)
const hangingInputAbortSignal = createHangingInputAbortSignal(workUnitStore) const hangingInputAbortSignal = createHangingInputAbortSignal(workUnitStore)
if (cacheContext.kind === 'private') { if (cacheContext.kind === 'private') {
...@@ -2155,9 +2148,11 @@ export async function cache( ...@@ -2155,9 +2148,11 @@ export async function cache(
// long as the request. In development private caches are persisted across // long as the request. In development private caches are persisted across
// requests, so `cacheHandlerKeyBase` (below) additionally scopes the handler // requests, so `cacheHandlerKeyBase` (below) additionally scopes the handler
// key by the request's cookies and headers. // key by the request's cookies and headers.
const cacheKeyParts: CacheKeyParts = hmrRefreshHash const cacheKeyParts: CacheKeyParts = [
? [buildId, id, args, hmrRefreshHash] id,
: [buildId, id, args] args,
await computeCacheKeyImplementationPart(workStore, workUnitStore, id),
]
const encodeCacheKeyParts = () => const encodeCacheKeyParts = () =>
encodeReply(cacheKeyParts, { encodeReply(cacheKeyParts, {
...@@ -3616,6 +3611,71 @@ export async function cache( ...@@ -3616,6 +3611,71 @@ export async function cache(
}) })
} }
/**
* This returns a cache key that has to cover everything that can affect the result of the cached
* function (apart from the arguments). So
* - codeHash: the code itself that generates the return value
* - Notably, this excludes the following modules. Those are included via the Next.js version
* anyway:
* - react, react-dom, private-next-rsc-server-reference, private-next-rsc-cache-wrapper
* - runtimeEnvVars: the keys and values runtime environment variables that the code reads (and are
* not inlined)
* - the version of Next.js (to account for RSC wire format changes, or use-cache-wrapper.ts
*
* In case that granular information isn't available, fall back to
* buildId/deploymentId/hmrRefreshHash, which is a correct hash but over-invalidates way too often.
*/
async function computeCacheKeyImplementationPart(
workStore: WorkStore,
workUnitStore: WorkUnitStore,
id: string
): Promise<unknown> {
let durability = workStore.durableUseCacheEntries
? getServerActionsManifest().node[id].workers?.[
normalizeWorkerPageName(workStore.page)
]?.durability
: undefined
if (
durability &&
// TODO replace this with more granular tracking: a list of all imported client components
durability.referencesClientComponent !== true
) {
// use cache is only supported in Node.js runtime. So we can use the Node.js crypto module here.
const crypto = require('crypto') as typeof import('crypto')
let runtimeEnvVarStateHash = crypto
// Hash the env var values, to not leak secrets into the cache key.
.createHash('sha256')
.update(
durability.runtimeEnvVars
.map((k) => {
// Make sure not to stringify `undefined` and `"undefined"` to the same value.
return process.env[k] != null ? `${k}=${process.env[k]}` : k
})
.join('\0') ?? ''
)
.digest('hex')
// When more accurate analysis information is available, use codeHash + runtimeEnvVars
return [durability.codeHash, nextVersion, runtimeEnvVarStateHash]
} else {
// Because the Action ID is not yet unique per implementation of that Action we can't
// safely reuse the results across builds yet. In the meantime we add the buildId to the
// arguments as a seed to ensure they're not reused. Remove this once Action IDs hash
// the implementation.
const buildId = workStore.deploymentId || workStore.buildId
// In dev mode, when the HMR refresh hash is set, we include it in the
// cache key. This ensures that cache entries are not reused when server
// components have been edited. This is a very coarse approach. But it's
// also only a temporary solution until Action IDs are unique per
// implementation. Remove this once Action IDs hash the implementation.
const hmrRefreshHash = getHmrRefreshHash(workUnitStore)
// otherwise fall back to buildId and/or the HMR hash.
return hmrRefreshHash ? [buildId, hmrRefreshHash] : [buildId]
}
}
/** /**
* Returns `true` if the `'use cache'` function is the page component itself, * Returns `true` if the `'use cache'` function is the page component itself,
* or `generateMetadata`/`generateViewport` in a page file. * or `generateMetadata`/`generateViewport` in a page file.
......
...@@ -27,15 +27,10 @@ describe('use-cache-custom-handler', () => { ...@@ -27,15 +27,10 @@ describe('use-cache-custom-handler', () => {
expect(cliOutput).toContain('ModernCustomCacheHandler::refreshTags') expect(cliOutput).toContain('ModernCustomCacheHandler::refreshTags')
// In development the cache key carries a trailing HMR refresh hash element // The implementation parts contain either the code hash and version or the
// (absent in production), so the args array may be followed by an optional // build ID, followed by an optional HMR refresh hash in development.
// quoted hash.
expect(next.cliOutput.slice(outputIndex)).toMatch( expect(next.cliOutput.slice(outputIndex)).toMatch(
/ModernCustomCacheHandler::get \["(development|[A-Za-z0-9_-]+)","([0-9a-f]{2})+",\[\](,"[^"]+")?\] \[ '_N_T_\/layout', '_N_T_\/page', '_N_T_\/', '_N_T_\/index' \]/ /ModernCustomCacheHandler::get \["([0-9a-f]{2})+",\[\],\["(development|[A-Za-z0-9_-]+)"(,"[^"]+")*\]\] \[ '_N_T_\/layout', '_N_T_\/page', '_N_T_\/', '_N_T_\/index' \]/
)
expect(next.cliOutput.slice(outputIndex)).toMatch(
/ModernCustomCacheHandler::set \["(development|[A-Za-z0-9_-]+)","([0-9a-f]{2})+",\[\](,"[^"]+")?\]/
) )
// Since no existing cache entry was retrieved, we don't need to call // Since no existing cache entry was retrieved, we don't need to call
...@@ -129,7 +124,7 @@ describe('use-cache-custom-handler', () => { ...@@ -129,7 +124,7 @@ describe('use-cache-custom-handler', () => {
if (isNextStart) { if (isNextStart) {
it('should save a short-lived cache during prerendering at buildtime', async () => { it('should save a short-lived cache during prerendering at buildtime', async () => {
expect(next.cliOutput).toMatch( expect(next.cliOutput).toMatch(
/ModernCustomCacheHandler::set \["[A-Za-z0-9_-]+","([0-9a-f]{2})+",\[{"id":"dynamic-cache"}]\]/ /ModernCustomCacheHandler::set \["([0-9a-f]{2})+",\[{"id":"dynamic-cache"}\],\["[A-Za-z0-9_-]+"(,"[^"]+")*\]\]/
) )
}) })
} }
......
...@@ -28,17 +28,17 @@ describe('use-cache-default-handler-expire-zero', () => { ...@@ -28,17 +28,17 @@ describe('use-cache-default-handler-expire-zero', () => {
// The default handler skips storing the `expire: 0` entry (it would never // The default handler skips storing the `expire: 0` entry (it would never
// be served back in production)... // be served back in production)...
expect(next.cliOutput).toMatch( expect(next.cliOutput).toMatch(
/DefaultCacheHandler: set \["[A-Za-z0-9_-]+","([0-9a-f]{2})+",\[{"id":"expire-zero"}]\] skipped dynamic entry/ /DefaultCacheHandler: set \["([0-9a-f]{2})+",\[{"id":"expire-zero"}\],\["[A-Za-z0-9_-]+"(,"[^"]+")*\]\] skipped dynamic entry/
) )
// ...and never reports it as stored. // ...and never reports it as stored.
expect(next.cliOutput).not.toMatch( expect(next.cliOutput).not.toMatch(
/DefaultCacheHandler: set \["[A-Za-z0-9_-]+","([0-9a-f]{2})+",\[{"id":"expire-zero"}]\] done/ /DefaultCacheHandler: set \["([0-9a-f]{2})+",\[{"id":"expire-zero"}\],\["[A-Za-z0-9_-]+"(,"[^"]+")*\]\] done/
) )
// The short-lived (`expire: 60`) cache is still stored. // The short-lived (`expire: 60`) cache is still stored.
expect(next.cliOutput).toMatch( expect(next.cliOutput).toMatch(
/DefaultCacheHandler: set \["[A-Za-z0-9_-]+","([0-9a-f]{2})+",\[{"id":"short-lived"}]\] done/ /DefaultCacheHandler: set \["([0-9a-f]{2})+",\[{"id":"short-lived"}\],\["[A-Za-z0-9_-]+"(,"[^"]+")*\]\] done/
) )
}) })
} else { } else {
...@@ -50,16 +50,16 @@ describe('use-cache-default-handler-expire-zero', () => { ...@@ -50,16 +50,16 @@ describe('use-cache-default-handler-expire-zero', () => {
// In development the default handler keeps `expire: 0` entries (its // In development the default handler keeps `expire: 0` entries (its
// minimum retention serves them warm across reloads), so it stores rather // minimum retention serves them warm across reloads), so it stores rather
// than skips. The dev cache key always carries a trailing HMR refresh // than skips. The implementation parts include the build ID and a
// hash element after the args array. // trailing HMR refresh hash.
await retry(async () => { await retry(async () => {
expect(next.cliOutput).toMatch( expect(next.cliOutput).toMatch(
/DefaultCacheHandler: set \["[A-Za-z0-9_-]+","([0-9a-f]{2})+",\[{"id":"expire-zero"}],"[^"]+"\] done/ /DefaultCacheHandler: set \["([0-9a-f]{2})+",\[{"id":"expire-zero"}\],\["[A-Za-z0-9_-]+"(,"[^"]+")+\]\] done/
) )
}) })
expect(next.cliOutput).not.toMatch( expect(next.cliOutput).not.toMatch(
/DefaultCacheHandler: set \["[A-Za-z0-9_-]+","([0-9a-f]{2})+",\[{"id":"expire-zero"}],"[^"]+"\] skipped/ /DefaultCacheHandler: set \["([0-9a-f]{2})+",\[{"id":"expire-zero"}\],\["[A-Za-z0-9_-]+"(,"[^"]+")+\]\] skipped/
) )
}) })
} }
......
// @ts-ignore
globalThis.blackbox = (v) => ({ DECOY: v.FOOBAR })
// @ts-ignore
const envVar = blackbox(process.env).DECOY
export async function logic() {
'use cache'
return `${envVar}`
}
import { logic } from './logic'
export default async function Page() {
const value = await logic()
return <p>{value}</p>
}
import { ClientComponent } from './client-reference' import { ClientComponent } from './client-reference'
import { preinit } from 'react-dom'
import './client-reference-css.css' import './client-reference-css.css'
export default async function Page() { export default async function Page() {
'use cache' 'use cache'
if (Date.now() < 0) {
// Try importing anything from react-dom
preinit('abc')
}
return ( return (
<p> <p>
<ClientComponent /> <ClientComponent />
......
...@@ -4,5 +4,5 @@ import { external } from 'external-dep' ...@@ -4,5 +4,5 @@ import { external } from 'external-dep'
export async function logic() { export async function logic() {
'use cache' 'use cache'
return `${foo()}:${external()}` return `${foo()}:${external()}:${process.env.BUNDLED_NON_INLINED_ENVVAR}`
} }
export function external() { export function external() {
return 'external-v1' return 'external-v1' + process.env.EXTERNAL_ENV_VAR
} }
...@@ -3,7 +3,9 @@ import { nextTestSetup, type NextInstance } from 'e2e-utils' ...@@ -3,7 +3,9 @@ import { nextTestSetup, type NextInstance } from 'e2e-utils'
async function getCodeHashes( async function getCodeHashes(
next: NextInstance, next: NextInstance,
pages?: string[] pages?: string[]
): Promise<{ id: string; page: string; codeHash?: string }[]> { ): Promise<
{ id: string; page: string; codeHash?: string; runtimeEnvVars?: string[] }[]
> {
const manifest = await next.readJSON( const manifest = await next.readJSON(
'.next/server/server-reference-manifest.json' '.next/server/server-reference-manifest.json'
) )
...@@ -12,6 +14,7 @@ async function getCodeHashes( ...@@ -12,6 +14,7 @@ async function getCodeHashes(
id: string id: string
page: string page: string
codeHash?: string codeHash?: string
runtimeEnvVars?: string[]
}[] = [] }[] = []
for (const [actionId, entry] of Object.entries<any>(manifest.node)) { for (const [actionId, entry] of Object.entries<any>(manifest.node)) {
for (const [workerKey, worker] of Object.entries<any>(entry.workers)) { for (const [workerKey, worker] of Object.entries<any>(entry.workers)) {
...@@ -19,7 +22,8 @@ async function getCodeHashes( ...@@ -19,7 +22,8 @@ async function getCodeHashes(
hashes.push({ hashes.push({
id: actionId, id: actionId,
page: workerKey, page: workerKey,
codeHash: worker?.codeHash, codeHash: worker?.durability?.codeHash,
runtimeEnvVars: worker?.durability?.runtimeEnvVars,
}) })
} }
} }
...@@ -32,15 +36,14 @@ async function getCodeHashes( ...@@ -32,15 +36,14 @@ async function getCodeHashes(
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)( ;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'app-dir - use-cache-code-hash', 'app-dir - use-cache-code-hash',
() => { () => {
describe('basic', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
skipStart: true,
}) })
it('emits codeHash only for use-cache functions', async () => { it('emits codeHash only for use-cache functions', async () => {
await next.build()
const values = Object.values(await getCodeHashes(next)) const values = Object.values(await getCodeHashes(next))
expect(values.length).toBe(3) expect(values.length).toBe(4)
const valuesWithoutCodeHash = values.filter( const valuesWithoutCodeHash = values.filter(
(e) => typeof e.codeHash !== 'string' (e) => typeof e.codeHash !== 'string'
...@@ -49,6 +52,44 @@ async function getCodeHashes( ...@@ -49,6 +52,44 @@ async function getCodeHashes(
expect(valuesWithoutCodeHash[0].page).toBe('app/use-server/page') expect(valuesWithoutCodeHash[0].page).toBe('app/use-server/page')
}) })
it('lists non-inlined runtime env vars', async () => {
const data = await getCodeHashes(next)
expect(data.find((e) => e.page === 'app/use-cache/page').runtimeEnvVars)
.toMatchInlineSnapshot(`
[
"BUNDLED_NON_INLINED_ENVVAR",
"NEXT_PRIVATE_DEBUG_CACHE",
"__NEXT_DEV_SERVER",
"NEXT_PRIVATE_DEBUG_RUNTIME_DATA",
"NEXT_OTEL_VERBOSE",
"NEXT_OTEL_PERFORMANCE_PREFIX",
"NEXT_SERVER_ACTIONS_ENCRYPTION_KEY",
"EXTERNAL_ENV_VAR",
]
`)
expect(
data.find((e) => e.page === 'app/env-dynamic/page').runtimeEnvVars
).toMatchInlineSnapshot(`
[
"NEXT_PRIVATE_DEBUG_CACHE",
"__NEXT_DEV_SERVER",
"NEXT_PRIVATE_DEBUG_RUNTIME_DATA",
"NEXT_OTEL_VERBOSE",
"NEXT_OTEL_PERFORMANCE_PREFIX",
"NEXT_SERVER_ACTIONS_ENCRYPTION_KEY",
]
`)
})
})
describe('invalidation', () => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
})
it('codeHash stays stable across identical rebuilds', async () => { it('codeHash stays stable across identical rebuilds', async () => {
await next.build() await next.build()
const first = await getCodeHashes(next) const first = await getCodeHashes(next)
...@@ -70,7 +111,7 @@ import { external } from 'external-dep' ...@@ -70,7 +111,7 @@ import { external } from 'external-dep'
export async function logic() { export async function logic() {
'use cache' 'use cache'
return \`\${foo()}:\${external()}\` + ":other" return \`\${foo()}:\${external()}:\${process.env.BUNDLED_NON_INLINED_ENVVAR}\` + ":other"
} }
`, `,
async () => { async () => {
...@@ -111,7 +152,7 @@ export async function logic() { ...@@ -111,7 +152,7 @@ export async function logic() {
await next.patchFile( await next.patchFile(
'node_modules/external-dep/index.js', 'node_modules/external-dep/index.js',
`export function external() { `export function external() {
return 'external-v2' return 'external-v2' + process.env.EXTERNAL_ENV_VAR
} }
`, `,
async () => { async () => {
...@@ -155,11 +196,14 @@ export async function logic() { ...@@ -155,11 +196,14 @@ export async function logic() {
`, `,
async () => { async () => {
await next.build() await next.build()
const after = await getCodeHashes(next, ['app/use-cache-client/page']) const after = await getCodeHashes(next, [
'app/use-cache-client/page',
])
expect(after).toEqual(before) expect(after).toEqual(before)
} }
) )
}) })
})
} }
) )
export async function action() {
'use cache'
return 'first'
}
export async function getData(action) {
'use cache: remote'
console.log(action)
return Math.random()
}
import { connection } from 'next/server'
import { action } from './action'
import { getData } from './get-data'
export default async function Page() {
await connection()
return <span id="data">{await getData(action)}</span>
}
export async function getData(client) {
'use cache: remote'
console.log(client)
return Math.random()
}
import { connection } from 'next/server'
import { client } from './client'
import { getData } from './get-data'
export default async function Page() {
await connection()
return <span id="data">{await getData(client)}</span>
}
export async function action() {
'use server'
return 'first'
}
export async function getData(action) {
'use cache: remote'
console.log(action)
return Math.random()
}
import { connection } from 'next/server'
import { action } from './action'
import { getData } from './get-data'
export default async function Page() {
await connection()
return <span id="data">{await getData(action)}</span>
}
'use client'
export default function Client({ children }: { children: React.ReactNode }) {
return <span id="data">{children}</span>
}
import { Suspense } from 'react'
import { cacheLife } from 'next/cache'
import { connection } from 'next/server'
import { getDate } from '../logic'
import Client from './client'
async function DynamicCache({ id }: { id: string }) {
'use cache: remote'
cacheLife('days')
return <Client>{getDate()}</Client>
}
export default async function Page() {
await connection()
return (
<main>
<Suspense>
<DynamicCache id="dynamic-cache" />
</Suspense>
</main>
)
}
export function getDate() {
return `${new Date().toISOString()}:${process.env.FOOBAR}`
}
import { connection } from 'next/server'
import { getDate } from '../logic'
async function getInnerData() {
'use cache: remote'
return getDate()
}
async function getOuterData() {
'use cache: remote'
return getInnerData()
}
export default async function Page() {
await connection()
return <span id="data">{await getOuterData()}</span>
}
import { Suspense } from 'react' import { Suspense } from 'react'
import { connection } from 'next/server' import { connection } from 'next/server'
import { getDate } from './logic'
async function getData() { async function getData() {
'use cache' 'use cache: remote'
return new Date().toISOString() return getDate()
} }
async function AsyncComp() { async function AsyncComp() {
let data = await getData() let data = await getData()
return <p id="data">{data}</p> return <span id="data">{data}</span>
} }
export default async function Home() { export default async function Home() {
...@@ -18,7 +19,7 @@ export default async function Home() { ...@@ -18,7 +19,7 @@ export default async function Home() {
return ( return (
<main> <main>
<Suspense fallback={<p>Loading...</p>}> <Suspense fallback={<div>Loading...</div>}>
<AsyncComp /> <AsyncComp />
</Suspense> </Suspense>
</main> </main>
......
import { Suspense } from 'react' import { Suspense } from 'react'
import { cacheLife } from 'next/cache' import { cacheLife } from 'next/cache'
import { getDate } from '../logic'
// The id prop is just used to assert on the logged cache key in tests.
async function DynamicCache({ id }: { id: string }) { async function DynamicCache({ id }: { id: string }) {
'use cache' 'use cache: remote'
cacheLife('seconds') cacheLife('days')
return <p id="data">{new Date().toISOString()}</p> return <span id="data">{getDate()}</span>
} }
export default function Page() { export default function Page() {
return ( return (
<p> <main>
This page uses a short-lived "use cache", which is omitted from the
prerender, but should still be saved in the cache handler.
<Suspense> <Suspense>
<DynamicCache id="dynamic-cache" /> <DynamicCache id="dynamic-cache" />
</Suspense> </Suspense>
</p> </main>
) )
} }
import { connection } from 'next/server'
import { getDate } from '../logic'
async function getData() {
'use cache: remote'
return getDate()
}
export async function GET() {
await connection()
return new Response(await getData())
}
// @ts-check
const fs = require('fs')
const path = require('path')
/**
* @type {Record<string, string>}
*/
let data = {}
try {
data = JSON.parse(
fs.readFileSync(path.join(__dirname, 'handler-remote-data.json'), 'utf8')
)
} catch (_e) {}
const client = {
/**
* @param {string} key
* @returns unknown
*/
async get(key) {
return data[key]
},
/**
*
* @param {string} key
* @param {string} value
* @param {{expire?: number} | undefined} options
*/
async set(key, value, options) {
// options.expire is ignored in this impl
data[key] = value
fs.writeFileSync(
path.join(__dirname, 'handler-remote-data.json'),
JSON.stringify(data, null, 2)
)
},
}
/**
* @type {import('next/dist/server/lib/cache-handlers/types').CacheHandler}
*/
module.exports = {
async get(cacheKey, softTags) {
console.log('CustomCacheHandler::get', cacheKey, JSON.stringify([softTags]))
// Retrieve from Redis
const stored = await client.get(cacheKey)
if (!stored) return undefined
// Deserialize the entry
const data = JSON.parse(stored)
// Reconstruct the ReadableStream from stored data
return {
value: new ReadableStream({
start(controller) {
controller.enqueue(Buffer.from(data.value, 'base64'))
controller.close()
},
}),
tags: data.tags,
stale: data.stale,
timestamp: data.timestamp,
expire: data.expire,
revalidate: data.revalidate,
}
},
async set(cacheKey, pendingEntry) {
console.log('CustomCacheHandler::set', cacheKey)
const entry = await pendingEntry
// Read the stream to get the data
const reader = entry.value.getReader()
const chunks = []
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
chunks.push(value)
}
} finally {
reader.releaseLock()
}
// Combine chunks and serialize for Redis storage
const data = Buffer.concat(chunks.map((chunk) => Buffer.from(chunk)))
await client.set(
cacheKey,
JSON.stringify({
value: data.toString('base64'),
tags: entry.tags,
stale: entry.stale,
timestamp: entry.timestamp,
expire: entry.expire,
revalidate: entry.revalidate,
}),
{ expire: entry.expire }
)
},
async refreshTags() {
// No-op for basic Redis implementation
// Could sync with external tag service if needed
},
async getExpiration(tags) {
// Return 0 to indicate no tags have been revalidated
// Could query Redis for tag expiration timestamps if tracking them
return 0
},
async updateTags(tags, durations) {
// Implement tag-based invalidation if needed
// Could iterate over keys with matching tags and delete them
},
}
...@@ -5,12 +5,17 @@ const nextConfig = { ...@@ -5,12 +5,17 @@ const nextConfig = {
cacheComponents: true, cacheComponents: true,
cacheHandlers: { cacheHandlers: {
default: require.resolve('./handler.js'), default: require.resolve('./handler.js'),
remote: require.resolve('./handler-remote.js'),
}, },
generateBuildId: process.env.BUILD_ID generateBuildId: process.env.BUILD_ID
? async () => { ? async () => {
return process.env.BUILD_ID return process.env.BUILD_ID
} }
: undefined, : undefined,
experimental: {
durableUseCacheEntries:
process.env.DURABLE_USE_CACHE_ENTRIES === '1' ? true : undefined,
},
} }
module.exports = nextConfig module.exports = nextConfig
...@@ -115,6 +115,6 @@ function createCacheSetLogRegExp(id: string) { ...@@ -115,6 +115,6 @@ function createCacheSetLogRegExp(id: string) {
// Expect a requestId, that's provided through ALS, to be present in the log // Expect a requestId, that's provided through ALS, to be present in the log
// message for the cache handler set call. // message for the cache handler set call.
return new RegExp( return new RegExp(
`set cache \\["[A-Za-z0-9_-]+","(?:[0-9a-f]{2})+",\\[{"id":"${id}"}]\\] requestId: \\d+` `set cache \\["(?:[0-9a-f]{2})+",\\[{"id":"${id}"}\\],\\["[A-Za-z0-9_-]+"(?:,"[^"]+")*\\]\\] requestId: \\d+`
) )
} }
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