【问题标题】:React NextJS 502 BAD_GATEWAY when navigating directly to inside url直接导航到内部 url 时反应 NextJS 502 BAD_GATEWAY
【发布时间】:2020-07-05 14:24:17
【问题描述】:

我有一个反应应用程序,使用 NextJS 我的网页内部有很多链接,一切都很好

但是当我尝试导航到网站而不是主页时,我收到消息“502 BAD_GATEWAY”

例如: 浏览此链接: https://power-tools.co.il/ 然后单击右侧的第一个工具,您将成功导航到: https://power-tools.co.il/rentals/generators

但如果您尝试直接导航到相同的网址: https://power-tools.co.il/rentals/generators

你会得到错误:“502 BAD_GATEWAY”

为什么会这样以及如何解决?

这是生产中运行时错误的日志:

info  - Loaded env from .env.production
2020-07-06T07:20:37.771Z    undefined   ERROR   Uncaught Exception  {"errorType":"Error","errorMessage":"Must use import to load ES Module: /var/task/node_modules/flatted/cjs/index.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/node_modules/flatted/cjs/index.js from /var/task/.next/serverless/pages/rentals/[categoryID].js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.\nInstead rename index.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/node_modules/flatted/package.json.\n","code":"ERR_REQUIRE_ESM","stack":["Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/node_modules/flatted/cjs/index.js","require() of ES modules is not supported.","require() of /var/task/node_modules/flatted/cjs/index.js from /var/task/.next/serverless/pages/rentals/[categoryID].js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.","Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/node_modules/flatted/package.json.","","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1149:13)","    at Module.load (internal/modules/cjs/loader.js:977:32)","    at Function.Module._load (internal/modules/cjs/loader.js:877:14)","    at Module.require (internal/modules/cjs/loader.js:1019:19)","    at require (internal/modules/cjs/helpers.js:77:18)","    at Object.U3Ae (/var/task/.next/serverless/pages/rentals/[categoryID].js:1964:18)","    at __webpack_require__ (/var/task/.next/serverless/pages/rentals/[categoryID].js:23:31)","    at Object.C8l3 (/var/task/.next/serverless/pages/rentals/[categoryID].js:1306:25)","    at __webpack_require__ (/var/task/.next/serverless/pages/rentals/[categoryID].js:23:31)","    at Module.wN1p (/var/task/.next/serverless/pages/rentals/[categoryID].js:4487:30)"]}
Unknown application error occurred
Error

【问题讨论】:

  • 不仅在导航到某个 URL 时,服务器会以502 响应,它还会在除主页之外的所有页面的页面重新加载时以502 响应。这里没有足够的信息可以使用,请分享日志或自己检查日志并分享发生错误的部分。
  • 日志仅显示构建和部署日志,不显示尝试导航的日志
  • 该应用程序是否在本地运行?不是next dev,而是在构建应用程序之后。试试next build && next start,看看错误是否可重现。
  • 是的,在本地运行良好,也可以导航到直接内部 url
  • 我对此表示怀疑(我建议使用脚本next build && next start 再次检查),但是由于您确认该应用程序使用脚本next build && next start 在本地环境中完美运行,我认为错误不是可重现,仅仅猜测出了什么问题并不是一个好主意。另外由于没有日志,我建议联系 vercel 支持或创建一个 github 问题。

标签: javascript reactjs next.js vercel


【解决方案1】:

由于这是 React,我怀疑 Web 服务器没有正确处理您的应用程序的路由。

作为一种可能的解决方案,您能否确保在您的 package.json 中使用您网站的 URL 值定义了 "homepage"

"homepage": "https://power-tools.co.il/",

并尝试再次部署它?

【讨论】:

  • 我已经添加了日志
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
相关资源
最近更新 更多