-
Notifications
You must be signed in to change notification settings - Fork 87
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
fix: set netlify-cache-tag for not prerendered content #2495
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f1cad19
test: refactor app router on-demand revalidation test and add test ca…
pieh bd713be
test: refactor pages router on-demand revalidation test and add test …
pieh 4b6de32
fix: capture cache tags during request handling and don't rely on tag…
pieh eaa7e5d
Update src/run/handlers/cache.cts
pieh 8913569
Merge branch 'main' into fix/cache-tags-not-prerendered
pieh 29607db
chore: remove dead code
pieh 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 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 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 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 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 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 |
---|---|---|
|
@@ -109,6 +109,44 @@ export class NetlifyCacheHandler implements CacheHandler { | |
return restOfRouteValue | ||
} | ||
|
||
private captureCacheTags(cacheValue: NetlifyIncrementalCacheValue | null, key: string) { | ||
if (!cacheValue) { | ||
return | ||
} | ||
|
||
const requestContext = getRequestContext() | ||
// Bail if we can't get request context | ||
if (!requestContext) { | ||
return | ||
} | ||
|
||
// Bail if we already have cache tags - `captureCacheTags()` is called on both `CacheHandler.get` and `CacheHandler.set` | ||
// that's because `CacheHandler.get` might not have a cache value (cache miss or on-demand revalidation) in which case | ||
// response is generated in blocking way and we need to capture cache tags from the cache value we are setting. | ||
// If both `CacheHandler.get` and `CacheHandler.set` are called in the same request, we want to use cache tags from | ||
// first `CacheHandler.get` and not from following `CacheHandler.set` as this is pattern for Stale-while-revalidate behavior | ||
// and stale response is served while new one is generated. | ||
if (requestContext.responseCacheTags) { | ||
return | ||
} | ||
Comment on lines
+123
to
+131
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'm trying to think of something less leaky but I can't 😞 |
||
|
||
if ( | ||
cacheValue.kind === 'PAGE' || | ||
cacheValue.kind === 'APP_PAGE' || | ||
cacheValue.kind === 'ROUTE' | ||
) { | ||
if (cacheValue.headers?.[NEXT_CACHE_TAGS_HEADER]) { | ||
const cacheTags = (cacheValue.headers[NEXT_CACHE_TAGS_HEADER] as string).split(',') | ||
requestContext.responseCacheTags = cacheTags | ||
} else if (cacheValue.kind === 'PAGE' && typeof cacheValue.pageData === 'object') { | ||
// pages router doesn't have cache tags headers in PAGE cache value | ||
// so we need to generate appropriate cache tags for it | ||
const cacheTags = [`_N_T_${key === '/index' ? '/' : key}`] | ||
requestContext.responseCacheTags = cacheTags | ||
} | ||
} | ||
} | ||
|
||
private async injectEntryToPrerenderManifest( | ||
key: string, | ||
revalidate: NetlifyCachedPageValue['revalidate'], | ||
|
@@ -176,6 +214,7 @@ export class NetlifyCacheHandler implements CacheHandler { | |
} | ||
|
||
this.captureResponseCacheLastModified(blob, key, span) | ||
this.captureCacheTags(blob.value, key) | ||
|
||
switch (blob.value?.kind) { | ||
case 'FETCH': | ||
|
@@ -273,6 +312,10 @@ export class NetlifyCacheHandler implements CacheHandler { | |
|
||
const value = this.transformToStorableObject(data, context) | ||
|
||
// if previous CacheHandler.get call returned null (page was either never rendered on was on-demand revalidated) | ||
pieh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// and we didn't yet capture cache tags, we try to get cache tags from freshly produced cache value | ||
this.captureCacheTags(value, key) | ||
|
||
await this.blobStore.setJSON(blobKey, { | ||
lastModified, | ||
value, | ||
|
This file contains 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 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 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.
Looks like we can delete this too
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.
... Oh, I don't think this is related to your changes, but still dead code
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.
Yeah, not really related to this change, but I did remove it now