Nuxt.js 生成的静态站点在 Firebase 托管上不会下降到 404,但在 Localhost 上工作

Nuxt.js Generated Static Site Not Falling to 404 on Firebase Hosting but works on Localhost

Nuxtjs 版本:2.14.5

我最近使用 nuxt generate 在 nuxt.config.js 中使用以下设置预渲染了我的 nuxtjs 网站。

export default {
  ssr: true,
  target: 'static',
  generate: {
    fallback: true,
    exclude: [
      '/preview'
    ],
    routes() {
      return getRoutes();
    }
  },

404.html 出现在我的 /dist 文件夹中

在本地主机上(运行 nuxt 启动),每当我出错时 link,404 页面都会按预期显示。

我已经将我的静态网站部署到 Firebase 托管,现在当我访问一个错误的 link 时,404 页面没有显示,而是指向一个看起来有点糟糕的主页。

有谁知道为什么会这样或我可以查看什么?

我找到了解决办法, 基本上它与 firebase.json 文件和重写有关。 作为一个完全静态的网站,javascript 不处理导航,因为用户直接向 firebase 请求特定页面的 html 文件。 因此,我们需要设置一个重写,指定“对于所有断开的链接,服务 404.html”

https://firebase.google.com/docs/hosting/full-config#rewrites

在Firebase.json,

"rewrites": [
      {
        "source": "**",
        "destination": "/404.html"
      }
    ]

目的地需要设置为404.html,就是这样!