g1t/apps/web/.agents/skills/react-router/references/framework-mode.md
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Initial g1t: services, event bus, intents and attempts | 1 | # Framework Mode |
| 2 | ||
| 3 | Framework Mode is React Router's full-stack mode. It uses the React Router Vite plugin, route config in `app/routes.ts`, route modules, generated route types, and rendering strategies such as SSR, SPA mode, and pre-rendering. | |
| 4 | ||
| 5 | Use this reference after the main skill identifies a Framework Mode app. | |
| 6 | ||
| 7 | ## Read the Local Docs by Mode | |
| 8 | ||
| 9 | Start with: | |
| 10 | ||
| 11 | ```txt | |
| 12 | react-router/docs/start/modes.md | |
| 13 | react-router/docs/start/framework/index.md | |
| 14 | ``` | |
| 15 | ||
| 16 | Then use the Framework docs under: | |
| 17 | ||
| 18 | ```txt | |
| 19 | react-router/docs/start/framework/ | |
| 20 | ``` | |
| 21 | ||
| 22 | Those files cover installation, routing, route modules, data loading, actions, navigation, pending UI, rendering, deploying, and testing. For task-specific details, read relevant files in: | |
| 23 | ||
| 24 | ```txt | |
| 25 | react-router/docs/how-to/ | |
| 26 | react-router/docs/explanation/ | |
| 27 | ``` | |
| 28 | ||
| 29 | Always check the `[MODES: framework, ...]` marker in a doc before applying it. | |
| 30 | ||
| 31 | ## Framework Shape | |
| 32 | ||
| 33 | Examples usually assume the default `appDirectory` of `app`. Check `react-router.config.ts` before assuming exact paths. | |
| 34 | ||
| 35 | Look for these files and conventions: | |
| 36 | ||
| 37 | ```txt | |
| 38 | react-router.config.ts | |
| 39 | app/root.tsx | |
| 40 | app/routes.ts | |
| 41 | app/routes/**/*.tsx | |
| 42 | route modules importing from ./+types/... | |
| 43 | ``` | |
| 44 | ||
| 45 | Typical route module: | |
| 46 | ||
| 47 | ```tsx | |
| 48 | import type { Route } from "./+types/product"; | |
| 49 | ||
| 50 | export async function loader({ params }: Route.LoaderArgs) { | |
| 51 | return { product: await getProduct(params.productId) }; | |
| 52 | } | |
| 53 | ||
| 54 | export default function Product({ loaderData }: Route.ComponentProps) { | |
| 55 | return <h1>{loaderData.product.name}</h1>; | |
| 56 | } | |
| 57 | ``` | |
| 58 | ||
| 59 | ## Route Configuration | |
| 60 | ||
| 61 | Framework apps use `app/routes.ts`. Many apps use file-system routing via `flatRoutes()`, but manual route config is also supported. | |
| 62 | ||
| 63 | Before editing routes, read: | |
| 64 | ||
| 65 | ```txt | |
| 66 | react-router/docs/start/framework/routing.md | |
| 67 | ``` | |
| 68 | ||
| 69 | If the app uses file-route conventions, read: | |
| 70 | ||
| 71 | ```txt | |
| 72 | react-router/docs/how-to/file-route-conventions.md | |
| 73 | ``` | |
| 74 | ||
| 75 | ## Route Modules | |
| 76 | ||
| 77 | Route modules are the main unit of Framework Mode. Before adding or changing route exports, read: | |
| 78 | ||
| 79 | ```txt | |
| 80 | react-router/docs/start/framework/route-module.md | |
| 81 | ``` | |
| 82 | ||
| 83 | Common exports include: | |
| 84 | ||
| 85 | | Export | Use | | |
| 86 | | --------------------------------- | ------------------------------------------------------------------- | | |
| 87 | | `default` | Route component rendered for the match | | |
| 88 | | `loader` | Server data loading for SSR/pre-rendering/server data requests | | |
| 89 | | `clientLoader` | Browser-only data loading or supplementing server loader data | | |
| 90 | | `action` | Server mutation called by `<Form>`, `useSubmit`, or fetchers | | |
| 91 | | `clientAction` | Browser-only mutation or client-side wrapper around a server action | | |
| 92 | | `ErrorBoundary` | UI for errors thrown by this route's loaders/actions/component | | |
| 93 | | `HydrateFallback` | Initial fallback while client loader hydration runs | | |
| 94 | | `links` / `meta` | Route document links and metadata | | |
| 95 | | `handle` | Arbitrary route metadata consumed via `useMatches` | | |
| 96 | | `shouldRevalidate` | Overrides default loader revalidation behavior | | |
| 97 | | `middleware` / `clientMiddleware` | Server/client request pipeline hooks when enabled | | |
| 98 | ||
| 99 | Use generated `Route.*` types from `./+types/<route>` for route module args and props. | |
| 100 | ||
| 101 | ## Layout and Root Route Rules | |
| 102 | ||
| 103 | - `app/root.tsx` is the root route and should contain global document/app shell concerns. | |
| 104 | - Put global providers, app-wide nav, app-wide footer, scripts/meta/links, and document structure in `root.tsx` when appropriate. | |
| 105 | - Use nested routes/layout routes for section-specific layouts. | |
| 106 | - Do not flatten routes that should share UI or data boundaries. | |
| 107 | ||
| 108 | Useful docs: | |
| 109 | ||
| 110 | ```txt | |
| 111 | react-router/docs/explanation/special-files.md | |
| 112 | react-router/docs/start/framework/routing.md | |
| 113 | ``` | |
| 114 | ||
| 115 | ## Data and Mutations | |
| 116 | ||
| 117 | Before working on route data: | |
| 118 | ||
| 119 | ```txt | |
| 120 | react-router/docs/start/framework/data-loading.md | |
| 121 | react-router/docs/start/framework/actions.md | |
| 122 | ``` | |
| 123 | ||
| 124 | Framework rules: | |
| 125 | ||
| 126 | - Load route data with `loader` or `clientLoader`. | |
| 127 | - Mutate route data with `action` or `clientAction`. | |
| 128 | - Prefer route loaders/actions over ad hoc `useEffect` fetching for route data. | |
| 129 | - Use `data()`/Responses and redirects according to the docs. | |
| 130 | - Let React Router revalidate after actions unless the docs point you to `shouldRevalidate`. | |
| 131 | - In SSR/server data routes, keep Node-only/database code in server-only modules and call it from `loader`/`action`, not from browser-rendered component code. | |
| 132 | ||
| 133 | Common patterns: | |
| 134 | ||
| 135 | - Validation failure from an action: return `data({ errors, values }, { status: 400 })`, then render errors from `Route.ComponentProps["actionData"]` or `fetcher.data`. | |
| 136 | - Missing record in a loader: throw `data("Not Found", { status: 404 })` and render the route `ErrorBoundary`. | |
| 137 | - Search/filter data: parse the route request URL/search params in the loader so the URL is shareable and bookmarkable. | |
| 138 | ||
| 139 | ## Forms, Fetchers, and Pending UI | |
| 140 | ||
| 141 | For forms and pending UI, read: | |
| 142 | ||
| 143 | ```txt | |
| 144 | react-router/docs/start/framework/actions.md | |
| 145 | react-router/docs/start/framework/pending-ui.md | |
| 146 | react-router/docs/how-to/fetchers.md | |
| 147 | react-router/docs/explanation/form-vs-fetcher.md | |
| 148 | ``` | |
| 149 | ||
| 150 | Rules of thumb: | |
| 151 | ||
| 152 | - Search/filter form that updates the URL: `<Form method="get">`. | |
| 153 | - Mutation that should change URL/history or redirect after completion: `<Form method="post">`. | |
| 154 | - Mutation that should keep the user on the same page: `useFetcher` / `<fetcher.Form>`. | |
| 155 | - Optimistic UI: derive from `fetcher.formData` or `navigation.formData`. | |
| 156 | ||
| 157 | ## Type Safety | |
| 158 | ||
| 159 | Before changing generated route types or typed URL behavior, read: | |
| 160 | ||
| 161 | ```txt | |
| 162 | react-router/docs/how-to/route-module-type-safety.md | |
| 163 | react-router/docs/explanation/type-safety.md | |
| 164 | ``` | |
| 165 | ||
| 166 | Rules: | |
| 167 | ||
| 168 | - Import types from `./+types/<route>`. | |
| 169 | - Use `Route.LoaderArgs`, `Route.ActionArgs`, `Route.ComponentProps`, etc. | |
| 170 | - Use type-only imports where appropriate. | |
| 171 | - Do not edit generated `.react-router/types` files. | |
| 172 | ||
| 173 | ## Metadata | |
| 174 | ||
| 175 | Before changing `meta`, read: | |
| 176 | ||
| 177 | ```txt | |
| 178 | react-router/docs/how-to/meta.md | |
| 179 | react-router/docs/start/framework/route-module.md | |
| 180 | ``` | |
| 181 | ||
| 182 | Important: `meta` receives `loaderData`; do not use deprecated `data` args. | |
| 183 | ||
| 184 | ## Rendering Strategy | |
| 185 | ||
| 186 | Framework Mode can be SSR, SPA, pre-rendered, or mixed depending on config and route behavior. Before changing rendering behavior, read: | |
| 187 | ||
| 188 | ```txt | |
| 189 | react-router/docs/start/framework/rendering.md | |
| 190 | react-router/docs/how-to/spa.md | |
| 191 | react-router/docs/how-to/pre-rendering.md | |
| 192 | react-router/docs/explanation/hydration.md | |
| 193 | ``` | |
| 194 | ||
| 195 | ## Middleware, Sessions, and Auth | |
| 196 | ||
| 197 | Before implementing middleware or auth/session flows, read: | |
| 198 | ||
| 199 | ```txt | |
| 200 | react-router/docs/how-to/middleware.md | |
| 201 | react-router/docs/explanation/sessions-and-cookies.md | |
| 202 | ``` | |
| 203 | ||
| 204 | Middleware and context APIs are version/config sensitive. Check the installed React Router version and the app's `react-router.config.ts` before implementing. | |
| 205 | ||
| 206 | ## RSC Framework | |
| 207 | ||
| 208 | If this Framework app uses `unstable_reactRouterRSC` or `@vitejs/plugin-rsc`, also read: | |
| 209 | ||
| 210 | ```txt | |
| 211 | references/rsc.md | |
| 212 | react-router/docs/how-to/react-server-components.md | |
| 213 | ``` |