Incremental Static Rendering

patternsdev/skills/react/incremental-static-rendering

作者 patternsdev48bf58a488cdMIT253 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 個月前更新

Teaches Incremental Static Regeneration (ISR) for updating static content post-build. Use when you have static pages that need periodic updates without a full site rebuild.

AI 產生的概覽

說明增量靜態再生(ISR),用於在建置後更新靜態頁面,而不必整站重新建置。

功能
這項技能是一份關於 Next.js 中增量靜態再生(也稱增量靜態生成)的教學指南。它說明何時適合與何時不適合採用這個模式,並提供範例程式碼,示範如何透過 fallback 延遲新增頁面,以及如何依時間間隔重新驗證既有頁面。它也列出這種做法的優點,例如支援動態資料、速度快、可用性高,以及可透過 CDN 分散提供。
適用情境
當你擁有以靜態內容為主、需要定期更新資料但不想重新建置整個網站時使用,例如大型部落格或電子商務型錄。它不適用於即時內容、完全個人化的頁面,或舊版本內容會讓使用者混淆的情況。
執行需求
不含指令碼,僅為說明性內容。它假定讀者熟悉 Next.js 的資料擷取 API,例如 getStaticProps、getStaticPaths、revalidate、fallback,以及 App Router 中的 generateStaticParams。

Incremental Static Generation

Static Generation (SSG) addresses most of the concerns of SSR and CSR but is suitable for rendering mostly static content. It poses limitations when the content to be rendered is dynamic or changing frequently.

Think of a growing blog with multiple posts. You wouldn't possibly want to rebuild and redeploy the site just because you want to correct a typo in one of the posts. Similarly, one new blog post should also not require a rebuild for all the existing pages. Thus, SSG on its own is not enough for rendering large websites or applications.

When to Use

  • Use this when you have mostly static pages that need periodic data updates without full rebuilds
  • This is helpful for large sites (blogs, e-commerce) where rebuilding every page on each change is impractical

When NOT to Use

  • When content changes in real-time and stale data is unacceptable (e.g., live scores, stock tickers)
  • For pages that are fully dynamic and personalized per user — SSR is a better fit
  • When the revalidation window creates a confusing experience where different users see different content versions

Instructions

  • Use revalidate in getStaticProps to set a time interval for background page regeneration
  • Use fallback: true in getStaticPaths to lazily generate pages on first request
  • Consider on-demand revalidation (revalidatePath, revalidateTag) for immediate updates after content changes
  • In Next.js 13+ App Router, use generateStaticParams and async components for ISR

Details

The Incremental Static Generation (iSSG) pattern was introduced as an upgrade to SSG, to help solve the dynamic data problem and help static sites scale for large amounts of frequently changing data. iSSG allows you to update existing pages and add new ones by pre-rendering a subset of pages in the background even while fresh requests for pages are coming in.

Sample Code

iSSG works on two fronts to incrementally introduce updates to an existing static site after it has been built.

  1. Allows addition of new pages
  2. Allows updates to existing pages also known as Incremental Static "Re"generation
Adding New pages

The lazy loading concept is used to include new pages on the website after the build. This means that the new page is generated immediately on the first request. While the generation takes place, a fallback page or a loading indicator can be shown to the user on the front-end.

js
export async function getStaticPaths() {  const products = await getProductsFromDatabase();
  const paths = products.map((product) => ({     params: { id: product.id }  }));
  // fallback: true means that the missing pages  // will not 404, and instead can render a fallback.  return { paths, fallback: true };}
// params will contain the id for each generated page.export async function getStaticProps({ params }) {  return {    props: {      product: await getProductFromDatabase(params.id)    }  }}
export default function Product({ product }) {  const router = useRouter();
  if (router.isFallback) {    return <div>Loading...</div>;  }
  // Render product}

Here, we have used fallback: true. Now if the page corresponding to a specific product is unavailable, we show a fallback version of the page, eg., a loading indicator as shown in the Product function above. Meanwhile, Next.js will generate the page in the background. Once it is generated, it will be cached and shown instead of the fallback page. The cached version of the page will now be shown to any subsequent visitors immediately upon request.

Update Existing pages

To re-render an existing page, a suitable timeout is defined for the page. This will ensure that the page is revalidated whenever the defined timeout period has elapsed. The user will continue to see the previous version of the page, till the page has finished revalidation. Thus, iSSG uses the stale-while-revalidate strategy where the user receives the cached or stale version while the revalidation takes place. The revalidation takes place completely in the background without the need for a full rebuild.

js
// This function runs at build time on the build serverexport async function getStaticProps() {  return {    props: {      products: await getProductsFromDatabase(),      revalidate: 60, // This will force the page to revalidate after 60 seconds    }  }}
// The page component receives products prop from getStaticProps at build timeexport default function Products({ products }) {  return (    <>      <h1>Products</h1>      <ul>        {products.map((product) => (          <li key={product.id}>{product.name}</li>        ))}      </ul>    </>  )}

The code to revalidate the page after 60 seconds is included in the getStaticProps() function. When a request comes in the available static page is served first. Every one minute the static page gets refreshed in the background with new data. Once generated, the new version of the static file becomes available and will be served for any new requests in the subsequent minute. This feature is available in Next.js 9.5 and above.

Advantages

iSSG provides all the advantages of SSG and then some more:

  1. Dynamic data: Its ability to support dynamic data without a need to rebuild the site.
  2. Speed: iSSG is at least as fast as SSG because data retrieval and rendering still takes place in the background. There is little processing required on the client or the server.
  3. Availability: A fairly recent version of any page will always be available online for users to access. Even if the regeneration fails in the background, the old version remains unaltered.
  4. Consistent: As the regeneration takes place on the server one page at a time, the load on the database and the backend is low and performance is consistent. As a result, there are no spikes in latency.
  5. Ease of Distribution: Just like SSG sites, iSSG sites can also be distributed through a network of CDN's used to serve pre-rendered web pages.

Source

來源與署名

來源:patternsdev/skills位於react/incremental-static-rendering提交48bf58a

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架