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:.output/public can be deployed to any static host.
Static Server Functions
Cache server function results at build time:How It Works
- Build Time - Server function executes and results are cached to JSON files
- Runtime - Client fetches pre-generated JSON instead of calling server
- Development - Functions execute normally for live development
Input-Based Caching
Cache results based on input parameters: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: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
-
Choose the Right Strategy
- Full static for content sites
- Hybrid for dynamic sections
- ISR for frequently updated content
-
Cache Wisely
- Cache stable data aggressively
- Don’t cache user-specific data
- Set appropriate cache durations
-
Optimize Build Times
- Limit prerendered routes
- Use incremental builds
- Cache build artifacts
-
Handle Errors
- Provide fallbacks for missing pages
- Generate error pages
- Monitor failed builds
-
SEO Optimization
- Generate sitemaps
- Create RSS feeds
- Include meta tags in static HTML
-
Performance
- Minimize bundle sizes
- Optimize images
- Use CDN for assets
-
Testing
- Test static builds locally
- Verify all routes work
- Check cache behavior
Limitations
-
No Runtime Server
- Cannot use server-only features
- No access to request context
- Limited to build-time data
-
Build Time
- Large sites take longer to build
- Need to rebuild for updates
- Resource-intensive for many routes
-
Dynamic Data
- Cannot fetch user-specific data
- Authentication requires client-side
- Real-time features need client polling
Next Steps
- Explore Deployment options
- Learn about Data Fetching strategies
- Review Server Functions documentation