Next.js Firebase Hosting 404 错误,除了 index.html

IT技术 reactjs firebase next.js
2021-05-27 16:08:53

我已经构建了一个 nextjs 应用程序,npm run build && npm run export并使用firebase deploy命令部署到firebase 在此之前,我已经firebase init在我的项目文件夹中使用了默认选项,例如。不是单页应用程序。

但是,在我访问 firebase 提供的 url 中的项目后,我看到了 index.html 的主页,但是每当我使用任何其他 slug 时,它都会抛出 404。为什么会发生这种情况?我已经包含了我的 firebase.json 文件,以防它可能有所帮助。

firebase.json

  "hosting": {
    "public": "out",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}
2个回答

根据规则,您可以让 Firebase Hosting 提供用户请求的确切文件。

要重写其它/所有URL到你的index.html,你需要一个添加重写规则到你firebase.json单页应用程序的典型重写规则可能如下所示:

"hosting": {
  // ...

  // Serves index.html for requests to files or directories that do not exist
  "rewrites": [ {
    "source": "**",
    "destination": "/index.html"
  } ]
}

对于想要将静态导出的 Next.js 应用程序部署到 Firebase 托管的每个人:

您必须将 "cleanUrls": true 添加到 firebase.json 中的托管配置中,如下所示:

"hosting": {
    "public": "build",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "cleanUrls": true,
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },

如果没有“cleanUrls”配置,用户必须导航到 https://example.com/login.html以便 Next.js 路由到登录页面。使用该参数,对https://example.com/login的 Web 请求将起作用。