Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): add clearNuxtData #5227

Merged
merged 16 commits into from
Sep 7, 2022
Merged

feat(nuxt): add clearNuxtData #5227

merged 16 commits into from
Sep 7, 2022

Conversation

cawa-93
Copy link
Contributor

@cawa-93 cawa-93 commented May 31, 2022

πŸ”— 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)
  • ✨ 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

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

@netlify
Copy link

netlify bot commented May 31, 2022

βœ… Deploy Preview for nuxt3-docs canceled.

Name Link
πŸ”¨ Latest commit 2af6412
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/63187dccf2ae8a0009745824

@pi0 pi0 added the enhancement New feature or request label May 31, 2022
@pi0
Copy link
Member

pi0 commented Jun 1, 2022

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

@cawa-93
Copy link
Contributor Author

cawa-93 commented Jun 1, 2022

@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

Comment on lines 215 to 223
for (const key of _keys) {
if (nuxt.payload.data[key] !== undefined) {
nuxt.payload.data[key] = undefined
}

if (nuxt.payload._errors[key] !== undefined) {
nuxt.payload._errors[key] = undefined
}
}
Copy link

@sasial-dev sasial-dev Jul 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[deleted two messages]

@pi0 pi0 changed the title feat(nuxt): add clearNuxtCache feat(nuxt): add clearNuxtData Sep 7, 2022
@pi0 pi0 merged commit b2f573f into nuxt:main Sep 7, 2022
@pi0
Copy link
Member

pi0 commented Sep 7, 2022

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 :)

@cawa-93
Copy link
Contributor Author

cawa-93 commented Sep 7, 2022

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'))
)

@pi0
Copy link
Member

pi0 commented Sep 7, 2022

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.

@cawa-93 cawa-93 deleted the feat/clearNuxtCache branch September 7, 2022 11:48
This was referenced Sep 9, 2022
@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3.x enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants