You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Firebase部署React应用后点击链接出现404页面问题排查

Firebase部署React单页应用后非首页路由返回404的问题与解决

问题现象

React应用部署到Firebase后,登录页面可正常访问,但点击其他页面链接时返回404错误。本地使用npm start运行应用完全正常,通过firebase serve本地模拟部署也无此问题。

部署配置信息

Firebase CLI初始化交互记录

? What do you want to use as your public directory? build
? Configure as a single-page app (rewrite all urls to /index.html)? Yes
? Set up automatic builds and deploys with GitHub? No
? File build/index.html already exists. Overwrite? No

firebase.json配置

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

解决步骤

  • 先通过npm start确认本地应用运行正常,无路由问题
  • 执行npm run build重新构建生产包
  • 重新执行firebase deploy完成部署
  • 访问出现404的页面(如/home),手动刷新页面后恢复正常访问

推测原因

初步推测是Firebase部署后,路由重写规则的生效存在延迟,导致刚部署完成时非首页路由无法正确映射到index.html,刷新后缓存更新,规则生效。

内容的提问来源于stack exchange,提问作者zhrgci

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 13:35:32