Skip to main content

Static Generation

TanStack Start supports static site generation (SSG) and static server function caching, allowing you to pre-render pages and cache server function results at build time for optimal performance.

Overview

Static generation provides:
  • Faster page loads - Pre-rendered HTML served instantly
  • Better SEO - Search engines can easily crawl static HTML
  • Reduced server load - No server rendering for static pages
  • Edge caching - Deploy to CDNs for global distribution
  • Cost efficiency - Lower hosting costs with static files

Full Static Generation

Generate a completely static site:
Build the static site:
The output in .output/public can be deployed to any static host.

Static Server Functions

Cache server function results at build time:

How It Works

  1. Build Time - Server function executes and results are cached to JSON files
  2. Runtime - Client fetches pre-generated JSON instead of calling server
  3. Development - Functions execute normally for live development

Input-Based Caching

Cache results based on input parameters:
At build time, call the function with different inputs:

Prerendering Routes

Pre-render specific routes at build time:

Dynamic Route Prerendering

Generate routes dynamically:

Incremental Static Regeneration (ISR)

Regenerate static pages on-demand:

Hybrid Rendering

Combine static and dynamic content:

Build-Time Data Fetching

Fetch data during the build process:
Add to package.json:
Use the generated data:

Caching Strategies

Cache Everything

Cache all server functions:

Selective Caching

Cache only specific functions:

Time-Based Caching

Implement custom time-based caching:

Static API Routes

Generate static API responses:

Sitemap Generation

Generate sitemap during build:

RSS Feed Generation

Deploy Static Sites

Cloudflare Pages

Netlify

Vercel

Best Practices

  1. Choose the Right Strategy
    • Full static for content sites
    • Hybrid for dynamic sections
    • ISR for frequently updated content
  2. Cache Wisely
    • Cache stable data aggressively
    • Don’t cache user-specific data
    • Set appropriate cache durations
  3. Optimize Build Times
    • Limit prerendered routes
    • Use incremental builds
    • Cache build artifacts
  4. Handle Errors
    • Provide fallbacks for missing pages
    • Generate error pages
    • Monitor failed builds
  5. SEO Optimization
    • Generate sitemaps
    • Create RSS feeds
    • Include meta tags in static HTML
  6. Performance
    • Minimize bundle sizes
    • Optimize images
    • Use CDN for assets
  7. Testing
    • Test static builds locally
    • Verify all routes work
    • Check cache behavior

Limitations

  1. No Runtime Server
    • Cannot use server-only features
    • No access to request context
    • Limited to build-time data
  2. Build Time
    • Large sites take longer to build
    • Need to rebuild for updates
    • Resource-intensive for many routes
  3. Dynamic Data
    • Cannot fetch user-specific data
    • Authentication requires client-side
    • Real-time features need client polling

Next Steps