sandbox/react-router/README.md
A modern, production-ready template for building full-stack React applications using React Router and Chakra UI with proper Emotion cache SSR support.
This example demonstrates how to properly configure Emotion cache for server-side rendering with React Router v7, addressing issue #10450.
The Emotion cache setup consists of three main parts:
app/emotion/)emotion-cache.ts - Creates the base Emotion cache instanceemotion-server.tsx - Server-side utilities (note: streaming limitations)emotion-client.tsx - Client-side cache provider and style injection
hooksapp/entry.server.tsx - Wraps server rendering with CacheProviderapp/entry.client.tsx - Wraps client hydration with ClientCacheProviderapp/root.tsx - Uses withEmotionCache HOC and includes emotion
insertion pointServer Rendering: The server wraps the React Router <ServerRouter> with
Emotion's <CacheProvider> to ensure styles are tracked during SSR.
Client Hydration: The client uses <ClientCacheProvider> to maintain a
consistent cache across hydration and subsequent client-side navigation.
Emotion Insertion Point: A <meta name="emotion-insertion-point"> tag is
placed in the <head> to control where Emotion injects styles.
Style Injection: The useInjectStyles hook ensures server-rendered
styles are properly transferred to the client-side cache during hydration.
Hydration Warning Fix: The <html> tag includes
suppressHydrationWarning to prevent warnings from next-themes modifying the
className during client-side rendering.
Emotion + React 18 Streaming SSR: Emotion doesn't fully support React 18's
renderToPipeableStream API yet. This implementation provides a working
solution by:
CacheProvider on both server and clientWhile this doesn't provide optimal critical CSS extraction during streaming, it prevents hydration mismatches and ensures styles render correctly.
For more information, see:
Install the dependencies:
npm install
Start the development server with HMR:
npm run dev
Your application will be available at http://localhost:5173.
Create a production build:
npm run build
To build and run using Docker:
docker build -t my-app .
# Run the container
docker run -p 3000:3000 my-app
The containerized application can be deployed to any platform that supports Docker, including:
If you're familiar with deploying Node applications, the built-in app server is production-ready.
Make sure to deploy the output of npm run build
āāā package.json
āāā package-lock.json (or pnpm-lock.yaml, or bun.lockb)
āāā build/
ā āāā client/ # Static assets
ā āāā server/ # Server-side code
This template comes with Chakra UI already configured with proper Emotion cache SSR support. The styling system includes:
Built with ā¤ļø using React Router.