在刷新页面上获取 "This page could not be found." - 使用 Next Js 和 React-admin
Get "This page could not be found." on refresh page - using Next Js and React-admin
上下文
我目前已经使用 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
您是否在构建中使用了 next export?
如果是,您需要在 next.config.js:
中添加此配置
async rewrites() {
return [
{
source: '/:any*',
destination: '/',
},
]
}
上下文 我目前已经使用 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
您是否在构建中使用了 next export? 如果是,您需要在 next.config.js:
中添加此配置async rewrites() {
return [
{
source: '/:any*',
destination: '/',
},
]
}