Next js:在后退按钮上单击页面内容未加载

nin*_*des 7 reactjs next.js

问题:在 React + nextjs 应用程序中

  1. 如果我使用 Link 从http://localhost:3000导航到http://localhost:3000/p/hello
  2. 刷新页面。
  3. 单击后退按钮:它会转到http://localhost:3000但仍然呈现http://localhost:3000/p/hello的内容,它不会加载http://localhost:3000的内容。

我正在使用“下一个”:“^8.1.0”。

导航链接

        <div>
            Click{' '}
            <Link as={`/p/hello`} href={`/postDetails?title=hello`}>
                <a>here</a>
            </Link>
            to read more
        </div>
Run Code Online (Sandbox Code Playgroud)

服务器.js:

const express = require('express')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app
  .prepare()
  .then(() => {
    const server = express()
server.get('/p/:id', (req, res) => {
  const actualPage = '/postDetails'
  const queryParams = { title: req.params.id }
  app.render(req, res, actualPage, queryParams)
})

server.get('*', (req, res) => {
    //app.render(req, res, '/', queryParams)
  return handle(req, res)
})

server.listen(3000, err => {
  if (err) throw err
  console.log('> Ready on http://localhost:3000')
})
  })

  .catch(ex => {
    console.error(ex.stack)
    process.exit(1)
  })
Run Code Online (Sandbox Code Playgroud)

它应该加载index.js(localost:3000)内容,但它显示http://localhost:3000/p/hello内容。

jsd*_*per -2

我认为您可能需要添加一个静态文件夹,其中您的公共资产(例如 html、图像、js)是:

server.use(express.static('public'))
Run Code Online (Sandbox Code Playgroud)

https://expressjs.com/en/starter/static-files.html