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

feat(nuxt3): useFetch with dynamic reactive request #3731

Merged
merged 4 commits into from
Mar 17, 2022
Merged

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Mar 17, 2022

πŸ”— Linked issue

❓ 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

The current version of useFetch, only allowed a static value for a request meaning we couldn't assign a dynamic value to the request URL.

This PR uses a computed value and watch option of useAsyncData (#3722) to automatically trigger refresh whenever request value changes. Main advantage over useAsyncData+fetch is that it automatically tracks dependencies after hydration since we can safely evaluate computed value on client-side without actually evaluating handler and making a double fetch.

This PR also improves key generation using unjs/ohash that can also safely hash function values as well for automatically generating fetch key. (however an explicit key is better idea!)

Previously: (still possible to directly pass a value)

const { data } = await useFetch('/api/hello')

New usage:

const count = ref(1)
const { data } = await useFetch(() => `/api/hello/${count.value}`)

Next steps:

As is also visible in the example demo, it is not possible to assign dynamic options to ohmyfetch. Therefore options like params cannot be dynamic. We could allow computed property, to support { url, ...options } in addition of current types (url ref, string, Request)

πŸ“ Checklist

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

@netlify
Copy link

netlify bot commented Mar 17, 2022

βœ”οΈ Deploy Preview for nuxt3-docs canceled.

πŸ”¨ Explore the source changes: f11cc9c

πŸ” Inspect the deploy log: https://app.netlify.com/sites/nuxt3-docs/deploys/62330e4174505a0008d20593

@pi0 pi0 added enhancement New feature or request nuxt3 labels Mar 17, 2022
@pi0 pi0 merged commit fb15082 into main Mar 17, 2022
@pi0 pi0 deleted the feat/use-fetch-reactive branch March 17, 2022 10:47
@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 nuxt3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants