admin管理员组文章数量:1356866
Nextjs has a static-site generation (SSG) feature that makes it possible to fetch data at build time and therefore generate already rendered pages (using getStaticProps
and getStaticPaths
).
Now let's say I have a blog with lots of articles that do not change, but some might get updated from time to time (especially the recent ones). I enable SSG on these articles.
Does it mean that everytime I update an article I have to rebuild the whole thing with tons of articles?
Or is there a way to tell Nextjs ≪ build only /article/[slug]
as /article/123-my-title
page ≫ ?
Nextjs has a static-site generation (SSG) feature that makes it possible to fetch data at build time and therefore generate already rendered pages (using getStaticProps
and getStaticPaths
).
Now let's say I have a blog with lots of articles that do not change, but some might get updated from time to time (especially the recent ones). I enable SSG on these articles.
Does it mean that everytime I update an article I have to rebuild the whole thing with tons of articles?
Or is there a way to tell Nextjs ≪ build only /article/[slug]
as /article/123-my-title
page ≫ ?
2 Answers
Reset to default 5You should check here the doc they show an example.
Today, we are also introducing Incremental Static Regeneration (beta), which is a mechanism to update existing pages, by re-rendering them in the background as traffic es in. Inspired by stale-while-revalidate, this ensures traffic is served uninterrupted, always statically, and the newly built page is pushed only after it's done generating.
Yes, you can do it using pageExtensions
In the current org, we have a mono repo Next JS project where we build multiple different websites (each having its own build process), accessible under the same domain but under different paths.
Let's say you need 3 different sets of files to be built, but not the entire app at once (if needed the entire app also can be built at once). You will need to categorize them by naming or renaming your files explicitly with a mon extension (Eg: .ila.tsx, .msr.tsx). You can then conditionally build only those files that match a particular extension name.
In next.config.js:
const nextConfig = {
output: 'export',
pageExtensions: (() => {
const { NEXT_PUBLIC_BUILDTYPE } = process.env;
switch (NEXT_PUBLIC_BUILDTYPE) {
case 'ila':
return ['ila.tsx', 'ila.ts', 'ila.js', 'ila.jsx'];
case 'msr':
return ['msr.tsx', 'msr.js', 'msr.js', 'msr.jsx'];
default:
return ['tsx', 'ts', 'jsx', 'js'];
}
})()
}
Notice that we have an IIFE as the value for the property 'pageExtensions' which allows us to conditionally select the type of files we want to build. Here, we pass an ENV variable to decide that.
Good to know: Fortunately, the default case in the switch block makes sure that only those files ending with .tsx, .ts, .jsx, or .js explicitly are built and will not include .ila.tsx, .msr.js, etc although those files appear to match the one mentioned in the default case.
本文标签: javascriptIs there a way to build a specific page with Nextjs using SSGStack Overflow
版权声明:本文标题:javascript - Is there a way to build a specific page with Nextjs using SSG? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744046597a2581639.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论