呼唤远方
如果您真的想将查询参数用于分页(类似/foo?page=2)和 SSG,我刚刚想出了一个解决方法。您可以使用 Next JSrewrites和redirects功能。首先,使用/foo/[page].js文件格式并静态生成所有页面,正如hangindev.com 解释的那样。然后,在next.config.js文件中,您必须导出两个函数:async redirects()和async rewrites().module.exports = { .... async redirects() { return [ { source: "/foo/1", destination: "/foo?page=1", permanent: true, }, { source: "/foo/2", destination: "/foo?page=2", permanent: true, }, { source: "/foo/3", destination: "/foo?page=3", permanent: true, }, ]; }, async rewrites() { return [ { source: "/foo", has: [{ type: "query", key: "page", value: "1" }], destination: "/foo/1", }, { source: "/foo", has: [{ type: "query", key: "page", value: "2" }], destination: "/foo/2", }, { source: "/foo", has: [{ type: "query", key: "page", value: "3" }], destination: "/foo/3", }, ]; }, };该redirects()功能确保用户无法看到/foo/2格式的页面,因为他们被重定向到/foo?page=2. 该rewrites()函数显示URL的/foo/2页面内容。/foo?page=2