简体   繁体   English

Next.js Firebase 除索引外的所有主机都出现 404 错误。html

[英]Next.js Firebase Hosting 404 error on all except index.html

I've built a nextjs app, with npm run build && npm run export and deployed to firebase using firebase deploy command.我已经构建了一个 nextjs 应用程序,使用npm run build && npm run export并使用firebase deploy命令部署到 firebase。 Prior to that, I've used firebase init in my project folder with just using the default options eg.在此之前,我在我的项目文件夹中使用了firebase init ,仅使用默认选项,例如。 not a single page application.不是单页应用程序。

After I go and visit my project in firebase provided url however, I see the home page which is index.html, but whenever I use any other slug it throws a 404. Why this is happening? After I go and visit my project in firebase provided url however, I see the home page which is index.html, but whenever I use any other slug it throws a 404. Why this is happening? I`ve included my firebase.json file, in case it might help.我已经包含了我的 firebase.json 文件,以防它可能有帮助。

firebase.json firebase.json

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

With the rules you have Firebase Hosting serves the exact file that the user requested.使用您拥有的规则 Firebase 托管服务于用户请求的确切文件。

To rewrite other/all URLs to your index.html , you'll need to add a rewrite rule to your firebase.json .要将其他/所有 URL 重写为index.html ,您需要向firebase.json添加 重写规则 A typical rewrite rule for single-page applications may look like this:单页应用程序的典型重写规则可能如下所示:

"hosting": {
  // ...

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

For everybody that wants to deploy a statically exported Next.js app to Firebase hosting:对于想要将静态导出的 Next.js 应用程序部署到 Firebase 主机的每个人:

You would have to add "cleanUrls": true to the hosting configuration in firebase.json like so:您必须将 "cleanUrls": true 添加到 firebase.json 中的托管配置中,如下所示:

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

Without the "cleanUrls" configuration, the user would have to navigate to https://example.com/login.html so that Next.js routes to the login page for example.如果没有“cleanUrls”配置,用户将不得不导航到https://example.com/login.html以便 Next.js 路由到登录页面。 With the parameter, a web request to https://example.com/login would work.使用该参数,对https://example.com/login的 web 请求将起作用。

If anyone is still looking for this, this is what fixed it for me:如果有人还在寻找这个,这就是为我解决的问题:

I used dynamicLinks as stated in the firebase hosting docs for the rewrites like so in my firebase.json file:我使用 了 firebase 托管文档中所述的 dynamicLinks进行重写,就像我的firebase.json文件中一样:

{
  "hosting": {
    "public": "out",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "cleanUrls": true,
    "rewrites": [
      {
        "source": "/**", 
        "dynamicLinks": true
      }
    ]
  }
}

This should allow dynamicLinks to start at ("https://CUSTOM_DOMAIN/{dynamicLink}").这应该允许动态链接从 ("https://CUSTOM_DOMAIN/{dynamicLink}") 开始。

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

相关问题 Firebase 托管。 Firebase Deploy 只部署 index.html - Firebase Hosting. Firebase Deploy only deploys index.html Firebase托管基于域的动态索引.html - Firebase hosting dynamic index.html based on domain 如何更改 index.html(firebase 托管)以显示项目内容? - How to change index.html (firebase hosting) to show project content? Firebase 托管“重写”以访问 Google Cloud Run 上的 Next.js 应用程序 - Firebase Hosting "rewrites" to Access Next.js App on Google Cloud Run Firebase 托管 – 无法将默认流量重写为 index.html 以外的任何其他内容 - Firebase Hosting – Cannot rewrite default traffic to anything else but index.html 下一个动态路线 404 firebase 托管 - next dynamic routes 404 firebase hosting 了解将 next.js 与 firebase 托管集成时如何计算服务器时间? - understanding how server-time is calculated when integrating next.js with firebase-hosting? Firebase:此文件不存在且没有索引。在当前目录中找到html或在根目录中找到404.html - Firebase: This file does not exist and there was no index.html found in the current directory or 404.html in the root directory Next.js 上 Firebase 功能与 Typescript - Next.js on Firebase Functions with Typescript Firebase 使用 Next.js 推送通知 - Firebase Push Notifications with Next.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM