-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Plumbing for cache indicator #84955
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
Merged
Plumbing for cache indicator #84955
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
2 changes: 2 additions & 0 deletions
2
packages/next/src/next-devtools/dev-overlay/cache-indicator.tsx
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,2 @@ | ||
export type ServerCacheStatus = 'filling' | 'filled' | ||
export type CacheIndicatorState = 'disabled' | 'ready' | ServerCacheStatus |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2768,9 +2768,13 @@ async function renderWithRestartOnCacheMissInDev( | |
getPayload: (requestStore: RequestStore) => Promise<RSCPayload>, | ||
onError: (error: unknown) => void | ||
) { | ||
const { renderOpts } = ctx | ||
const { clientReferenceManifest, ComponentMod, setReactDebugChannel } = | ||
renderOpts | ||
const { htmlRequestId, renderOpts, requestId } = ctx | ||
const { | ||
clientReferenceManifest, | ||
ComponentMod, | ||
setCacheStatus, | ||
setReactDebugChannel, | ||
} = renderOpts | ||
assertClientReferenceManifest(clientReferenceManifest) | ||
|
||
// If the render is restarted, we'll recreate a fresh request store | ||
|
@@ -2898,6 +2902,10 @@ async function renderWithRestartOnCacheMissInDev( | |
} | ||
} | ||
|
||
if (process.env.NODE_ENV === 'development') { | ||
setCacheStatus!('filling', htmlRequestId, requestId) | ||
} | ||
eps1lon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Cache miss. We will use the initial render to fill caches, and discard its result. | ||
// Then, we can render again with warm caches. | ||
|
||
|
@@ -2967,6 +2975,12 @@ async function renderWithRestartOnCacheMissInDev( | |
) | ||
) | ||
|
||
if (process.env.NODE_ENV === 'development') { | ||
// TODO: Don't know if this is the right time. | ||
// It's not wired up on the frontend though. | ||
Comment on lines
+2979
to
+2980
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine here. Technically it could be directly after awaiting cacheReady, but then we'd have a bit more nothing-time before the first chunks stream in. So I think it's better to prolong the filling state until here. |
||
setCacheStatus!('filled', htmlRequestId, requestId) | ||
} | ||
|
||
return { | ||
stream: finalServerStream, | ||
debugChannel, | ||
|
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsafe non-null assertions on
setCacheStatus
could cause a runtime error. The function is optional and can be undefined even whenNODE_ENV === 'development'
, but the code uses non-null assertions without checking if the function is actually defined.View Details
📝 Patch Details
Analysis
Unsafe non-null assertions on
setCacheStatus
in cache component renderingWhat fails: Lines 2808 and 2869 in
packages/next/src/server/app-render/app-render.tsx
use non-null assertions (setCacheStatus!()
) to call an optional function that can be undefined, causing a runtime TypeError if the function is not available.How to reproduce:
experimental.cacheComponents: true
in next.config.jsprocess.env.NODE_ENV = 'development'
externally (before Next.js initialization)opts.dev = false
)Result: Runtime error:
Expected: The code should safely check if the function exists before calling it.
Root cause: The function
setCacheStatus
is defined as an optional property in theRenderOptsPartial
type. It gets assigned a value only if both:config.experimental.cacheComponents
is true ANDdevBundlerService
is defined (which requiresopts.dev
to be true)However, the guard that calls this function only checks
process.env.NODE_ENV === 'development' && experimental.cacheComponents
. SinceNODE_ENV
can be set externally before Next.js initializes, it's possible for the guard to pass even whenopts.dev
is false, makingdevBundlerService
undefined andsetCacheStatus
unassigned.Fix: Added null checks before calling
setCacheStatus
at both locations to ensure the function is only invoked if it's actually defined:if (process.env.NODE_ENV === 'development')
toif (process.env.NODE_ENV === 'development' && setCacheStatus)
if (process.env.NODE_ENV === 'development')
toif (process.env.NODE_ENV === 'development' && setCacheStatus)
!
) since we now have proper guardsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This branch should only be hit with cache components. If not, that'd be a bug.