-
Notifications
You must be signed in to change notification settings - Fork 29.5k
fix: unstable_cache should perform blocking revalidation during ISR revalidation #83820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ztanner
merged 1 commit into
canary
from
09-15-fix_unstable_cache_should_perform_blocking_revalidation_during_isr_revalidation
Sep 15, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
test/production/app-dir/unstable-cache-foreground-revalidate/app/isr-10/page.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { unstable_cache } from 'next/cache' | ||
|
||
export const revalidate = 10 | ||
|
||
const getCachedData = unstable_cache( | ||
async () => { | ||
const generatedAt = Date.now() | ||
|
||
// Log when this function is actually executed | ||
console.log('[TEST] unstable_cache callback executed at:', generatedAt) | ||
|
||
// Add a delay to simulate expensive operation | ||
await new Promise((resolve) => setTimeout(resolve, 100)) | ||
|
||
return { | ||
generatedAt, | ||
random: Math.random(), | ||
} | ||
}, | ||
['cached-data'], | ||
{ | ||
revalidate: 5, | ||
} | ||
) | ||
|
||
export default async function Page() { | ||
const pageRenderStart = Date.now() | ||
console.log('[TEST] Page render started at:', pageRenderStart) | ||
|
||
const cachedData = await getCachedData() | ||
|
||
console.log( | ||
'[TEST] Page render completed with cache data from:', | ||
cachedData.generatedAt | ||
) | ||
|
||
return ( | ||
<div> | ||
<div id="page-time">{pageRenderStart}</div> | ||
<div id="cache-generated-at">{cachedData.generatedAt}</div> | ||
<div id="random">{cachedData.random}</div> | ||
</div> | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
test/production/app-dir/unstable-cache-foreground-revalidate/app/layout.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function RootLayout({ children }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
test/production/app-dir/unstable-cache-foreground-revalidate/next.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {} |
74 changes: 74 additions & 0 deletions
74
...app-dir/unstable-cache-foreground-revalidate/unstable-cache-foreground-revalidate.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('unstable-cache-foreground-revalidate', () => { | ||
const { next, isNextDev } = nextTestSetup({ | ||
files: __dirname, | ||
skipDeployment: true, | ||
}) | ||
|
||
if (isNextDev) { | ||
it.skip('should not run in dev mode', () => {}) | ||
return | ||
} | ||
|
||
it('should block and wait for fresh data when ISR page revalidate time is greater than unstable_cache TTL', async () => { | ||
// Initial render to warm up cache | ||
await next.render('/isr-10') | ||
|
||
// Record initial log position | ||
const initialLogLength = next.cliOutput.length | ||
|
||
// Wait for both ISR and unstable_cache to become stale | ||
await new Promise((resolve) => setTimeout(resolve, 11000)) | ||
|
||
// This request triggers ISR background revalidation | ||
await next.render('/isr-10') | ||
|
||
// Wait for ISR background revalidation to complete | ||
await new Promise((resolve) => setTimeout(resolve, 2000)) | ||
|
||
// Get logs since the initial render | ||
const logs = next.cliOutput.substring(initialLogLength) | ||
|
||
const cacheExecutions = [ | ||
...logs.matchAll(/\[TEST\] unstable_cache callback executed at: (\d+)/g), | ||
] | ||
const completions = [ | ||
...logs.matchAll( | ||
/\[TEST\] Page render completed with cache data from: (\d+)/g | ||
), | ||
] | ||
|
||
if (completions.length === 0) { | ||
throw new Error('No page completions found in logs') | ||
} | ||
|
||
const lastCompletion = completions[completions.length - 1] | ||
const lastCacheExecution = | ||
cacheExecutions.length > 0 | ||
? cacheExecutions[cacheExecutions.length - 1] | ||
: null | ||
|
||
if (!lastCacheExecution) { | ||
throw new Error( | ||
`Expected cache execution during ISR revalidation but found none. ` + | ||
`Cache executions: ${cacheExecutions.length}, Page completions: ${completions.length}` | ||
) | ||
} | ||
|
||
const cacheExecutedAt = parseInt(lastCacheExecution[1]) | ||
const cacheDataFrom = parseInt(lastCompletion[1]) | ||
const timeDiff = Math.abs(cacheExecutedAt - cacheDataFrom) | ||
|
||
console.log('ISR revalidation timing:') | ||
console.log('- Cache executed at:', cacheExecutedAt) | ||
console.log('- ISR used cache data from:', cacheDataFrom) | ||
console.log('- Time difference:', timeDiff, 'ms') | ||
|
||
// With foreground revalidation: | ||
// - ISR waits for fresh data, so timestamps should match (< 1000ms difference) | ||
// Without foreground revalidation: | ||
// - ISR uses stale data, so timestamps will be far apart (> 10000ms) | ||
expect(timeDiff).toBeLessThan(1000) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.