[英]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.