-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: add lastModified field to live collection cache hints #13999
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| 'astro': patch | ||
| --- | ||
|
|
||
| Adds `lastModified` field to experimental live collection cache hints | ||
|
|
||
| Live loaders can now set a `lastModified` field in the cache hints for entries and collections to indicate when the data was last modified. This is then available in the `cacheHint` field returned by `getCollection` and `getEntry`. |
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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import { | |
| unescapeHTML, | ||
| } from '../runtime/server/index.js'; | ||
| import type { | ||
| CacheHint, | ||
| LiveDataCollectionResult, | ||
| LiveDataEntry, | ||
| LiveDataEntryResult, | ||
|
|
@@ -72,6 +73,7 @@ export function createCollectionToGlobResultMap({ | |
| const cacheHintSchema = z.object({ | ||
| tags: z.array(z.string()).optional(), | ||
| maxAge: z.number().optional(), | ||
| lastModified: z.date().optional(), | ||
| }); | ||
|
|
||
| async function parseLiveEntry( | ||
|
|
@@ -529,33 +531,45 @@ export function createGetLiveCollection({ | |
| if (processedEntries.length > 0) { | ||
| const entryTags = new Set<string>(); | ||
| let minMaxAge: number | undefined; | ||
| let latestModified: Date | undefined; | ||
|
|
||
| for (const entry of processedEntries) { | ||
| if (entry.cacheHint) { | ||
| if (entry.cacheHint.tags) { | ||
| entry.cacheHint.tags.forEach((tag) => entryTags.add(tag)); | ||
| } | ||
| if (typeof entry.cacheHint.maxAge === 'number') { | ||
| minMaxAge = | ||
| minMaxAge === undefined | ||
| ? entry.cacheHint.maxAge | ||
| : Math.min(minMaxAge, entry.cacheHint.maxAge); | ||
| if (minMaxAge === undefined || entry.cacheHint.maxAge < minMaxAge) { | ||
| minMaxAge = entry.cacheHint.maxAge; | ||
| } | ||
| } | ||
| if (entry.cacheHint.lastModified instanceof Date) { | ||
| if (latestModified === undefined || entry.cacheHint.lastModified > latestModified) { | ||
| latestModified = entry.cacheHint.lastModified; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Merge collection and entry cache hints | ||
| if (entryTags.size > 0 || minMaxAge !== undefined || cacheHint) { | ||
| const mergedCacheHint: any = {}; | ||
| if (entryTags.size > 0 || minMaxAge !== undefined || latestModified || cacheHint) { | ||
| const mergedCacheHint: CacheHint = {}; | ||
| if (cacheHint?.tags || entryTags.size > 0) { | ||
| mergedCacheHint.tags = [...(cacheHint?.tags || []), ...entryTags]; | ||
| // Merge and dedupe tags | ||
| mergedCacheHint.tags = [...new Set([...(cacheHint?.tags || []), ...entryTags])]; | ||
| } | ||
| if (cacheHint?.maxAge !== undefined || minMaxAge !== undefined) { | ||
| mergedCacheHint.maxAge = | ||
| cacheHint?.maxAge !== undefined && minMaxAge !== undefined | ||
| ? Math.min(cacheHint.maxAge, minMaxAge) | ||
| : (cacheHint?.maxAge ?? minMaxAge); | ||
| } | ||
| if (cacheHint?.lastModified && latestModified) { | ||
| mergedCacheHint.lastModified = | ||
| cacheHint.lastModified > latestModified ? cacheHint.lastModified : latestModified; | ||
| } else if (cacheHint?.lastModified || latestModified) { | ||
| mergedCacheHint.lastModified = cacheHint?.lastModified ?? latestModified; | ||
| } | ||
|
Comment on lines
+567
to
+572
Member
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. Out of curiosity, do timezones have a role in this logic?
Contributor
Author
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. No, they're Date objects so it compares the timestamp |
||
| cacheHint = mergedCacheHint; | ||
| } | ||
| } | ||
|
|
||
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
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.
You might want to add a comment explaining why the use of
Setinstead of a simple arrayThere 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.
Good point (it's to dedupe them)