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 {
}
#[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(
self.get_rsc_transitions(
self.ecmascript_client_reference_transition(),
......@@ -459,7 +459,7 @@ impl AppProject {
}
#[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 = [
(
AppProject::client_transition_name(),
......
This diff is collapsed.
......@@ -459,13 +459,22 @@ pub struct ActionManifestEntry<'a> {
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ActionManifestWorkerEntry<'a> {
#[serde(rename = "moduleId")]
pub module_id: ActionManifestModuleId<'a>,
#[serde(rename = "async")]
pub is_async: bool,
#[serde(rename = "codeHash")]
pub code_hash: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
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)]
......
......@@ -72,6 +72,11 @@ type Actions = {
[name: string]: {
moduleId: string | number
async: boolean
durability?: {
codeHash: string
runtimeEnvVars: string[]
referencesClientComponent?: boolean
}
}
}
// Record which layer the action is in (rsc or sc_action), in the specific entry
......
......@@ -261,7 +261,17 @@ function createServerModuleMap(): ServerModuleMap {
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) {
workerEntry = workers[normalizeWorkerPageName(workStore.page)]
......@@ -280,9 +290,15 @@ function createServerModuleMap(): ServerModuleMap {
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 {
* The flight entry loader keys actions by bundlePath. bundlePath corresponds
* with the relative path (including 'app') to the page entrypoint.
*/
function normalizeWorkerPageName(pageName: string) {
export function normalizeWorkerPageName(pageName: string) {
if (pathHasPrefix(pageName, 'app')) {
return pageName
}
......
......@@ -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)
// The outer store is `'request'`-typed and built from the forwarded
......
......@@ -51,7 +51,9 @@ import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight
import {
getClientReferenceManifest,
getServerActionsManifest,
getServerModuleMap,
normalizeWorkerPageName,
} from '../app-render/manifests-singleton'
import type { CacheEntry } from '../lib/cache-handlers/types'
import type { CacheSignal } from '../app-render/cache-signal'
......@@ -153,9 +155,13 @@ interface PublicCacheContext {
type CacheContext = PrivateCacheContext | PublicCacheContext
export type CacheKeyParts =
| [buildId: string, id: string, args: unknown[]]
| [buildId: string, id: string, args: unknown[], hmrRefreshHash: string]
const nextVersion = process.env.__NEXT_VERSION as string
export type CacheKeyParts = [
id: string,
args: unknown[],
implementationHash: unknown,
]
interface UseCachePageInnerProps {
params: Promise<Params>
......@@ -1285,7 +1291,7 @@ async function generateCacheEntryImpl(
const temporaryReferences = createServerTemporaryReferenceSet()
const outerWorkUnitStore = cacheContext.outerWorkUnitStore
const [, , args] =
const [, args] =
typeof encodedArguments === 'string'
? await decodeReply<CacheKeyParts>(
encodedArguments,
......@@ -1982,19 +1988,6 @@ export async function cache(
// In case getClientReferenceManifestSingleton is implemented using AsyncLocalStorage.
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)
if (cacheContext.kind === 'private') {
......@@ -2155,9 +2148,11 @@ export async function cache(
// long as the request. In development private caches are persisted across
// requests, so `cacheHandlerKeyBase` (below) additionally scopes the handler
// key by the request's cookies and headers.
const cacheKeyParts: CacheKeyParts = hmrRefreshHash
? [buildId, id, args, hmrRefreshHash]
: [buildId, id, args]
const cacheKeyParts: CacheKeyParts = [
id,
args,
await computeCacheKeyImplementationPart(workStore, workUnitStore, id),
]
const encodeCacheKeyParts = () =>
encodeReply(cacheKeyParts, {
......@@ -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,
* or `generateMetadata`/`generateViewport` in a page file.
......
......@@ -27,15 +27,10 @@ describe('use-cache-custom-handler', () => {
expect(cliOutput).toContain('ModernCustomCacheHandler::refreshTags')
// In development the cache key carries a trailing HMR refresh hash element
// (absent in production), so the args array may be followed by an optional
// quoted hash.
// The implementation parts contain either the code hash and version or the
// build ID, followed by an optional HMR refresh hash in development.
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' \]/
)
expect(next.cliOutput.slice(outputIndex)).toMatch(
/ModernCustomCacheHandler::set \["(development|[A-Za-z0-9_-]+)","([0-9a-f]{2})+",\[\](,"[^"]+")?\]/
/ModernCustomCacheHandler::get \["([0-9a-f]{2})+",\[\],\["(development|[A-Za-z0-9_-]+)"(,"[^"]+")*\]\] \[ '_N_T_\/layout', '_N_T_\/page', '_N_T_\/', '_N_T_\/index' \]/
)
// Since no existing cache entry was retrieved, we don't need to call
......@@ -129,7 +124,7 @@ describe('use-cache-custom-handler', () => {
if (isNextStart) {
it('should save a short-lived cache during prerendering at buildtime', async () => {
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', () => {
// The default handler skips storing the `expire: 0` entry (it would never
// be served back in production)...
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.
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.
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 {
......@@ -50,16 +50,16 @@ describe('use-cache-default-handler-expire-zero', () => {
// In development the default handler keeps `expire: 0` entries (its
// minimum retention serves them warm across reloads), so it stores rather
// than skips. The dev cache key always carries a trailing HMR refresh
// hash element after the args array.
// than skips. The implementation parts include the build ID and a
// trailing HMR refresh hash.
await retry(async () => {
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(
/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 { preinit } from 'react-dom'
import './client-reference-css.css'
export default async function Page() {
'use cache'
if (Date.now() < 0) {
// Try importing anything from react-dom
preinit('abc')
}
return (
<p>
<ClientComponent />
......
......@@ -4,5 +4,5 @@ import { external } from 'external-dep'
export async function logic() {
'use cache'
return `${foo()}:${external()}`
return `${foo()}:${external()}:${process.env.BUNDLED_NON_INLINED_ENVVAR}`
}
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'
async function getCodeHashes(
next: NextInstance,
pages?: string[]
): Promise<{ id: string; page: string; codeHash?: string }[]> {
): Promise<
{ id: string; page: string; codeHash?: string; runtimeEnvVars?: string[] }[]
> {
const manifest = await next.readJSON(
'.next/server/server-reference-manifest.json'
)
......@@ -12,6 +14,7 @@ async function getCodeHashes(
id: string
page: string
codeHash?: string
runtimeEnvVars?: string[]
}[] = []
for (const [actionId, entry] of Object.entries<any>(manifest.node)) {
for (const [workerKey, worker] of Object.entries<any>(entry.workers)) {
......@@ -19,7 +22,8 @@ async function getCodeHashes(
hashes.push({
id: actionId,
page: workerKey,
codeHash: worker?.codeHash,
codeHash: worker?.durability?.codeHash,
runtimeEnvVars: worker?.durability?.runtimeEnvVars,
})
}
}
......@@ -32,134 +36,174 @@ async function getCodeHashes(
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'app-dir - use-cache-code-hash',
() => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
describe('basic', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('emits codeHash only for use-cache functions', async () => {
const values = Object.values(await getCodeHashes(next))
expect(values.length).toBe(4)
const valuesWithoutCodeHash = values.filter(
(e) => typeof e.codeHash !== 'string'
)
expect(valuesWithoutCodeHash.length).toBe(1)
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",
]
`)
})
})
it('emits codeHash only for use-cache functions', async () => {
await next.build()
const values = Object.values(await getCodeHashes(next))
expect(values.length).toBe(3)
describe('invalidation', () => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
})
const valuesWithoutCodeHash = values.filter(
(e) => typeof e.codeHash !== 'string'
)
expect(valuesWithoutCodeHash.length).toBe(1)
expect(valuesWithoutCodeHash[0].page).toBe('app/use-server/page')
})
it('codeHash stays stable across identical rebuilds', async () => {
await next.build()
const first = await getCodeHashes(next)
it('codeHash stays stable across identical rebuilds', async () => {
await next.build()
const first = await getCodeHashes(next)
await next.build()
const second = await getCodeHashes(next)
await next.build()
const second = await getCodeHashes(next)
expect(second).toEqual(first)
})
expect(second).toEqual(first)
})
it("changes when the action's own code changes", async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page'])
it("changes when the action's own code changes", async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page'])
await next.patchFile(
'app/use-cache/logic.tsx',
`import { foo } from './foo'
await next.patchFile(
'app/use-cache/logic.tsx',
`import { foo } from './foo'
import { external } from 'external-dep'
export async function logic() {
'use cache'
return \`\${foo()}:\${external()}\` + ":other"
return \`\${foo()}:\${external()}:\${process.env.BUNDLED_NON_INLINED_ENVVAR}\` + ":other"
}
`,
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache/page'])
// Same set of actions, but the hash for the changed action differs.
expect(Object.keys(after)).toEqual(Object.keys(before))
expect(after).not.toEqual(before)
}
)
})
it('codeHash changes when an imported dependency changes', async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page'])
await next.patchFile(
'app/use-cache/foo.tsx',
`export function foo() {
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache/page'])
// Same set of actions, but the hash for the changed action differs.
expect(Object.keys(after)).toEqual(Object.keys(before))
expect(after).not.toEqual(before)
}
)
})
it('codeHash changes when an imported dependency changes', async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page'])
await next.patchFile(
'app/use-cache/foo.tsx',
`export function foo() {
return "foo-v2"
}
`,
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache/page'])
expect(Object.keys(after)).toEqual(Object.keys(before))
expect(after).not.toEqual(before)
}
)
})
it('codeHash changes when an external (node_modules) dependency changes', async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page'])
await next.patchFile(
'node_modules/external-dep/index.js',
`export function external() {
return 'external-v2'
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache/page'])
expect(Object.keys(after)).toEqual(Object.keys(before))
expect(after).not.toEqual(before)
}
)
})
it('codeHash changes when an external (node_modules) dependency changes', async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page'])
await next.patchFile(
'node_modules/external-dep/index.js',
`export function external() {
return 'external-v2' + process.env.EXTERNAL_ENV_VAR
}
`,
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache/page'])
expect(Object.keys(after)).toEqual(Object.keys(before))
expect(after).not.toEqual(before)
}
)
})
it('codeHash does not change when an unrelated file changes', async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page'])
await next.patchFile(
'app/use-cache/unrelated.ts',
`export function unrelated() {
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache/page'])
expect(Object.keys(after)).toEqual(Object.keys(before))
expect(after).not.toEqual(before)
}
)
})
it('codeHash does not change when an unrelated file changes', async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page'])
await next.patchFile(
'app/use-cache/unrelated.ts',
`export function unrelated() {
return 'unrelated-v2'
}
`,
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache/page'])
expect(after).toEqual(before)
}
)
})
it('codeHash does not change when a client file changes', async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache-client/page'])
await next.patchFile(
'app/use-cache-client/data.ts',
`export function data() {
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache/page'])
expect(after).toEqual(before)
}
)
})
it('codeHash does not change when a client file changes', async () => {
await next.build()
const before = await getCodeHashes(next, ['app/use-cache-client/page'])
await next.patchFile(
'app/use-cache-client/data.ts',
`export function data() {
return 'data-v2'
}
`,
async () => {
await next.build()
const after = await getCodeHashes(next, ['app/use-cache-client/page'])
expect(after).toEqual(before)
}
)
async () => {
await next.build()
const after = await getCodeHashes(next, [
'app/use-cache-client/page',
])
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 { connection } from 'next/server'
import { getDate } from './logic'
async function getData() {
'use cache'
'use cache: remote'
return new Date().toISOString()
return getDate()
}
async function AsyncComp() {
let data = await getData()
return <p id="data">{data}</p>
return <span id="data">{data}</span>
}
export default async function Home() {
......@@ -18,7 +19,7 @@ export default async function Home() {
return (
<main>
<Suspense fallback={<p>Loading...</p>}>
<Suspense fallback={<div>Loading...</div>}>
<AsyncComp />
</Suspense>
</main>
......
import { Suspense } from 'react'
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 }) {
'use cache'
cacheLife('seconds')
return <p id="data">{new Date().toISOString()}</p>
'use cache: remote'
cacheLife('days')
return <span id="data">{getDate()}</span>
}
export default function Page() {
return (
<p>
This page uses a short-lived "use cache", which is omitted from the
prerender, but should still be saved in the cache handler.
<main>
<Suspense>
<DynamicCache id="dynamic-cache" />
</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 = {
cacheComponents: true,
cacheHandlers: {
default: require.resolve('./handler.js'),
remote: require.resolve('./handler-remote.js'),
},
generateBuildId: process.env.BUILD_ID
? async () => {
return process.env.BUILD_ID
}
: undefined,
experimental: {
durableUseCacheEntries:
process.env.DURABLE_USE_CACHE_ENTRIES === '1' ? true : undefined,
},
}
module.exports = nextConfig
......@@ -115,6 +115,6 @@ function createCacheSetLogRegExp(id: string) {
// Expect a requestId, that's provided through ALS, to be present in the log
// message for the cache handler set call.
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