Vercel + Reactjs & Vite 刷新页面时返回 404。

4

我有一个基于React、TailwindCSS和Vite构建的代码库,使用react router进行路由。

出现问题的是,在构建时,访问主页和其余页面都能正常显示,但一旦刷新,就会出现404错误。

我已经检查了我的vercel.json文件。我尝试了其他一些解决方案,但不确定哪里出错了。我试过在我的文件夹根目录(当前位置)和我的src文件夹内添加此文件,因为我看到一些示例也是这样。

我已经尝试过更改

routes: [
{ 
 "src" :  //here
 }
 ]

根据我看到的示例,但是没有运气!

让我知道我需要做什么或者如果您想查看其他文件,但是源代码在这里可用。

https://github.com/InsurTech-Groups/home-form-english

谢谢!

附加信息

vercel.json

{
  "version": 2,
  "builds": [
    {
      "src": "package.json",
      "use": "@vercel/node",
      "config": {
        "maxLambdaSize": "75mb"
      }
    },
    {
      "src": "index.html",
      "use": "@vercel/static-build",
      "config": {
        "distDir": "dist",
        "command": "npm run build",
        "env": {
          "NODE_ENV": "production"
        },
        "output": {
          "clean": true
        },
        "postbuild": {
          "command": "npm run postbuild",
          "env": {
            "BUILD_DIR": "$VERCEL_BUILD_OUTPUT_DIR"
          }
        },
        "files": [
          "dist/**/*",
          "public/**/*",
          "src/**/*.{js,jsx,ts,tsx}",
          "!**/node_modules/**"
        ]
      }
    }
  ],
  "routes": [
    {
      "src": [{ "src": "/[^.]+", "dest": "/", "status": 200 }],
      "dest": "/index.html"
    }
  ]
}

链接 Github网址:https://github.com/InsurTech-Groups/home-form-english Vercel网站现场:https://home-form-english.vercel.app

1个回答

5

看起来当我删除了vercel.json中的所有内容,只留下这个时,问题得到了解决

{
  "rewrites": [{ 
      "source": "/(.*)",
      "destination": "/" }]
}

任何在源代码中提到的路径 "/(.*)" 都将重写请求到 "/" 目标路径... 来源:Vercel 文档 (https://vercel.com/docs/projects/project-configuration#rewrites) - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接