【问题标题】:Get "This page could not be found." on refresh page - using Next Js and React-admin获取“找不到此页面。”在刷新页面上 - 使用 Next Js 和 React-admin
【发布时间】:2022-01-12 20:19:37
【问题描述】:

上下文 我目前已经使用 React-admin 和 NextJS 创建了一个项目。

问题 每当我刷新页面时,我都会被重定向到 404 页面 - 它是在我从 URL (localhost:3000/#/) 中删除哈希后开始的。只有当我在 localhost:3000 页面时它才能正常工作。

代码:

// into src/admin/ReactAdmin.tsx file
import { Admin, Resource } from 'react-admin'
import { createBrowserHistory as createHistory } from 'history'

const history = createHistory()

import {
  RestrictionList,
  RestrictionEdit,
  RestrictionCreate,
} from './components/restricao/'
import baseDataProvider from './DataProvider'

const App = () => (
  <Admin dataProvider={baseDataProvider} history={history}>
    <Resource
      name='source'
      list={RestrictionList}
      edit={RestrictionEdit}
      create={RestrictionCreate}
    />
  </Admin>
)

export default App

//into next.config.js file
module.exports = {
  trailingSlash: true,
}

// into src/pages/index.tsx file
import dynamic from 'next/dynamic'

const ReactAdmin = dynamic(() => import('../admin/ReactAdmin'), {
  ssr: false,
})

const HomePage = () => <ReactAdmin />

export default HomePage

【问题讨论】:

  • 遇到过类似的问题,你用的是什么版本的nextjs? check this similar issue and possible solutions
  • 嗨@shammahk,感谢分享。我正在使用“^12.0.8”。这是最新的。
  • 在开发模式下是否会出现此问题?
  • 没错,@juliomalves。它在本地主机中。

标签: reactjs next.js react-admin


【解决方案1】:

您是否在构建中使用了下一个导出? 如果是,则需要在 next.config.js 中添加此配置:

async rewrites() {
    return [
      {
        source: '/:any*',
        destination: '/',
      },
    ]
  }

【讨论】:

  • next export 不支持重写,不过 (nextjs.org/docs/advanced-features/…)。您将无法将其与静态 HTML 导出一起使用。您可能希望在回答中完全省略对 next export 的引用。
猜你喜欢
  • 2019-12-24
  • 2021-08-15
  • 2012-12-19
  • 2018-05-02
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 2022-07-31
  • 1970-01-01
相关资源
最近更新 更多