framework icon indicating copy to clipboard operation
framework copied to clipboard

feat(nuxt): add `clearNuxtCache`

Open cawa-93 opened this issue 3 years ago • 3 comments

🔗 Linked issue

https://github.com/nuxt/framework/discussions/4636

❓ Type of change

  • [ ] 📖 Documentation (updates to the documentation or readme)
  • [ ] 🐞 Bug fix (a non-breaking change that fixes an issue)
  • [ ] 👌 Enhancement (improving an existing functionality like performance)
  • [x] ✨ New feature (a non-breaking change that adds functionality)
  • [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Resolves #4636

Added a simple helper function that deletes data from the internal cache by key. This method is useful if you want to invalidate the data fetching for another page.

📝 Checklist

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

cawa-93 avatar May 31 '22 11:05 cawa-93

Deploy Preview for nuxt3-docs canceled.

Name Link
Latest commit 2af6412f435aed0b94d84c35f4d5281d74accb37
Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/63187dccf2ae8a0009745824

netlify[bot] avatar May 31 '22 11:05 netlify[bot]

With the keys => { keys } change, implementation looks good to me! Would you mind also adding API docs page under Utils?

pi0 avatar Jun 01 '22 07:06 pi0

@pi0 I have already added 3.api/3.utils/clear-nuxt-cache.md and describe clearNuxtCache in 2.guide/2.features/5.data-fetching.md

cawa-93 avatar Jun 01 '22 08:06 cawa-93

Thanks for making PR @cawa-93. I've made few refactors to reflect last changes and also cleaning up shared state. Didn't had a change to try it please share feedback if last changes have any issues or could be improved :)

pi0 avatar Sep 07 '22 11:09 pi0

Uncovered use case: Remove cache for several unknown keys. Example: you have app with posts and filter for posts. Each query for posts saved in cache in uniq key, like posts.list-{param1:1}, posts.list-{param2:2}, posts.list-{param1:1,param2:3} (nuxt-trpc works llike that). In this case you may want clear data for all keys posts.list-*. May be clearNuxtData should also accept function to filter keys?

clearNuxtData(key => key.startsWith('posts.list'))

Or provide another method to get all keys in cache and filter it manually:

clearNuxtData(
  getNuxtDataKeys().filter(key => key.startsWith('posts.list'))
)

cawa-93 avatar Sep 07 '22 11:09 cawa-93

Filter support seems a good idea 👍🏼 When constructing _keys, we can check type and if is function, use Object.keys + filter function. Feel free to make a PR.

pi0 avatar Sep 07 '22 11:09 pi0