(After having edited my original question many times over, I have decided to completely rework it as to make it more clear and succinct. This is in accordance with StackOverflow recommendations.)
The setup:
Create a new Nuxt 3 project:
pnpm dlx nuxi init test-project
cd test-project
pnpm i
pnpm run dev -o
Edit app.vue
like this:
<script setup>
const { data: thing } = useFetch("http://localhost:8000/things/1").then(res => {
console.log(res)
return res
})
</script>
<template>
<div>
<p>{{ thing }}</p>
</div>
</template>
I added the console.log-statement to track where fetching takes place.
No changes made to the config. It is my understanding that Nuxt 3 defaults to SSR.
The issue:
Two scenarios:
I. When making alterations to the code in app.vue
and thus triggering HMR thing
will contain the expected data from the API.
II. However, when refreshing the page in the browser thing
will be null. The paragraph-tag will be empty.
My observations:
- The same behavior applies for useAsyncData().
- Everytime I trigger HMR (scenario I.) the client makes a request to the API and successfully receives the data (as can be seen in the network tab of the developer tools).
- Whenever I refresh the page in the browser (scenario II.), I receive both one console log in the terminal and one in the browser console. Both contain an error with status code 500 and message "fetch failed ()". However, according to the network tab, no client-side request has been made.
- If I use
$fetch
instead, it will log the same error to the terminal. However, while the request fails server-side, it successfully tries again client-side and thus the API data will be displayed on the page.
My conclusions so far:
It seems to me, that all server-side requests fail and all client-side requests succeed.
When $fetch
fails server-side, it throws an error and tries again on client-side. This is what I'd expect. However, useFetch
and useAsyncData
don't behave this way. Instead, the error is written to the response object and no client-side request is made. (This is not a major problem though, since I can check the error-entry in the response object and make another request on client-side if needed.)
Open questions:
Why do all server-side requests fail? I understand that the Nuxt-server does not have access to the browser and therefore cookies and such. But these are simple GET requests to a locally hosted Laravel API that doesn't require authentication. They shouldn't need CSRF- or session-cookies to be successful. I can make a successful request to the API route with Postman without cookies.
Is this due to me doing something wrong? Or is this expected behavior in development mode (pnpm run dev
)? I never had this issue with Nuxt 2.
I am afraid, I am missing something obvious.
Thank You!
"nuxt": "3.0.0-rc.12",
sometimesuseFetch()
returnsnull
– RailleryuseFetch
on one line, then do my intended request second, the first will be null and the seconduseFetch
will be valid. Adding in a 100ms delay (setTimeout
) allows theuseFetch
to execute. Seems to be a timing thing. – Slightly