Skip to main content
You can set Cache-Control headers on SSR page responses by adding the cacheControl option to Sonamu’s SSR routes.

Basic Usage

Adding to registerSSR

Result:

SSR Caching Strategy

SSR Preset

The most recommended approach is using CachePresets.ssr.
CachePresets.ssr:

How It Works

Benefits:
  • Most requests get instant response (CDN cache)
  • Fast response even during stale period
  • Background updates keep content fresh

Strategies by Page Type

Frequently changing content

Practical Examples

1. E-commerce Site

2. Blog

3. SaaS Dashboard

CSR Fallback

All requests that don’t match SSR routes are handled by CSR fallback.
How it works:

Global Handler

You can apply Cache-Control to all SSR pages at once.

Using SSRRoute Object

Priority Order

SSR Cache-Control settings are applied in the following order:
  1. registerSSR’s cacheControl (highest priority)
  2. cacheControlHandler return value
  3. Default (no Cache-Control header)

Vary Header

Use Vary header for multi-language SSR pages.
Result:

CDN Optimization

CloudFront + Stale-While-Revalidate

AWS CloudFront supports stale-while-revalidate.
Effect:
  • Browser: Requests CDN every minute
  • CDN: Requests server every 5 minutes
  • Stale: Fast response for up to 15 minutes (5 min + 10 min)

Combining with Static Assets

Result:
  • HTML: Updates every 10 seconds (fresh content)
  • JS/CSS: 1 year cache (fast loading)

Preloading and Caching

Combine SSR’s preload feature with Cache-Control.
Effect:
  1. During SSR: Preload data on server β†’ Generate HTML β†’ Cache
  2. Cache hit: CDN returns HTML instantly (fast)
  3. Hydration: Client uses preloaded data

Handling Personalized Pages

Pages Requiring Authentication

Conditional SSR

When rendering different pages based on user state:

Precautions

Precautions when using SSR Cache-Control:
  1. Personalized pages must be private or no-store: Prevent CDN caching
  2. Short TTL for dynamic content: Prevent stale HTML
  3. Consider authentication cookies: Separate with Vary header
  4. Configure CSR fallback: Handle unmatched routes

Debugging

Checking Cache-Control Header

Check in browser developer tools:

CDN Cache Status

CDNs provide cache status headers: CloudFront:
Cloudflare:

Next Steps

What is Cache-Control?

HTTP caching fundamentals

Cache Presets

Predefined cache configurations

Using in APIs

Apply to @api decorators