如果我从 index.js 页面单击关于链接,我的 url 看起来'/about/1/'和工作正常,但如果我刷新页面,我会收到错误 404。如果我在浏览器中输入/abouts?id=2"并刷新页面,一切正常。你知道我该如何解决这个问题吗?我想要像这样的链接
/about/1/
第二个
as为参数push,并replace是一个可选装饰的URL。如果您在服务器上配置了自定义路由,则很有用。
因此,要实现此行为,您需要在您的内部配置自定义路由server.js——例如使用express. 用这个扩展你的服务器:
const next = require('next');
const express = require('express');
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
// Nice permalinks for pages.
// Sets up a custom route, that then uses next.js to render the about page.
server.get('/about/:id', (req, res) => {
const params = { id: req.params.id };
return app.render(req, res, '/about', params);
});
// For all other routes, use next.js.
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(port, '0.0.0.0', err => {
if (err) throw err;
console.log(`${'\u2705'} Ready on http://localhost:${port}`);
});
});