-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
feat(nuxt): custom cache support for data fetching composables #20747
feat(nuxt): custom cache support for data fetching composables #20747
Conversation
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Daniel Roe <daniel@roe.dev>
β¦fo on composables
Β Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
β Live Preview ready!
|
// Setup nuxt instance payload | ||
const nuxt = useNuxtApp() | ||
|
||
const getCachedData = () => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key] | ||
const getCachedData = () => (nuxt.isHydrating || options.forcePayloadExtraction) ? nuxt.payload.data[key] : nuxt.static.data[key] |
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.
I'm reluctant to merge this PR as it's really a feature to work around a bug.
What about instead supporting a cache
/store
option that defaults to nuxt.isHydrating ? nuxt.payload.data : nuxt.static.data
? Then this could be set to a custom location, or forced to nuxt.payload.data
, which would have the same effect as forcing payload extraction.
It could be part of #15445.
Thoughts @pi0?
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.
More generic cache
option with better defaults sound reasonably better alternative approach.
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.
Would it be possible to update this instead to take the cache
/store
option? π
Thoughts welcome on best API.
π Linked issue
#20476
resolves #15445
β Type of change
π Description
Resolves #20476
This adds support for custom caching within
useAsyncData
anduseFetch
. It works by providing agetCachedData
function via the options which receives a key and can return cached data if it exists. For example, to make all requests cached by default, the following options could be used:π Checklist