Skip to content
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

Merged
merged 27 commits into from
Oct 16, 2023

Conversation

darioferderber
Copy link
Contributor

@darioferderber darioferderber commented May 9, 2023

πŸ”— Linked issue

#20476
resolves #15445

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Resolves #20476

This adds support for custom caching within useAsyncData and useFetch. It works by providing a getCachedData 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:

const nuxtApp = useNuxtApp()
const { data } = await useAsyncData(() => { /* fetcher */ }, {
  // this will not refetch if the key exists in the payload
  getCachedData: key => nuxtApp.payload.static[key] || nuxtApp.payload.data[key]
})

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codesandbox
Copy link

codesandbox bot commented May 9, 2023

CodeSandbox logoCodeSandbox logoΒ  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@nuxt-studio
Copy link

nuxt-studio bot commented May 9, 2023

βœ… Live Preview ready!

Name Edit Preview Latest Commit
Nuxt Docs Edit on Studio β†—οΈŽ View Live Preview 65343da

// 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]
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

@danielroe danielroe left a 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.

@danielroe danielroe changed the title feat: add forcePayloadExtraction to useAsyncData and useFetch feat: allow passing custom store to data fetching composables Oct 16, 2023
@danielroe danielroe changed the title feat: allow passing custom store to data fetching composables feat(nuxt): custom cache support for data fetching composables Oct 16, 2023
@danielroe danielroe merged commit b52548d into nuxt:main Oct 16, 2023
35 of 36 checks passed
@github-actions github-actions bot mentioned this pull request Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants