Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
/frameworkPublic archive

feat(nuxt): add clearNuxtData - #5227

Merged
pi0 merged 16 commits into
nuxt:mainfrom
cawa-93:feat/clearNuxtCache
Sep 7, 2022
Merged

feat(nuxt): add clearNuxtData#5227
pi0 merged 16 commits into
nuxt:mainfrom
cawa-93:feat/clearNuxtCache

Conversation

@cawa-93

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

Copy link
Copy Markdown
Contributor

🔗 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

netlifyBot commented May 31, 2022

Copy link
Copy Markdown

Deploy Preview for nuxt3-docs canceled.

NameLink
🔨 Latest commit2af6412
🔍 Latest deploy loghttps://app.netlify.com/sites/nuxt3-docs/deploys/63187dccf2ae8a0009745824

Comment threaddocs/content/3.api/3.utils/clear-nuxt-cache.md Outdated
Comment threadpackages/nuxt/src/app/composables/asyncData.ts Outdated
Comment threadpackages/nuxt/src/app/composables/asyncData.ts Outdated
@pi0pi0 added the enhancement New feature or request label May 31, 2022
@pi0

pi0 commented Jun 1, 2022

Copy link
Copy Markdown
Member

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

@cawa-93

Copy link
Copy Markdown
ContributorAuthor

@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
}
}

@sasial-devsasial-devJul 4, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[deleted two messages]

@pi0pi0 changed the title feat(nuxt): add clearNuxtCachefeat(nuxt): add clearNuxtDataSep 7, 2022
@pi0
pi0 merged commit b2f573f into nuxt:mainSep 7, 2022
@pi0

pi0 commented Sep 7, 2022

Copy link
Copy Markdown
Member

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
Copy Markdown
ContributorAuthor

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

pi0 commented Sep 7, 2022

Copy link
Copy Markdown
Member

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.

Comment threadpackages/nuxt/src/app/composables/asyncData.ts
@cawa-93
cawa-93 deleted the feat/clearNuxtCache branch September 7, 2022 11:48
This was referenced Sep 9, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

3.xenhancementNew feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@cawa-93@pi0@sasial-dev@danielroe