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: implement force option for refreshing and triggeredBy for getCachedData #25850

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

manniL
Copy link
Member

@manniL manniL commented Feb 18, 2024

πŸ”— Linked issue

Resolves #24332.

And linking #24271 for prep work

❓ 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

This PR adds a force option to to refresh function (from useAsyncData) and a force option to refreshNuxtData. It allows to bypass a possibly existing getCachedData function and forces to refetch.

As force cannot be set when watching params, e.g. through useFetch or manually in useAsyncData, getCachedData now receives the "context" that triggered the function and requests the cached data, allowing more granular results.

Happy for feedback on the implementation.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have added tests (if possible).
  • I have updated the documentation accordingly.

Copy link

stackblitz bot commented Feb 18, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@manniL manniL marked this pull request as ready for review February 18, 2024 16:53
@DrJume
Copy link

DrJume commented Feb 18, 2024

To change the behaviour of cache usage when using the watch parameter in useFetch/useAsyncData I have a proposition for an advanced variant using a one-time called function, which can return normal watch deps and setup other watchers with different cache behaviour:

const { data, refresh } = await useFetch('https://icanhazdadjoke.com/', {
  query: { page },
  // overrides default behaviour
  watch: (fetchOptions, { noCache, customCache }) => { // fetchOptions contain e.g. query
    noCache([page]) // -> watches and internally calls refetch({force:true})
    customCache([], 'custom') // -> refetch({cache: 'custom'})
    return [fetchOptions] // returned normal watch deps
  },
  getCachedData: (key, cache) => nuxt.payload.data[key] || nuxt.static.data[key],
  headers: {
    Accept: 'application/json',
  },
});

For this variant, the default could be using getCachedData for all changes to the returned deps, preventing breaking changes to current behaviour.

Additionally, information could be passed on to getCachedData with a custom cache name/label for specific watch deps.

With this, cache behaviour (watch) and cache retrieval (getCachedData) would be separate.

I hope this makes sense.

*/
getCachedData?: (key: string) => DataT
getCachedData?: (key: string, triggeredBy?: GetCachedDataTriggeredBy) => DataT
Copy link
Member

@danielroe danielroe Mar 8, 2024

Choose a reason for hiding this comment

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

in honesty I'm not yet sure about this as an API. But I also might be missing something.

What is the situation where passing force wouldn't be good enough?

Copy link
Member Author

Choose a reason for hiding this comment

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

In a scenario where you can't set force because watch is triggered - #24332 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

on the force option for refresh, could we just add the force option and basically ignore hasCachedData when forced? And always respect hasCachedData when calling refresh from watch?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will update the PR πŸ‘

@manniL manniL marked this pull request as draft March 14, 2024 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re-use getCachedData when refreshing asyncData
3 participants