简体   繁体   English

Nuxt 3 SSR:useFetch() 在页面刷新时返回 null

[英]Nuxt 3 SSR: useFetch() returning null on page refresh

(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 .) (在多次编辑我的原始问题之后,我决定完全修改它以使其更加清晰和简洁。这符合StackOverflow 的建议。)

The setup:设置:

Create a new Nuxt 3 project:创建一个新的 Nuxt 3 项目:

pnpm dlx nuxi init test-project
cd test-project
pnpm i
pnpm run dev -o

Edit app.vue like this:像这样编辑app.vue

<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.我添加了 console.log-statement 来跟踪提取发生的位置。

No changes made to the config.没有对配置进行任何更改。 It is my understanding that Nuxt 3 defaults to SSR.据我了解,Nuxt 3 默认为 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. I. 当对app.vue中的代码进行更改并因此触发 HMR 时, thing将包含来自 API 的预期数据。

II.二。 However, when refreshing the page in the browser thing will be null. The paragraph-tag will be empty.但是,当在浏览器中刷新页面时, thing将是 null。段落标签将为空。

My observations:我的观察:

  • The same behavior applies for useAsyncData().相同的行为适用于 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).每次我触发 HMR(场景 I.)时,客户端都会向 API 发出请求并成功接收到数据(可以在开发人员工具的 .network 选项卡中看到)。
  • 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.每当我在浏览器中刷新页面时(场景 II.),我都会在终端和浏览器控制台中收到一份控制台日志。 Both contain an error with status code 500 and message "fetch failed ()".两者都包含状态代码为 500 的错误和消息“获取失败 ()”。 However, according to the.network tab, no client-side request has been made.但是,根据 .network 选项卡,没有发出客户端请求。
  • If I use $fetch instead, it will log the same error to the terminal.如果我改为使用$fetch ,它会将相同的错误记录到终端。 However, while the request fails server-side, it successfully tries again client-side and thus the API data will be displayed on the page.但是,当请求在服务器端失败时,它会在客户端再次尝试成功,因此 API 数据将显示在页面上。

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.$fetch在服务器端失败时,它会抛出错误并在客户端再次尝试。 This is what I'd expect.这是我所期望的。 However, useFetch and useAsyncData don't behave this way.但是, useFetchuseAsyncData不会以这种方式运行。 Instead, the error is written to the response object and no client-side request is made.相反,错误会写入响应 object,并且不会发出客户端请求。 (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.) (但这不是主要问题,因为我可以检查响应 object 中的错误条目,并在需要时在客户端发出另一个请求。)

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.我知道 Nuxt 服务器无法访问浏览器,因此无法访问 cookies 等。 But these are simple GET requests to a locally hosted Laravel API that doesn't require authentication.但这些是对本地托管的 Laravel API 的简单 GET 请求,不需要身份验证。 They shouldn't need CSRF- or session-cookies to be successful.他们不应该需要 CSRF 或会话 cookie 才能成功。 I can make a successful request to the API route with Postman without cookies.我可以成功请求 API 路由 Postman 没有 cookies。

Is this due to me doing something wrong?这是因为我做错了什么吗? Or is this expected behavior in development mode ( pnpm run dev )?或者这是开发模式下的预期行为( pnpm run dev )? I never had this issue with Nuxt 2.我从来没有遇到过 Nuxt 2 的这个问题。

I am afraid, I am missing something obvious.恐怕,我错过了一些明显的东西。

Thank You!谢谢你!

[UPDATE NODEJS] [更新节点]

I got the same error as you, my API is not protected and complete public, but also can't get data on SSR page load.我得到了和你一样的错误,我的 API 没有保护和完全公开,但也无法在 SSR 页面加载上获取数据。

It took me 1 day to search for the error, but maybe this is how to solve my problem, hope it helps you:我花了 1 天时间来查找错误,但也许这就是解决我的问题的方法,希望对您有所帮助:

Update the latest NodeJS LTS version here: https://nodejs.org/en/在此处更新最新的 NodeJS LTS 版本: https://nodejs.org/en/

I updated NodeJS version from 18.12 to 18.13 .我将 NodeJS 版本从18.12 更新到 18.13 And my problem was completely solved.而我的问题就彻底解决了。

I've run into the same issue, I did try a couple of the workarounds like setting a timeout or an arbitrary useFetch() (as suggested by Turukawa in comments) but success still varied.我遇到了同样的问题,我确实尝试了一些解决方法,比如设置超时或任意 useFetch() (正如 Turukawa 在评论中所建议的那样),但成功仍然各不相同。

I did find using $fetch instead seems to be stable and works as expected.我确实发现使用 $fetch 似乎很稳定并且可以按预期工作。

Edit: I had also tried updating to Node v 18.13.0, but it did not fix the issue.编辑:我也曾尝试更新到 Node v 18.13.0,但它没有解决问题。

wire417. wire417。

Try to change your API address from "localhost" to "127.0.0.1" in useFetch request.尝试在 useFetch 请求中将您的 API 地址从“localhost”更改为“127.0.0.1”。 It may help you.它可能对你有帮助。

Make sure your api is accessible.确保您的 api 可用。 You can add const { data, error } = useFetch(...) to see what happens.您可以添加const { data, error } = useFetch(...)来查看会发生什么。

nuxt 3.0.0 will still work properly with your app nuxt 3.0.0 仍将与您的应用程序一起正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM