Unverified Commit 00156f54 authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Include buildId in the cache breaker until we have hashed Action IDs (#70542)

Currently our Action IDs don't include hashing of the implementation
which they ideally should, or at least have a separate ID for that. This
means it is not safe to reuse cache entries across deployments since the
Action ID can remain unchanged.

For now we include the build ID as part of the cache key to ensure we
don't use cache entries. We should remove this or replace it with the
hash of the Action later.
parent c23f9577
...@@ -1904,6 +1904,7 @@ export default async function build( ...@@ -1904,6 +1904,7 @@ export default async function build(
nextConfigOutput: config.output, nextConfigOutput: config.output,
pprConfig: config.experimental.ppr, pprConfig: config.experimental.ppr,
isAppPPRFallbacksEnabled: config.experimental.pprFallbacks, isAppPPRFallbacksEnabled: config.experimental.pprFallbacks,
buildId,
}) })
) )
...@@ -2123,6 +2124,7 @@ export default async function build( ...@@ -2123,6 +2124,7 @@ export default async function build(
pprConfig: config.experimental.ppr, pprConfig: config.experimental.ppr,
isAppPPRFallbacksEnabled: isAppPPRFallbacksEnabled:
config.experimental.pprFallbacks, config.experimental.pprFallbacks,
buildId,
}) })
} }
) )
......
...@@ -1360,6 +1360,7 @@ export async function buildAppStaticPaths({ ...@@ -1360,6 +1360,7 @@ export async function buildAppStaticPaths({
ComponentMod, ComponentMod,
isRoutePPREnabled, isRoutePPREnabled,
isAppPPRFallbacksEnabled, isAppPPRFallbacksEnabled,
buildId,
}: { }: {
dir: string dir: string
page: string page: string
...@@ -1376,6 +1377,7 @@ export async function buildAppStaticPaths({ ...@@ -1376,6 +1377,7 @@ export async function buildAppStaticPaths({
ComponentMod: AppPageModule ComponentMod: AppPageModule
isRoutePPREnabled: boolean | undefined isRoutePPREnabled: boolean | undefined
isAppPPRFallbacksEnabled: boolean | undefined isAppPPRFallbacksEnabled: boolean | undefined
buildId: string
}): Promise<PartialStaticPathsResult> { }): Promise<PartialStaticPathsResult> {
ComponentMod.patchFetch() ComponentMod.patchFetch()
...@@ -1424,6 +1426,7 @@ export async function buildAppStaticPaths({ ...@@ -1424,6 +1426,7 @@ export async function buildAppStaticPaths({
after: false, after: false,
dynamicIO: false, dynamicIO: false,
}, },
buildId,
}, },
}, },
async (): Promise<PartialStaticPathsResult> => { async (): Promise<PartialStaticPathsResult> => {
...@@ -1592,6 +1595,7 @@ export async function isPageStatic({ ...@@ -1592,6 +1595,7 @@ export async function isPageStatic({
cacheHandler, cacheHandler,
pprConfig, pprConfig,
isAppPPRFallbacksEnabled, isAppPPRFallbacksEnabled,
buildId,
}: { }: {
dir: string dir: string
page: string page: string
...@@ -1613,6 +1617,7 @@ export async function isPageStatic({ ...@@ -1613,6 +1617,7 @@ export async function isPageStatic({
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
pprConfig: ExperimentalPPRConfig | undefined pprConfig: ExperimentalPPRConfig | undefined
isAppPPRFallbacksEnabled: boolean | undefined isAppPPRFallbacksEnabled: boolean | undefined
buildId: string
}): Promise<PageIsStaticResult> { }): Promise<PageIsStaticResult> {
const isPageStaticSpan = trace('is-page-static-utils', parentId) const isPageStaticSpan = trace('is-page-static-utils', parentId)
return isPageStaticSpan return isPageStaticSpan
...@@ -1739,6 +1744,7 @@ export async function isPageStatic({ ...@@ -1739,6 +1744,7 @@ export async function isPageStatic({
nextConfigOutput, nextConfigOutput,
isRoutePPREnabled, isRoutePPREnabled,
isAppPPRFallbacksEnabled, isAppPPRFallbacksEnabled,
buildId,
})) }))
} }
} else { } else {
......
...@@ -66,6 +66,8 @@ export interface StaticGenerationStore { ...@@ -66,6 +66,8 @@ export interface StaticGenerationStore {
isPrefetchRequest?: boolean isPrefetchRequest?: boolean
requestEndedState?: { ended?: boolean } requestEndedState?: { ended?: boolean }
buildId: string
} }
export type StaticGenerationAsyncStorage = export type StaticGenerationAsyncStorage =
......
...@@ -43,7 +43,8 @@ export async function exportAppRoute( ...@@ -43,7 +43,8 @@ export async function exportAppRoute(
distDir: string, distDir: string,
htmlFilepath: string, htmlFilepath: string,
fileWriter: FileWriter, fileWriter: FileWriter,
experimental: Required<Pick<ExperimentalConfig, 'after' | 'dynamicIO'>> experimental: Required<Pick<ExperimentalConfig, 'after' | 'dynamicIO'>>,
buildId: string
): Promise<ExportRouteResult> { ): Promise<ExportRouteResult> {
// Ensure that the URL is absolute. // Ensure that the URL is absolute.
req.url = `http://localhost:3000${req.url}` req.url = `http://localhost:3000${req.url}`
...@@ -76,6 +77,7 @@ export async function exportAppRoute( ...@@ -76,6 +77,7 @@ export async function exportAppRoute(
incrementalCache, incrementalCache,
waitUntil: undefined, waitUntil: undefined,
onClose: undefined, onClose: undefined,
buildId,
}, },
} }
......
...@@ -246,7 +246,8 @@ async function exportPageImpl( ...@@ -246,7 +246,8 @@ async function exportPageImpl(
distDir, distDir,
htmlFilepath, htmlFilepath,
fileWriter, fileWriter,
input.renderOpts.experimental input.renderOpts.experimental,
input.renderOpts.buildId
) )
} }
......
...@@ -56,6 +56,7 @@ export type StaticGenerationContext = { ...@@ -56,6 +56,7 @@ export type StaticGenerationContext = {
| 'nextExport' | 'nextExport'
| 'isDraftMode' | 'isDraftMode'
| 'isDebugDynamicAccesses' | 'isDebugDynamicAccesses'
| 'buildId'
> & > &
Partial<RequestLifecycleOpts> Partial<RequestLifecycleOpts>
} }
...@@ -114,6 +115,7 @@ export const withStaticGenerationStore: WithStore< ...@@ -114,6 +115,7 @@ export const withStaticGenerationStore: WithStore<
requestEndedState, requestEndedState,
isPrefetchRequest, isPrefetchRequest,
buildId: renderOpts.buildId,
} }
// TODO: remove this when we resolve accessing the store outside the execution context // TODO: remove this when we resolve accessing the store outside the execution context
......
...@@ -2506,6 +2506,7 @@ export default abstract class Server< ...@@ -2506,6 +2506,7 @@ export default abstract class Server<
onClose: res.onClose.bind(res), onClose: res.onClose.bind(res),
onInstrumentationRequestError: onInstrumentationRequestError:
this.renderOpts.onInstrumentationRequestError, this.renderOpts.onInstrumentationRequestError,
buildId: this.renderOpts.buildId,
}, },
} }
......
...@@ -776,6 +776,7 @@ export default class DevServer extends Server { ...@@ -776,6 +776,7 @@ export default class DevServer extends Server {
maxMemoryCacheSize: this.nextConfig.cacheMaxMemorySize, maxMemoryCacheSize: this.nextConfig.cacheMaxMemorySize,
nextConfigOutput: this.nextConfig.output, nextConfigOutput: this.nextConfig.output,
isAppPPRFallbacksEnabled: this.nextConfig.experimental.pprFallbacks, isAppPPRFallbacksEnabled: this.nextConfig.experimental.pprFallbacks,
buildId: this.renderOpts.buildId,
}) })
return pathsResult return pathsResult
} finally { } finally {
......
...@@ -53,6 +53,7 @@ export async function loadStaticPaths({ ...@@ -53,6 +53,7 @@ export async function loadStaticPaths({
cacheHandler, cacheHandler,
nextConfigOutput, nextConfigOutput,
isAppPPRFallbacksEnabled, isAppPPRFallbacksEnabled,
buildId,
}: { }: {
dir: string dir: string
distDir: string distDir: string
...@@ -70,6 +71,7 @@ export async function loadStaticPaths({ ...@@ -70,6 +71,7 @@ export async function loadStaticPaths({
cacheHandler?: string cacheHandler?: string
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
isAppPPRFallbacksEnabled: boolean | undefined isAppPPRFallbacksEnabled: boolean | undefined
buildId: string
}): Promise<PartialStaticPathsResult> { }): Promise<PartialStaticPathsResult> {
// update work memory runtime-config // update work memory runtime-config
require('../../shared/lib/runtime-config.external').setConfig(config) require('../../shared/lib/runtime-config.external').setConfig(config)
...@@ -129,6 +131,7 @@ export async function loadStaticPaths({ ...@@ -129,6 +131,7 @@ export async function loadStaticPaths({
nextConfigOutput, nextConfigOutput,
isRoutePPREnabled, isRoutePPREnabled,
isAppPPRFallbacksEnabled, isAppPPRFallbacksEnabled,
buildId,
}) })
} }
......
import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import { createSnapshot } from '../../client/components/async-local-storage' import { createSnapshot } from '../../client/components/async-local-storage'
/* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable import/no-extraneous-dependencies */
import { import {
...@@ -15,6 +16,8 @@ import { ...@@ -15,6 +16,8 @@ import {
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external' import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external'
import { staticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage.external' import { staticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage.external'
import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin'
import { import {
getClientReferenceManifestSingleton, getClientReferenceManifestSingleton,
getServerModuleMap, getServerModuleMap,
...@@ -75,6 +78,7 @@ const runInCleanSnapshot = createSnapshot() ...@@ -75,6 +78,7 @@ const runInCleanSnapshot = createSnapshot()
async function generateCacheEntry( async function generateCacheEntry(
staticGenerationStore: StaticGenerationStore, staticGenerationStore: StaticGenerationStore,
clientReferenceManifest: DeepReadonly<ClientReferenceManifest>,
cacheHandler: CacheHandler, cacheHandler: CacheHandler,
serializedCacheKey: string | ArrayBuffer, serializedCacheKey: string | ArrayBuffer,
encodedArguments: FormData | string, encodedArguments: FormData | string,
...@@ -82,7 +86,7 @@ async function generateCacheEntry( ...@@ -82,7 +86,7 @@ async function generateCacheEntry(
): Promise<ReadableStream> { ): Promise<ReadableStream> {
const temporaryReferences = createServerTemporaryReferenceSet() const temporaryReferences = createServerTemporaryReferenceSet()
const [, args] = await decodeReply<any[]>( const [, , args] = await decodeReply<any[]>(
encodedArguments, encodedArguments,
getServerModuleMap(), getServerModuleMap(),
{ {
...@@ -96,11 +100,9 @@ async function generateCacheEntry( ...@@ -96,11 +100,9 @@ async function generateCacheEntry(
let didError = false let didError = false
let firstError: any = null let firstError: any = null
const clientReferenceManifestSingleton = getClientReferenceManifestSingleton()
const stream = renderToReadableStream( const stream = renderToReadableStream(
result, result,
clientReferenceManifestSingleton.clientModules, clientReferenceManifest.clientModules,
{ {
environmentName: 'Cache', environmentName: 'Cache',
temporaryReferences, temporaryReferences,
...@@ -172,9 +174,22 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -172,9 +174,22 @@ export function cache(kind: string, id: string, fn: any) {
const name = fn.name const name = fn.name
const cachedFn = { const cachedFn = {
[name]: async function (...args: any[]) { [name]: async function (...args: any[]) {
const staticGenerationStore = staticGenerationAsyncStorage.getStore()
if (staticGenerationStore === undefined) {
throw new Error(
'"use cache" cannot be used outside of App Router. Expected a StaticGenerationStore.'
)
}
// 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 = staticGenerationStore.buildId
const temporaryReferences = createClientTemporaryReferenceSet() const temporaryReferences = createClientTemporaryReferenceSet()
const encodedArguments: FormData | string = await encodeReply( const encodedArguments: FormData | string = await encodeReply(
[id, args], [buildId, id, args],
{ {
temporaryReferences, temporaryReferences,
} }
...@@ -193,13 +208,6 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -193,13 +208,6 @@ export function cache(kind: string, id: string, fn: any) {
let entry: undefined | CacheEntry = let entry: undefined | CacheEntry =
await cacheHandler.get(serializedCacheKey) await cacheHandler.get(serializedCacheKey)
const staticGenerationStore = staticGenerationAsyncStorage.getStore()
if (staticGenerationStore === undefined) {
throw new Error(
'"use cache" cannot be used outside of App Router. Expected a StaticGenerationStore.'
)
}
let stream let stream
if ( if (
entry === undefined || entry === undefined ||
...@@ -216,9 +224,16 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -216,9 +224,16 @@ export function cache(kind: string, id: string, fn: any) {
// might include request specific things like cookies() inside a React.cache(). // might include request specific things like cookies() inside a React.cache().
// Note: It is important that we await at least once before this because it lets us // Note: It is important that we await at least once before this because it lets us
// pop out of any stack specific contexts as well - aka "Sync" Local Storage. // pop out of any stack specific contexts as well - aka "Sync" Local Storage.
// Get the clientReferenceManifestSingleton while we're still in the outer Context.
// In case getClientReferenceManifestSingleton is implemented using AsyncLocalStorage.
const clientReferenceManifestSingleton =
getClientReferenceManifestSingleton()
stream = await runInCleanSnapshot( stream = await runInCleanSnapshot(
generateCacheEntry, generateCacheEntry,
staticGenerationStore, staticGenerationStore,
clientReferenceManifestSingleton,
cacheHandler, cacheHandler,
serializedCacheKey, serializedCacheKey,
encodedArguments, encodedArguments,
...@@ -229,9 +244,12 @@ export function cache(kind: string, id: string, fn: any) { ...@@ -229,9 +244,12 @@ export function cache(kind: string, id: string, fn: any) {
if (entry.stale) { if (entry.stale) {
// If this is stale, and we're not in a prerender (i.e. this is dynamic render), // If this is stale, and we're not in a prerender (i.e. this is dynamic render),
// then we should warm up the cache with a fresh revalidated entry. // then we should warm up the cache with a fresh revalidated entry.
const clientReferenceManifestSingleton =
getClientReferenceManifestSingleton()
const ignoredStream = await runInCleanSnapshot( const ignoredStream = await runInCleanSnapshot(
generateCacheEntry, generateCacheEntry,
staticGenerationStore, staticGenerationStore,
clientReferenceManifestSingleton,
cacheHandler, cacheHandler,
serializedCacheKey, serializedCacheKey,
encodedArguments, encodedArguments,
......
...@@ -117,6 +117,7 @@ export class EdgeRouteModuleWrapper { ...@@ -117,6 +117,7 @@ export class EdgeRouteModuleWrapper {
after: isAfterEnabled, after: isAfterEnabled,
dynamicIO: false, dynamicIO: false,
}, },
buildId: '', // TODO: Populate this properly.
}, },
} }
......
...@@ -362,6 +362,7 @@ export function unstable_cache<T extends Callback>( ...@@ -362,6 +362,7 @@ export function unstable_cache<T extends Callback>(
page: '/', page: '/',
isStaticGeneration: false, isStaticGeneration: false,
fallbackRouteParams: null, fallbackRouteParams: null,
buildId: '', // Since this is a fake one it can't "use cache" anyway.
}, },
cb, cb,
...args ...args
......
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