diff --git a/packages/next/src/server/web/spec-extension/unstable-cache.ts b/packages/next/src/server/web/spec-extension/unstable-cache.ts index e58f6101c6376..cbeeedd772e70 100644 --- a/packages/next/src/server/web/spec-extension/unstable-cache.ts +++ b/packages/next/src/server/web/spec-extension/unstable-cache.ts @@ -1,3 +1,4 @@ +import { maybePostpone } from '../../../client/components/maybe-postpone' import type { StaticGenerationStore, StaticGenerationAsyncStorage, @@ -29,6 +30,23 @@ export function unstable_cache( const store: undefined | StaticGenerationStore = staticGenerationAsyncStorage?.getStore() + if (store && typeof options.revalidate === 'number') { + // Revalidate 0 is a special case, it means opt-out of static generation. + if (options.revalidate === 0) { + maybePostpone(store, 'revalidate: 0') + // Set during dynamic rendering + store.revalidate = 0 + // If revalidate was already set in the store before we should check if the new value is lower, set it to the lowest of the two. + } else if (typeof store.revalidate === 'number') { + if (store.revalidate > options.revalidate) { + store.revalidate = options.revalidate + } + // All other cases we set the value from the option. + } else { + store.revalidate = options.revalidate + } + } + const incrementalCache: | import('../../lib/incremental-cache').IncrementalCache | undefined =