Skip to main content

Static Generation

TanStack Start supports static site generation (SSG), enabling you to prerender pages at build time. This results in optimal performance, SEO, and deployment flexibility.

What is Static Generation?

Static generation prerenders routes at build time, producing HTML files that can be served from any static host. Benefits include:
  • Optimal Performance: No server-side rendering overhead
  • Better SEO: Fully rendered HTML for search engines
  • Lower Costs: Deploy to free static hosting
  • Global Distribution: Easy CDN deployment
  • Offline Support: Progressive web app capabilities

Configuring Static Generation

1

Enable Prerendering

Configure routes to prerender in vite.config.ts:
2

Build Static Site

Run the build command:
This generates static HTML files in dist/client for each specified route.
3

Deploy Static Files

Deploy the dist/client directory to any static host:

Dynamic Route Prerendering

Prerender routes with dynamic parameters:

Incremental Static Regeneration

Combine static generation with on-demand regeneration:

On-Demand Revalidation

Trigger static page regeneration:

Time-Based Revalidation

Schedule periodic rebuilds:

Hybrid Static and Dynamic

Combine static pages with dynamic routes:
Non-prerendered routes (like /blog/[slug]) fall back to SSR or client-side rendering.

Client-Side Data Fetching

Load additional data on the client:

Sitemap Generation

Generate sitemaps during build:
Run after build:

RSS Feed Generation

Generate RSS feeds for static content:

Image Optimization

Optimize images during build:
Use optimized images:

Metadata Generation

Generate SEO metadata for static pages:

Progressive Web App

Generate PWA manifest and service worker:
Register service worker:

Build-Time Environment Variables

Access environment variables during build:

Best Practices

  • Prerender high-traffic pages: Focus on pages that benefit most from SSG
  • Use incremental builds: Only rebuild changed pages
  • Optimize images: Compress and convert to modern formats
  • Generate sitemaps: Help search engines discover your content
  • Implement caching: Set aggressive cache headers for static assets
  • Monitor build times: Keep builds fast for better developer experience
  • Test static output: Verify generated HTML is correct
  • Use CDNs: Distribute static files globally
  • Implement fallbacks: Handle non-prerendered routes gracefully
  • Version assets: Use content hashing for cache busting

Learn More