简体   繁体   English

页面在初始页面加载时有效,但如果我刷新页面,我会收到 Zeit、Nextjs 和现在的 404 错误

[英]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(如果存在)。

now.json现在.json

{
  "version": 2,
  "builds": [
    { "src": "next.config.js", "use": "@now/next" }
  ],
}

Folder structure文件夹结构

在此处输入图像描述

next.config.json next.config.json

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:)但是,是的,我被困住了,我会永远感激一些帮助:)

Package.json Package.json

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.

相关问题 AngularJS在页面刷新时收到错误404 - Angularjs get error 404 while page refresh 我使用 jquery 更改(重写)URL,但是当用户或我刷新页面时,我们收到 404 not found 错误 - I change (rewrite) URL with jquery but when user or I refresh the page, we get 404 not found error 页面加载404错误 - 404 error on page load Zeit Now 路由问题。 重新加载索引页以外的页面时出现 404 NOT FOUND - Zeit Now routing problem. 404 NOT FOUND when reloading pages other than index page nextjs 从 404 之后的页面返回到 404 之前的页面 - nextjs get back to page before 404 from page after 404 Cookie with static NextJS 导出页面刷新返回初始值 - Cookie with static NextJS export return to initial value on page refresh 除非我专门刷新页面,否则脚本在 React、NextJS 项目中不起作用 - Scripts not working in React, NextJS project unless I specifically refresh the page 为什么选择初始加载页面时出现错误? - Why do I get an error when choosing the initial loading page? jQuery函数无法在页面加载时触发,仅在刷新页面或再次重新访问同一页面后才起作用 - JQuery Function not firing on page load it works only after i refresh page or re-visit the same page again Rails 5-Google Charts仅在页面刷新时显示,而不在初始页面加载时显示 - Rails 5 - Google Charts only displaying on page refresh, not initial page load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM