Skip to content

Commit

Permalink
Ensure unstable_cache revalidate option enables ISR (#57390)
Browse files Browse the repository at this point in the history
Noticed that if you use `unstable_cache` it did not opt-in to ISR even though fetch() does opt-in. This ensures the prerender-manifest holds the revalidate value.
  • Loading branch information
timneutkens authored Oct 25, 2023
1 parent 509b88a commit d8160e0
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/next/src/server/web/spec-extension/unstable-cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { maybePostpone } from '../../../client/components/maybe-postpone'
import type {
StaticGenerationStore,
StaticGenerationAsyncStorage,
Expand Down Expand Up @@ -29,6 +30,23 @@ export function unstable_cache<T extends Callback>(
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 =
Expand Down

0 comments on commit d8160e0

Please sign in to comment.