|
1 | | -# Welcome to React Router! |
| 1 | +# Welcome to React Router with Chakra UI! |
2 | 2 |
|
3 | 3 | A modern, production-ready template for building full-stack React applications |
4 | | -using React Router. |
| 4 | +using React Router and Chakra UI with proper Emotion cache SSR support. |
5 | 5 |
|
6 | 6 | [](https://stackblitz.com/github/remix-run/react-router-templates/tree/main/default) |
7 | 7 |
|
8 | 8 | ## Features |
9 | 9 |
|
10 | | -- 🚀 Server-side rendering |
| 10 | +- 🚀 Server-side rendering with React Router v7 |
11 | 11 | - ⚡️ Hot Module Replacement (HMR) |
12 | 12 | - 📦 Asset bundling and optimization |
13 | 13 | - 🔄 Data loading and mutations |
14 | 14 | - 🔒 TypeScript by default |
15 | | -- 🎉 TailwindCSS for styling |
| 15 | +- 🎨 Chakra UI with Emotion cache configured for SSR |
| 16 | +- 🌓 Dark mode support with next-themes |
16 | 17 | - 📖 [React Router docs](https://reactrouter.com/) |
17 | 18 |
|
| 19 | +## Emotion Cache SSR Setup |
| 20 | + |
| 21 | +This example demonstrates how to properly configure Emotion cache for |
| 22 | +server-side rendering with React Router v7, addressing |
| 23 | +[issue #10450](https://github.com/chakra-ui/chakra-ui/issues/10450). |
| 24 | + |
| 25 | +### Architecture |
| 26 | + |
| 27 | +The Emotion cache setup consists of three main parts: |
| 28 | + |
| 29 | +#### 1. Emotion Cache Utilities (`app/emotion/`) |
| 30 | + |
| 31 | +- **`emotion-cache.ts`** - Creates the base Emotion cache instance |
| 32 | +- **`emotion-server.tsx`** - Server-side utilities (note: streaming limitations) |
| 33 | +- **`emotion-client.tsx`** - Client-side cache provider and style injection |
| 34 | + hooks |
| 35 | + |
| 36 | +#### 2. Entry Files |
| 37 | + |
| 38 | +- **`app/entry.server.tsx`** - Wraps server rendering with `CacheProvider` |
| 39 | +- **`app/entry.client.tsx`** - Wraps client hydration with `ClientCacheProvider` |
| 40 | + |
| 41 | +#### 3. Root Layout |
| 42 | + |
| 43 | +- **`app/root.tsx`** - Uses `withEmotionCache` HOC and includes emotion |
| 44 | + insertion point |
| 45 | + |
| 46 | +### Key Implementation Details |
| 47 | + |
| 48 | +1. **Server Rendering**: The server wraps the React Router `<ServerRouter>` with |
| 49 | + Emotion's `<CacheProvider>` to ensure styles are tracked during SSR. |
| 50 | + |
| 51 | +2. **Client Hydration**: The client uses `<ClientCacheProvider>` to maintain a |
| 52 | + consistent cache across hydration and subsequent client-side navigation. |
| 53 | + |
| 54 | +3. **Emotion Insertion Point**: A `<meta name="emotion-insertion-point">` tag is |
| 55 | + placed in the `<head>` to control where Emotion injects styles. |
| 56 | + |
| 57 | +4. **Style Injection**: The `useInjectStyles` hook ensures server-rendered |
| 58 | + styles are properly transferred to the client-side cache during hydration. |
| 59 | + |
| 60 | +5. **Hydration Warning Fix**: The `<html>` tag includes |
| 61 | + `suppressHydrationWarning` to prevent warnings from next-themes modifying the |
| 62 | + className during client-side rendering. |
| 63 | + |
| 64 | +### Known Limitations |
| 65 | + |
| 66 | +**Emotion + React 18 Streaming SSR**: Emotion doesn't fully support React 18's |
| 67 | +`renderToPipeableStream` API yet. This implementation provides a working |
| 68 | +solution by: |
| 69 | + |
| 70 | +- Using `CacheProvider` on both server and client |
| 71 | +- Relying on client-side style injection during hydration |
| 72 | +- Maintaining style consistency through the emotion insertion point |
| 73 | + |
| 74 | +While this doesn't provide optimal critical CSS extraction during streaming, it |
| 75 | +prevents hydration mismatches and ensures styles render correctly. |
| 76 | + |
| 77 | +For more information, see: |
| 78 | + |
| 79 | +- [Emotion Discussion #2859](https://github.com/emotion-js/emotion/discussions/2859) |
| 80 | +- [Emotion Issue #2800](https://github.com/emotion-js/emotion/issues/2800) |
| 81 | + |
18 | 82 | ## Getting Started |
19 | 83 |
|
20 | 84 | ### Installation |
@@ -83,9 +147,13 @@ Make sure to deploy the output of `npm run build` |
83 | 147 |
|
84 | 148 | ## Styling |
85 | 149 |
|
86 | | -This template comes with [Tailwind CSS](https://tailwindcss.com/) already |
87 | | -configured for a simple default starting experience. You can use whatever CSS |
88 | | -framework you prefer. |
| 150 | +This template comes with [Chakra UI](https://chakra-ui.com/) already configured |
| 151 | +with proper Emotion cache SSR support. The styling system includes: |
| 152 | + |
| 153 | +- Component library with accessible primitives |
| 154 | +- Dark mode support via next-themes |
| 155 | +- Emotion-based styling with SSR hydration |
| 156 | +- Responsive design utilities |
89 | 157 |
|
90 | 158 | --- |
91 | 159 |
|
|
0 commit comments