← Back to posts

Static vs Dynamic Asset Generation ⚡

Choose the right approach for your project: pre-generate assets at build time or generate them on-demand with SSR.

#static #dynamic #performance #seo

Static vs Dynamic Asset Generation

astro-assets-generation supports both static (build-time) and dynamic (on-demand) image generation. Choosing the right approach depends on your deployment target and content update frequency.

Static Generation

Use getStaticPathsForAssets to pre-generate all images during astro build:

export const getStaticPaths = async () => {
  const posts = await getCollection("blog");
  return getStaticPathsForAssets(
    modules,
    posts.map((p) => ({ slug: p.id }))
  );
};

export const GET = apiImageEndpoint(modules);

Pros: Zero latency, works with any static host (Netlify, GitHub Pages, S3…). Cons: Images are only updated on the next build.

Dynamic Generation (this app)

Disable prerendering to generate images on every request:

export const prerender = false;

export const GET: APIRoute = apiImageEndpoint(
  import.meta.glob("./_*.tsx", { eager: true })
);

Pros: Always up-to-date, can use request-time data. Cons: Requires a server-side runtime (Node.js, Vercel, Cloudflare…).

When to use which?

StaticDynamic
Deploy targetAny hostServer required
Content updatesNeeds rebuildInstant
LatencyNoneSmall overhead
Dynamic dataNoYes

For most blog sites, static generation is the better choice. For user-generated content or dashboards, use dynamic generation.

Generated OG image Generated on-demand

OG image for Static vs Dynamic Asset Generation ⚡

/blog/third-post/assets/cover-author.png