N
Nuxt4mo ago
Tumulte

How do you refresh a fetch with a dynamic path (/api/page/[id])

I have a hard time understanding that doc https://nuxt.com/docs/getting-started/data-fetching#computed-url Basically I have a fetch {data, refresh} = useFetch("/api/${dynamicID}") (<- I know it’s suposed to be ) but when I refresh the dynamicID doesn’t change which is normal... but what’s the workaround (note that I can’t use watch because it’s on immediate: false` and I need to refresh it on cue) ?
Nuxt
Data fetching · Get Started with Nuxt
Nuxt provides composables to handle data fetching within your application.
8 Replies
Cake
Cake4mo ago
what's dynamicID?
Tumulte
TumulteOP4mo ago
it’s an url parameter /api/[id].ts (and a ref in the component)
Cake
Cake4mo ago
u'd do something like
$fetch(() => `/api/${myRef.value}`)
$fetch(() => `/api/${myRef.value}`)
Tumulte
TumulteOP4mo ago
yeah but it doesn’t work on refresh whatever value is set when the component is instanciated will be triggered
CS
CS4mo ago
I think useAsyncData would do the trick, where your call function uses what Cake commented here earlier
Tumulte
TumulteOP4mo ago
I haven’t tried useAsync yet but ()=> doesn’t change the outcome
Cue
Cue4mo ago
You're doing useFetch("/api/${dynamicID}") whilst claiming dynamicId is a ref. That means it should be:
useFetch(() => `/api/${dynamicID.value}`)
useFetch(() => `/api/${dynamicID.value}`)
But you're stating this doesn't change the outcome. You also mention the presence of immediate: false, and later state it does not work on refresh – which is expected behaviour? Can you provide more input here? Possible a repro @ https://nuxt.new?
Zampa
Zampa4mo ago
Nuxt
refreshNuxtData · Nuxt
refreshNuxtData refetches all data from the server and updates the page.
Want results from more Discord servers?
Add your server