Skip to main content
Learn how to configure TanStack Query’s client caching and HTTP caching for SSR responses in Sonamu.

Cache Control Overview

TanStack Query

Client memory caching staleTime/gcTime

HTTP Cache

Cache-Control headers CDN/Browser caching

Revalidation

Auto/manual refresh Keep data fresh

Performance

Fast response Reduced server load

TanStack Query Client Caching

Basic Configuration

staleTime vs gcTime

Per-Query Cache Settings

Pass options to Service Hook for individual settings.

Caching Strategy by Data Type

Manual Cache Control

Cache Invalidation

Direct Cache Update

Cache Query

SSR Cache-Control

You can set HTTP Cache-Control headers in registerSSR.

Individual Route Caching

Generated header:

Caching Strategy Examples

CacheControlConfig Type

Global Caching Handler

Set a global handler that applies to all SSR routes.

Hybrid Caching

Use SSR and client caching together.
Caching layers:
  1. Browser HTTP cache: 1 hour (max-age=3600)
  2. TanStack Query memory: 5 minutes (staleTime)
  3. Server SSR rendering: Render on cache miss
Flow:
  1. First visit: SSR → HTML + data → Store in browser cache
  2. Re-visit within 5 min: Use TanStack Query memory cache
  3. 5 min ~ 1 hour: Use browser HTTP cache
  4. After 1 hour: New request to server

Development Environment Debugging

React Query Devtools

Check Cache State

Check HTTP Cache Headers

Check response headers in Browser Developer Tools → Network tab:

Performance Optimization Strategies

1. Use Subsets

Cache only needed fields to save memory:

2. Selective Revalidation

Actively revalidate only important data:

3. Cache Prefetching

Pre-cache data that will be needed next:

Cautions

Cautions when using cache: 1. staleTime setting required: Default 0 always refetches 2. Private for personal data: No public caching 3. Sensitive data: Disable caching with noStore 4. SSR cacheControl: Individual settings take precedence over global handler 5. Memory management: Remove unnecessary cache with gcTime

Next Steps

SSR Setup

SSR basic structure

Data Preloading

How to use registerSSR

Hydration Strategies

Hydration optimization

TanStack Query Docs

Query detailed guide