admin管理员组文章数量:1313006
I have page which depends on route params (ex.: slug) like so /:slug
. This route path is defined properly in my next.config.js file:
module.exports = withPlugins(plugins, {
exportPathMap: (defaultPathMap) => {
return {
'/': { page: '/home/home' },
'/blog/:slug': { page: '/careers/careers' }
}
}
});
This works fine when running the project in dev mode but once i export the project as static the route is not accessible and i get the regular 404 error from next.
Is there a way to fix this without using query parameters?
/?slug=123
This solution .js/blob/canary/examples/with-static-export/next.config.js is also not acceptable since the posts e from backend CMS
I have page which depends on route params (ex.: slug) like so http://example./blog/:slug
. This route path is defined properly in my next.config.js file:
module.exports = withPlugins(plugins, {
exportPathMap: (defaultPathMap) => {
return {
'/': { page: '/home/home' },
'/blog/:slug': { page: '/careers/careers' }
}
}
});
This works fine when running the project in dev mode but once i export the project as static the route is not accessible and i get the regular 404 error from next.
Is there a way to fix this without using query parameters?
http://example./?slug=123
This solution https://github./zeit/next.js/blob/canary/examples/with-static-export/next.config.js is also not acceptable since the posts e from backend CMS
Share Improve this question edited Mar 25, 2019 at 14:16 prototype asked Mar 25, 2019 at 14:04 prototypeprototype 3,3132 gold badges29 silver badges43 bronze badges2 Answers
Reset to default 5This is not possible because Next.js static export generates, well, static html pages. If you think about it, for this to work Next.js would somehow have to export every possible bination of letters valid in a url segment, which is not a good idea at all.
The closest you could get is using query parameters and the as
attribute, for example when linking to a page:
<Link href='/blog/page?slug=SLUG_HERE' as='/blog/slug'>
// Link content here
</Link>
This only breaks when the user tries to link to or reload the page because there is no server-side support for the masking. You could theoretically use Nginx or Apache to proxy (is proxy the right word?) requests from /blog/SLUG_HERE
to /blog/page?slug=SLUG_HERE
. This is left up to you to figure out.
To handle dynamic paths in your next js project (provided you are going through the export route!).
- Ensure trailingSlash is set to false or not defined at all in your next.config.js file
This way, every request will land in the index ponent, and from here, you can just handle your path redirect.
if (window.location.pathname !== "/") {
Router.push(window.location.pathname + window.location.search);
}
Ensure your project is mounted before doing this (e.g do this with useEffect hook)
本文标签: javascriptNextjs dynamic page params for static exportStack Overflow
版权声明:本文标题:javascript - Next.js dynamic page params for static export - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741826654a2399683.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论