-
Notifications
You must be signed in to change notification settings - Fork 0
Cache components manual #1030
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
Cache components manual #1030
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a688841
Remove all 'legacy' caching/memoization, to prepare for cacheComponents
oBusk 0a78f23
Enable cacheComponents
oBusk 5269c28
Remove runtime configuration as it's not a thing with cacheComponents
oBusk 4c86bff
Render the diff page in Suspense to make cacheComponents happy
oBusk 23e5ccc
Make header render without blocking
oBusk 3e27eea
Make about/api static ('use cache')
oBusk 76f1d28
Fix indexpage not building by making it static (use cache)
oBusk e8a68ae
Opt into caching where appropiate
oBusk 6d8119b
Fix canonicalSpec failing to cache because function not async
oBusk fdc51f8
Bundlephobia caching and error handling
oBusk 0af74df
Packagephobia caching and error handling
oBusk c0be266
Fix tests failing when functions contain cacheLife
oBusk e9fc10c
Improve caching for versionData
oBusk 9ce7a45
Try to fix suspense key errors
oBusk 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import { unstable_cache } from "next/cache"; | ||
| import { cache } from "react"; | ||
| import { cacheLife } from "next/cache"; | ||
| import { createSimplePackageSpec } from "^/lib/createSimplePackageSpec"; | ||
| import type SimplePackageSpec from "^/lib/SimplePackageSpec"; | ||
| import { simplePackageSpecToString } from "^/lib/SimplePackageSpec"; | ||
| import packument from "./packument"; | ||
|
|
||
| // Packuments include a lot of data, often enough to make them too large for the cache. | ||
|
|
@@ -17,17 +16,21 @@ export type VersionMap = { | |
| [version: string]: VersionData; | ||
| }; | ||
|
|
||
| async function getVersionDataInner( | ||
| spec: string | SimplePackageSpec, | ||
| ): Promise<VersionMap> { | ||
| const specString = | ||
| typeof spec === "string" ? spec : simplePackageSpecToString(spec); | ||
| /** | ||
| * Separate function that takes only packagename for better caching. | ||
| * | ||
| * We want `[email protected]` and `[email protected]` to share the same cache entry for `a`. | ||
| */ | ||
| async function getVersionMap(packageName: string): Promise<VersionMap> { | ||
| "use cache"; | ||
|
|
||
| cacheLife("hours"); | ||
|
|
||
| const { | ||
| time, | ||
| "dist-tags": tags, | ||
| versions, | ||
| } = await packument(specString, { | ||
| } = await packument(packageName, { | ||
| // Response is too large to cache in Next's Data Cache; always fetch | ||
| cache: "no-store", | ||
| }); | ||
|
|
@@ -52,13 +55,13 @@ async function getVersionDataInner( | |
| return versionData; | ||
| } | ||
|
|
||
| const getVersionData = | ||
| // Cache for request de-dupe | ||
| cache( | ||
| // unstable cache to cache between requests (5 minute TTL) | ||
| unstable_cache(getVersionDataInner, ["versionData"], { | ||
| revalidate: 300, | ||
| }), | ||
| ); | ||
| async function getVersionData( | ||
| spec: string | SimplePackageSpec, | ||
| ): Promise<VersionMap> { | ||
| const { name } = | ||
| typeof spec === "string" ? createSimplePackageSpec(spec) : spec; | ||
|
|
||
| return getVersionMap(name); | ||
| } | ||
|
|
||
| export default getVersionData; | ||
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.
Add a JSDoc comment explaining that this component is used as a Suspense fallback for NavLink, which reads from client state (usePathname) that may not be available during SSR.