I am trying to use nuxt3s useFetch feature to fetch the pages content. Whenever the slug changes I want to refetch/refresh.
The docs (https://nuxt.com/docs/getting-started/data-fetching) tell me that:
- the URL is auto-watched, so a refresh should always happen if my URL changes
- that I could use the
watch
property within useFetch to refresh
Both does not work. I even tried to watch the slug with a watcher and trigger a refresh
. No success either. Somehow the whole await useFetch()
is just never called anymore...
Here is my code:
<script setup>
const runTimeConfig = useRuntimeConfig()
// Tried to use this, but I don't really understand what that key is for:
// definePageMeta({
// key: (route) => route.params.slug,
// })
const route = useRoute()
const slug = computed(() => route.params.slug)
const endpoint = `${runTimeConfig.public.API_URL}/wp/v2/pages?slug=${slug.value}`
console.log('endpoint: ', endpoint)
const {
data: page,
pending,
error,
refresh,
} = await useFetch(endpoint, {
key: `key-${slug.value}`,
initialCache: false,
})
console.log('page: ', toRaw(page.value))
Now if I change the slug, I can see that the endpoint log is appearing with the correct URL. But if I log the page, after navigation it logs:page: null
...
Also that value is not updated later (first I thought the async await stuff is not working properly).
useFetch
refetch dynamic paramsuseRoute().params
~/pages/test-params/[id].vue
~/server/api/params.get.ts
useFetch
refetch dynamic queryuseRoute().query
API route example
~/server/api/slug.get.ts