React Router (framework mode) and Remix
Last reviewed: 2026-10-27 · Applies to: React Router 8.5 (7.18 still maintained) framework mode; Remix v2 (@remix-run/* 2.17, same concepts)
When to read this
When react-router with @react-router/dev (or react-router.config.*) or @remix-run/* is a dependency. Caching is HTTP-first: the route headers export, plus DIY client caching in clientLoader. There is no built-in server loader cache.
How to detect
@react-router/dev or react-router.config.ts = framework mode (react-router alone can be data/declarative mode). @remix-run/react 2.x = Remix v2. React Router 8 removed the react-router-dom package and is ESM-only. remix@3.x is a different framework (not covered).
Default recipe
// app/routes/products.$id.tsx
import { data } from 'react-router'
import type { Route } from './+types/products.$id'
export async function loader({ params }: Route.LoaderArgs) {
const product = await getProduct(params.id) // your own server cache if needed
return data(product, {
headers: { 'Cache-Control': 'public, max-age=0, s-maxage=300, stale-while-revalidate=86400' },
})
}
// Loader headers are NOT sent automatically; re-emit them here
export function headers({ loaderHeaders, parentHeaders }: Route.HeadersArgs) {
return {
'Cache-Control': loaderHeaders.get('Cache-Control') ?? parentHeaders.get('Cache-Control') ?? 'no-store',
}
}Rules and gotchas
- Headers from loaders/actions aren't sent unless the
headersexport returns them. Only the deepest matching route's headers are sent; merge parent headers manually.Set-Cookieis the exception (preserved automatically). Default tono-storewhen no route sets a policy. - Single-fetch
.datarequests are separate URLs. If a CDN caches documents, make sure.dataresponses carry the sameCache-Controland that the CDN key respects v8's trailing-slash-aware.dataURLs. - CVE-2025-43864 / CVE-2025-43865: request headers (
X-React-Router-SPA-Mode,X-React-Router-Prerender-Data) could make an error or altered response cacheable by a CDN. Fixed in 7.5.2; keep React Router patched. - Revalidation defaults: framework mode with SSR revalidates active loaders after navigations and successful submissions (not after 4xx/5xx submissions). SPA/data mode revalidates GET navigations only when params/search change. Tune with
shouldRevalidate(fall back todefaultShouldRevalidate). - Personalized loaders return
private, no-cache(orno-store), nevers-maxage.
Client caching
const cache = new Map<string, unknown>()
export async function clientLoader({ request, serverLoader }: Route.ClientLoaderArgs) {
const key = new URL(request.url).pathname
if (cache.has(key)) return cache.get(key)
const result = await serverLoader()
cache.set(key, result)
return result
}
clientLoader.hydrate = true as const // prime the cache on hydration
export async function clientAction({ serverAction }: Route.ClientActionArgs) {
cache.clear() // invalidate on writes
return serverAction()
}Bound this cache (LRU) and clear it on logout. Pairing clientLoader with TanStack Query's queryClient.ensureQueryData is a common alternative (references/frameworks/client-data-caches.md).
SPA mode and prerendering
ssr: falseinreact-router.config.ts: root route rendered at build time toindex.html; data viaclientLoader/clientAction. Serveindex.htmlno-cache, hashed assetsimmutable.prerender: true | string[] | async ({ getStaticPaths }) => [...]works withssr: trueorfalse; prerendered HTML can get long CDN TTLs with purge on deploy.
Version notes
- v8:
react-router-domremoved; ESM-only; Node ≥ 22.22; middleware always on; trailing-slash-aware data URLs by default; raw request passed to loaders (use the normalizedurl). v8.3 adds RSC metadata so routes withclientLoadercan skip server requests.
- Remix v1/v2
json()helper with headers (usedata()in React Router 7+). react-router-domimports (removed in v8).
Verify
curl -s -o /dev/null -D - https://example.com/products/1 | grep -iE 'cache-control|age|cache-status'
curl -s -o /dev/null -D - 'https://example.com/products/1.data' | grep -i cache-control # matches the document policy
curl -s -o /dev/null -D - -H 'X-React-Router-SPA-Mode: yes' https://example.com/ | grep -iE 'cache-control|status'