【问题标题】:When I refresh the react.js and next.js page, it gives me 404 error当我刷新 react.js 和 next.js 页面时,它给了我 404 错误
【发布时间】:2021-02-20 00:44:19
【问题描述】:

我有一个 React.js 和 Next.js 网站,它运行良好,直到我导出生产版本。当我在服务器上发布我的项目时,我遇到了这个问题:当我刷新页面时,它给了我 404 错误。

我找到了Next.js 404 error after refreshing the page 并设置了它的配置。但我的问题依然存在。

我所有的链接都是这样的:

import Link from 'next/link';
export default () => (
 <Link href="/profile">
   <a>profile</a>
 </Link>
)

【问题讨论】:

  • 您在哪里部署了您的应用程序?你在用next export吗?
  • 我已在共享主机上发布。是的,我正在使用下一个导出

标签: reactjs next.js


【解决方案1】:

对于静态 HTML 导出,您必须使用 exportPathMap 参考:https://nextjs.org/docs/api-reference/next.config.js/exportPathMap

module.exports = {
  exportPathMap: async function (
    defaultPathMap,
    { dev, dir, outDir, distDir, buildId }
  ) {
    return {
      '/': { page: '/' },
      '/profile': { page: '/profile' },
      '/p/hello-nextjs': { page: '/post', query: { title: 'hello-nextjs' } },
      '/p/learn-nextjs': { page: '/post', query: { title: 'learn-nextjs' } },
      '/p/deploy-nextjs': { page: '/post', query: { title: 'deploy-nextjs' } },
    }
  },
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多