当我刷新react.js和next.js页面时,它给我404错误

时间:2020-11-07 14:48:00

标签: reactjs next.js

我有一个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>
)

1 个答案:

答案 0 :(得分:0)

对于静态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' } },
    }
  },
}