[英]Page works on initial page load, but if i refresh the page I get an 404 error with Zeit, Nextjs & now
I have recently upgraded from now v1
to v2
.我最近从now v1
升级到v2
。
Everything works locally with now dev
and all pages reload without any problems. now dev
可以在本地运行,所有页面都可以重新加载,没有任何问题。 However when I push to prod with now --prod
and navigate to a page everything works as expected, however if I reload the page I will take to the error page with an error of 404
但是,当我使用now --prod
推送到 prod 并导航到页面时,一切都按预期工作,但是如果我重新加载页面,我将进入错误页面,错误为404
I must admit i am finding the lines a little blurry on how I should setup my project since the move to v2
.我必须承认,自从迁移到v2
以来,我发现我应该如何设置我的项目的界限有点模糊。 I am so sure its to do with my routing... but I always thought it took the routes directly from my folder structure?我很确定它与我的路由有关......但我一直认为它直接从我的文件夹结构中获取路由?
To add to the confusion, if you reload, you will get a 404, however from that 404 error page, if you use the navigation the site will render without any problems.更令人困惑的是,如果您重新加载,您将收到 404,但是从该 404 错误页面中,如果您使用导航,该网站将毫无问题地呈现。
According to the Docs , it should pick up the name from the filesystem, which i believe it does, as it navigates there initially.根据文档,它应该从文件系统中获取名称,我相信它确实如此,因为它最初导航到那里。
By default, routing is defined by the filesystem of your deployment.默认情况下,路由由部署的文件系统定义。 For example, if a user makes a request to /123.png, and your now.json file does not contain any routes with a valid src matching that path, it will fallback to the filesystem and serve /123.png if it exists.例如,如果用户向 /123.png 发出请求,而您的 now.json 文件不包含任何具有与该路径匹配的有效 src 的路由,它将回退到文件系统并提供 /123.png(如果存在)。
{
"version": 2,
"builds": [
{ "src": "next.config.js", "use": "@now/next" }
],
}
const webpack = require('webpack')
const withCSS = require('@zeit/next-css')
module.exports = withCSS({
webpack: (config, {}) => {
const originalEntry = config.entry
config.entry = async () => {
const entries = await originalEntry()
if (entries['main.js'] && !entries['main.js'].includes('./polyfills.js')) {
entries['main.js'].unshift('./polyfills.js')
}
return entries
}
return config
},
env: {
env: process.env.NODE_ENV,
},
})
But yes, I am stuck and would be eternally grateful for some help:)但是,是的,我被困住了,我会永远感激一些帮助:)
I am unsure if this is helpful to know for the problem, but here is my scripts.我不确定这是否有助于了解问题,但这是我的脚本。
"scripts": {
"build": "next build",
"dev:inspect": "NODE_ENV=development nodemon --inspect server.js",
"start": "now dev",
"test": "echo \"Error: no test specified\" && exit 0",
"lint-scripts": "eslint './{Frontend/**,test/**,config/**,.}/*.js'",
"lint-styles": "stylelint './Frontend/**/*.css'",
"eslint": "eslint . --fix",
"lint": "npm run lint-scripts && npm run lint-styles",
"commit": "npx git-cz"
},
[Edit] [编辑]
"routes": [
{"src": "/index", "dest": "/" },
{"src": "/charts", "dest": "/charts" }
]
Make a vercel.json and paste the redirect rules code.制作一个 vercel.json 并粘贴重定向规则代码。 Save the file in your root directory, then redeploy.将文件保存在根目录中,然后重新部署。
{
"routes": [{ "src": "/[^.]+", "dest": "/", "status": 200 }]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.