react-router 7.16.0 → 7.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/dist/development/{browser-nIQ4Nsyi.d.mts → browser-CGcs-0pD.d.mts} +1 -1
  3. package/dist/development/{chunk-QUQL4437.mjs → chunk-6CSD65Y2.mjs} +2 -2
  4. package/dist/{production/chunk-NALGHHKE.mjs → development/chunk-ASILSGTR.mjs} +2 -2
  5. package/dist/development/{chunk-SRID2YZ2.js → chunk-KFNXW4AL.js} +1 -1
  6. package/dist/development/{chunk-XEJDWL2B.js → chunk-PBLBZ3QU.js} +7 -7
  7. package/dist/{production/chunk-SKEDDLRM.js → development/chunk-PULC7NLK.js} +99 -99
  8. package/dist/development/{context-m8rizgnE.d.mts → context-CmHpk1Ws.d.mts} +1 -1
  9. package/dist/development/dom-export.d.mts +3 -3
  10. package/dist/development/dom-export.d.ts +1 -1
  11. package/dist/development/dom-export.js +28 -28
  12. package/dist/development/dom-export.mjs +3 -3
  13. package/dist/development/{index-react-server-client-BLiUx67a.d.ts → index-react-server-client-CwU9bE5R.d.ts} +1 -1
  14. package/dist/development/{index-react-server-client-CdKROblb.d.mts → index-react-server-client-DPrDrCew.d.mts} +1 -1
  15. package/dist/development/index-react-server-client.d.mts +2 -2
  16. package/dist/development/index-react-server-client.d.ts +1 -1
  17. package/dist/development/index-react-server-client.js +4 -4
  18. package/dist/development/index-react-server-client.mjs +2 -2
  19. package/dist/development/index-react-server.js +1 -1
  20. package/dist/development/index-react-server.mjs +1 -1
  21. package/dist/development/index.d.mts +6 -6
  22. package/dist/development/index.d.ts +2 -2
  23. package/dist/development/index.js +85 -85
  24. package/dist/development/index.mjs +3 -3
  25. package/dist/development/lib/types/internal.js +1 -1
  26. package/dist/development/lib/types/internal.mjs +1 -1
  27. package/dist/production/{browser-nIQ4Nsyi.d.mts → browser-CGcs-0pD.d.mts} +1 -1
  28. package/dist/{development/chunk-S54KXAEJ.mjs → production/chunk-5TQZEVD5.mjs} +2 -2
  29. package/dist/production/{chunk-EAQNHM3N.js → chunk-CTIXC7EV.js} +7 -7
  30. package/dist/{development/chunk-IBI7OMNB.js → production/chunk-EN242BO4.js} +99 -99
  31. package/dist/production/{chunk-Q65P7S7Y.mjs → chunk-OSYEOCBT.mjs} +2 -2
  32. package/dist/production/{chunk-Y7DNFQZP.js → chunk-RTRY3JFT.js} +1 -1
  33. package/dist/production/{context-m8rizgnE.d.mts → context-CmHpk1Ws.d.mts} +1 -1
  34. package/dist/production/dom-export.d.mts +3 -3
  35. package/dist/production/dom-export.d.ts +1 -1
  36. package/dist/production/dom-export.js +28 -28
  37. package/dist/production/dom-export.mjs +3 -3
  38. package/dist/production/{index-react-server-client-BLiUx67a.d.ts → index-react-server-client-CwU9bE5R.d.ts} +1 -1
  39. package/dist/production/{index-react-server-client-CdKROblb.d.mts → index-react-server-client-DPrDrCew.d.mts} +1 -1
  40. package/dist/production/index-react-server-client.d.mts +2 -2
  41. package/dist/production/index-react-server-client.d.ts +1 -1
  42. package/dist/production/index-react-server-client.js +4 -4
  43. package/dist/production/index-react-server-client.mjs +2 -2
  44. package/dist/production/index-react-server.js +1 -1
  45. package/dist/production/index-react-server.mjs +1 -1
  46. package/dist/production/index.d.mts +6 -6
  47. package/dist/production/index.d.ts +2 -2
  48. package/dist/production/index.js +85 -85
  49. package/dist/production/index.mjs +3 -3
  50. package/dist/production/lib/types/internal.js +1 -1
  51. package/dist/production/lib/types/internal.mjs +1 -1
  52. package/docs/explanation/backend-for-frontend.md +50 -0
  53. package/docs/explanation/code-splitting.md +61 -0
  54. package/docs/explanation/concurrency.md +135 -0
  55. package/docs/explanation/form-vs-fetcher.md +292 -0
  56. package/docs/explanation/hot-module-replacement.md +137 -0
  57. package/docs/explanation/hydration.md +14 -0
  58. package/docs/explanation/index-query-param.md +86 -0
  59. package/docs/explanation/index.md +4 -0
  60. package/docs/explanation/lazy-route-discovery.md +78 -0
  61. package/docs/explanation/location.md +6 -0
  62. package/docs/explanation/progressive-enhancement.md +150 -0
  63. package/docs/explanation/race-conditions.md +88 -0
  64. package/docs/explanation/react-transitions.md +160 -0
  65. package/docs/explanation/route-matching.md +7 -0
  66. package/docs/explanation/server-client-execution.md +4 -0
  67. package/docs/explanation/sessions-and-cookies.md +465 -0
  68. package/docs/explanation/special-files.md +16 -0
  69. package/docs/explanation/state-management.md +524 -0
  70. package/docs/explanation/styling.md +87 -0
  71. package/docs/explanation/type-safety.md +82 -0
  72. package/docs/how-to/accessibility.md +44 -0
  73. package/docs/how-to/client-data.md +199 -0
  74. package/docs/how-to/data-strategy.md +317 -0
  75. package/docs/how-to/error-boundary.md +231 -0
  76. package/docs/how-to/error-reporting.md +142 -0
  77. package/docs/how-to/fetchers.md +307 -0
  78. package/docs/how-to/file-route-conventions.md +410 -0
  79. package/docs/how-to/file-uploads.md +217 -0
  80. package/docs/how-to/form-validation.md +120 -0
  81. package/docs/how-to/headers.md +164 -0
  82. package/docs/how-to/index.md +4 -0
  83. package/docs/how-to/instrumentation.md +556 -0
  84. package/docs/how-to/meta.md +40 -0
  85. package/docs/how-to/middleware.md +763 -0
  86. package/docs/how-to/navigation-blocking.md +233 -0
  87. package/docs/how-to/optimize-revalidation.md +12 -0
  88. package/docs/how-to/pre-rendering.md +225 -0
  89. package/docs/how-to/presets.md +103 -0
  90. package/docs/how-to/react-server-components.md +899 -0
  91. package/docs/how-to/resource-routes.md +126 -0
  92. package/docs/how-to/route-module-type-safety.md +100 -0
  93. package/docs/how-to/search-params.md +4 -0
  94. package/docs/how-to/security.md +30 -0
  95. package/docs/how-to/server-bundles.md +66 -0
  96. package/docs/how-to/spa.md +120 -0
  97. package/docs/how-to/status.md +63 -0
  98. package/docs/how-to/suspense.md +132 -0
  99. package/docs/how-to/using-handle.md +117 -0
  100. package/docs/how-to/view-transitions.md +237 -0
  101. package/docs/how-to/webhook.md +50 -0
  102. package/docs/index.md +39 -0
  103. package/docs/start/data/actions.md +138 -0
  104. package/docs/start/data/custom.md +198 -0
  105. package/docs/start/data/data-loading.md +44 -0
  106. package/docs/start/data/index.md +4 -0
  107. package/docs/start/data/installation.md +52 -0
  108. package/docs/start/data/navigating.md +12 -0
  109. package/docs/start/data/pending-ui.md +12 -0
  110. package/docs/start/data/route-object.md +268 -0
  111. package/docs/start/data/routing.md +281 -0
  112. package/docs/start/data/testing.md +8 -0
  113. package/docs/start/declarative/index.md +4 -0
  114. package/docs/start/declarative/installation.md +43 -0
  115. package/docs/start/declarative/navigating.md +133 -0
  116. package/docs/start/declarative/routing.md +237 -0
  117. package/docs/start/declarative/url-values.md +65 -0
  118. package/docs/start/framework/actions.md +174 -0
  119. package/docs/start/framework/data-loading.md +201 -0
  120. package/docs/start/framework/deploying.md +96 -0
  121. package/docs/start/framework/index.md +4 -0
  122. package/docs/start/framework/installation.md +41 -0
  123. package/docs/start/framework/navigating.md +182 -0
  124. package/docs/start/framework/pending-ui.md +142 -0
  125. package/docs/start/framework/rendering.md +59 -0
  126. package/docs/start/framework/route-module.md +527 -0
  127. package/docs/start/framework/routing.md +362 -0
  128. package/docs/start/framework/testing.md +133 -0
  129. package/docs/start/index.md +4 -0
  130. package/docs/start/modes.md +201 -0
  131. package/docs/upgrading/component-routes.md +363 -0
  132. package/docs/upgrading/future.md +280 -0
  133. package/docs/upgrading/index.md +4 -0
  134. package/docs/upgrading/remix.md +403 -0
  135. package/docs/upgrading/router-provider.md +442 -0
  136. package/docs/upgrading/v6.md +382 -0
  137. package/package.json +2 -1
@@ -0,0 +1,233 @@
1
+ ---
2
+ title: Navigation Blocking
3
+ ---
4
+
5
+ # Navigation Blocking
6
+
7
+ [MODES: framework, data]
8
+
9
+ <br/>
10
+ <br/>
11
+
12
+ When users are in the middle of a workflow, like filling out an important form, you may want to prevent them from navigating away from the page.
13
+
14
+ This example will show:
15
+
16
+ - Setting up a route with a form and action called with a fetcher
17
+ - Blocking navigation when the form is dirty
18
+ - Showing a confirmation when the user tries to leave the page
19
+
20
+ ## 1. Set up a route with a form
21
+
22
+ Add a route with the form, we'll use a "contact" route for this example:
23
+
24
+ ```ts filename=routes.ts
25
+ import {
26
+ type RouteConfig,
27
+ index,
28
+ route,
29
+ } from "@react-router/dev/routes";
30
+
31
+ export default [
32
+ index("routes/home.tsx"),
33
+ route("contact", "routes/contact.tsx"),
34
+ ] satisfies RouteConfig;
35
+ ```
36
+
37
+ Add the form to the contact route module:
38
+
39
+ ```tsx filename=routes/contact.tsx
40
+ import { useFetcher } from "react-router";
41
+ import type { Route } from "./+types/contact";
42
+
43
+ export async function action({
44
+ request,
45
+ }: Route.ActionArgs) {
46
+ let formData = await request.formData();
47
+ let email = formData.get("email");
48
+ let message = formData.get("message");
49
+ console.log(email, message);
50
+ return { ok: true };
51
+ }
52
+
53
+ export default function Contact() {
54
+ let fetcher = useFetcher();
55
+
56
+ return (
57
+ <fetcher.Form method="post">
58
+ <p>
59
+ <label>
60
+ Email: <input name="email" type="email" />
61
+ </label>
62
+ </p>
63
+ <p>
64
+ <textarea name="message" />
65
+ </p>
66
+ <p>
67
+ <button type="submit">
68
+ {fetcher.state === "idle" ? "Send" : "Sending..."}
69
+ </button>
70
+ </p>
71
+ </fetcher.Form>
72
+ );
73
+ }
74
+ ```
75
+
76
+ ## 2. Add dirty state and onChange handler
77
+
78
+ To track the dirty state of the form, we'll use a single boolean and a quick form onChange handler. You may want to track the dirty state differently but this works for this guide.
79
+
80
+ ```tsx filename=routes/contact.tsx lines=[2,8-12]
81
+ export default function Contact() {
82
+ let [isDirty, setIsDirty] = useState(false);
83
+ let fetcher = useFetcher();
84
+
85
+ return (
86
+ <fetcher.Form
87
+ method="post"
88
+ onChange={(event) => {
89
+ let email = event.currentTarget.email.value;
90
+ let message = event.currentTarget.message.value;
91
+ setIsDirty(Boolean(email || message));
92
+ }}
93
+ >
94
+ {/* existing code */}
95
+ </fetcher.Form>
96
+ );
97
+ }
98
+ ```
99
+
100
+ ## 3. Block navigation when the form is dirty
101
+
102
+ ```tsx filename=routes/contact.tsx lines=[1,6-8]
103
+ import { useBlocker } from "react-router";
104
+
105
+ export default function Contact() {
106
+ let [isDirty, setIsDirty] = useState(false);
107
+ let fetcher = useFetcher();
108
+ let blocker = useBlocker(
109
+ useCallback(() => isDirty, [isDirty]),
110
+ );
111
+
112
+ // ... existing code
113
+ }
114
+ ```
115
+
116
+ While this will now block a navigation, there's no way for the user to confirm it.
117
+
118
+ ## 4. Show confirmation UI
119
+
120
+ This uses a simple div, but you may want to use a modal dialog.
121
+
122
+ ```tsx filename=routes/contact.tsx lines=[19-41]
123
+ export default function Contact() {
124
+ let [isDirty, setIsDirty] = useState(false);
125
+ let fetcher = useFetcher();
126
+ let blocker = useBlocker(
127
+ useCallback(() => isDirty, [isDirty]),
128
+ );
129
+
130
+ return (
131
+ <fetcher.Form
132
+ method="post"
133
+ onChange={(event) => {
134
+ let email = event.currentTarget.email.value;
135
+ let message = event.currentTarget.message.value;
136
+ setIsDirty(Boolean(email || message));
137
+ }}
138
+ >
139
+ {/* existing code */}
140
+
141
+ {blocker.state === "blocked" && (
142
+ <div>
143
+ <p>Wait! You didn't send the message yet:</p>
144
+ <p>
145
+ <button
146
+ type="button"
147
+ onClick={() => blocker.proceed()}
148
+ >
149
+ Leave
150
+ </button>{" "}
151
+ <button
152
+ type="button"
153
+ onClick={() => blocker.reset()}
154
+ >
155
+ Stay here
156
+ </button>
157
+ </p>
158
+ </div>
159
+ )}
160
+ </fetcher.Form>
161
+ );
162
+ }
163
+ ```
164
+
165
+ If the user clicks "leave" then `blocker.proceed()` will proceed with the navigation. If they click "stay here" then `blocker.reset()` will clear the blocker and keep them on the current page.
166
+
167
+ ## 5. Reset the blocker when the action resolves
168
+
169
+ If the user doesn't click either "leave" or "stay here", then submits the form, the blocker will still be active. Let's reset the blocker when the action resolves with an effect.
170
+
171
+ ```tsx filename=routes/contact.tsx
172
+ useEffect(() => {
173
+ if (fetcher.data?.ok) {
174
+ if (blocker.state === "blocked") {
175
+ blocker.reset();
176
+ }
177
+ }
178
+ }, [fetcher.data]);
179
+ ```
180
+
181
+ ## 6. Clear the form when the action resolves
182
+
183
+ While unrelated to navigation blocking, let's clear the form when the action resolves with a ref.
184
+
185
+ ```tsx
186
+ let formRef = useRef<HTMLFormElement>(null);
187
+
188
+ // put it on the form
189
+ <fetcher.Form
190
+ ref={formRef}
191
+ method="post"
192
+ onChange={(event) => {
193
+ // ... existing code
194
+ }}
195
+ >
196
+ {/* existing code */}
197
+ </fetcher.Form>;
198
+ ```
199
+
200
+ ```tsx
201
+ useEffect(() => {
202
+ if (fetcher.data?.ok) {
203
+ // clear the form in the effect
204
+ formRef.current?.reset();
205
+ if (blocker.state === "blocked") {
206
+ blocker.reset();
207
+ }
208
+ }
209
+ }, [fetcher.data]);
210
+ ```
211
+
212
+ Alternatively, if a navigation is currently blocked, instead of resetting the blocker, you can proceed through to the blocked navigation.
213
+
214
+ ```tsx
215
+ useEffect(() => {
216
+ if (fetcher.data?.ok) {
217
+ if (blocker.state === "blocked") {
218
+ // proceed with the blocked navigation
219
+ blocker.proceed();
220
+ } else {
221
+ formRef.current?.reset();
222
+ }
223
+ }
224
+ }, [fetcher.data]);
225
+ ```
226
+
227
+ In this case the user flow is:
228
+
229
+ - User fills out the form
230
+ - User forgets to click "send" and clicks a link instead
231
+ - The navigation is blocked, and the confirmation message is shown
232
+ - Instead of clicking "leave" or "stay here", the user submits the form
233
+ - The user is taken to the requested page
@@ -0,0 +1,12 @@
1
+ ---
2
+ title: Revalidation Optimization
3
+ hidden: true
4
+ ---
5
+
6
+ [copy pasted]
7
+
8
+ During client-side transitions, React Router will optimize reloading of routes that are already rendering, like not reloading layout routes that aren't changing. In other cases, like form submissions or search param changes, React Router doesn't know which routes need to be reloaded, so it reloads them all to be safe. This ensures your UI always stays in sync with the state on your server.
9
+
10
+ This function lets apps further optimize by returning `false` when React Router is about to reload a route. If you define this function on a route module, React Router will defer to your function on every navigation and every revalidation after an action is called. Again, this makes it possible for your UI to get out of sync with your server if you do it wrong, so be careful.
11
+
12
+ `fetcher.load` calls also revalidate, but because they load a specific URL, they don't have to worry about route param or URL search param revalidations. `fetcher.load`'s only revalidate by default after action submissions and explicit revalidation requests via [`useRevalidator`][use-revalidator].
@@ -0,0 +1,225 @@
1
+ ---
2
+ title: Pre-Rendering
3
+ ---
4
+
5
+ # Pre-Rendering
6
+
7
+ [MODES: framework]
8
+
9
+ <br/>
10
+ <br/>
11
+
12
+ Pre-Rendering allows you to speed up page loads for static content by rendering pages at build time instead of at runtime.
13
+
14
+ ## Configuration
15
+
16
+ Pre-rendering is enabled via the `prerender` config in `react-router.config.ts`.
17
+
18
+ The simplest configuration is a boolean `true` which will pre-render all off the applications static paths based on `routes.ts`:
19
+
20
+ ```ts filename=react-router.config.ts
21
+ import type { Config } from "@react-router/dev/config";
22
+
23
+ export default {
24
+ prerender: true,
25
+ } satisfies Config;
26
+ ```
27
+
28
+ The boolean `true` will not include any dynamic paths (i.e., `/blog/:slug`) because the parameter values are unknown.
29
+
30
+ To configure specific paths including dynamic values, you can specify an array of paths:
31
+
32
+ ```ts filename=react-router.config.ts
33
+ import type { Config } from "@react-router/dev/config";
34
+
35
+ let slugs = getPostSlugs();
36
+
37
+ export default {
38
+ prerender: [
39
+ "/",
40
+ "/blog",
41
+ ...slugs.map((s) => `/blog/${s}`),
42
+ ],
43
+ } satisfies Config;
44
+ ```
45
+
46
+ If you need to perform more complex and/or asynchronous logic to determine the paths, you can also provide a function that returns an array of paths. This function provides you with a `getStaticPaths` method you can use to avoid manually adding all of the static paths in your application:
47
+
48
+ ```ts filename=react-router.config.ts
49
+ import type { Config } from "@react-router/dev/config";
50
+
51
+ export default {
52
+ async prerender({ getStaticPaths }) {
53
+ let slugs = await getPostSlugsFromCMS();
54
+ return [
55
+ ...getStaticPaths(), // "/" and "/blog"
56
+ ...slugs.map((s) => `/blog/${s}`),
57
+ ];
58
+ },
59
+ } satisfies Config;
60
+ ```
61
+
62
+ ### Concurrency
63
+
64
+ By default, pages are pre-rendered one path at a time. You can enable concurrency to pre-render multiple paths in parallel which can speed up build times in many cases. You should experiment with the value that provides the best performance for your app.
65
+
66
+ To specify concurrency, move your `prerender` config down into a `prerender.paths` field and you can specify the concurrency in `prerender.concurrency`:
67
+
68
+ ```ts filename=react-router.config.ts
69
+ import type { Config } from "@react-router/dev/config";
70
+
71
+ let slugs = getPostSlugs();
72
+
73
+ export default {
74
+ prerender: {
75
+ paths: [
76
+ "/",
77
+ "/blog",
78
+ ...slugs.map((s) => `/blog/${s}`),
79
+ ],
80
+ concurrency: 4,
81
+ },
82
+ } satisfies Config;
83
+ ```
84
+
85
+ ## Pre-Rendering with/without a Runtime Server
86
+
87
+ Pre-Rendering can be used in two ways based on the `ssr` config value:
88
+
89
+ - Alongside a runtime SSR server with `ssr:true` (the default value)
90
+ - Deployed to a static file server with `ssr:false`
91
+
92
+ ### Pre-rendering with `ssr:true`
93
+
94
+ When pre-rendering with `ssr:true`, you're indicating you will still have a runtime server but you are choosing to pre-render certain paths for quicker Response times.
95
+
96
+ ```ts filename=react-router.config.ts
97
+ import type { Config } from "@react-router/dev/config";
98
+
99
+ export default {
100
+ // Can be omitted - defaults to true
101
+ ssr: true,
102
+ prerender: ["/", "/blog", "/blog/popular-post"],
103
+ } satisfies Config;
104
+ ```
105
+
106
+ #### Data Loading and Pre-rendering
107
+
108
+ There is no extra application API for pre-rendering. Routes being pre-rendered use the same route `loader` functions as server rendering:
109
+
110
+ ```tsx
111
+ export async function loader({ request, params }) {
112
+ let post = await getPost(params.slug);
113
+ return post;
114
+ }
115
+
116
+ export function Post({ loaderData }) {
117
+ return <div>{loaderData.title}</div>;
118
+ }
119
+ ```
120
+
121
+ Instead of a request coming to your route on a deployed server, the build creates a `new Request()` and runs it through your app just like a server would.
122
+
123
+ When server rendering, requests to paths that have not been pre-rendered will be server rendered as usual.
124
+
125
+ #### Static File Output
126
+
127
+ The rendered result will be written out to your `build/client` directory. You'll notice two files for each path:
128
+
129
+ - `[url].html` HTML file for initial document requests
130
+ - `[url].data` file for client side navigation browser requests
131
+
132
+ The output of your build will indicate what files were pre-rendered:
133
+
134
+ ```sh
135
+ > react-router build
136
+ vite v5.2.11 building for production...
137
+ ...
138
+ vite v5.2.11 building SSR bundle for production...
139
+ ...
140
+ Prerender: Generated build/client/index.html
141
+ Prerender: Generated build/client/blog.data
142
+ Prerender: Generated build/client/blog/index.html
143
+ Prerender: Generated build/client/blog/my-first-post.data
144
+ Prerender: Generated build/client/blog/my-first-post/index.html
145
+ ...
146
+ ```
147
+
148
+ During development, pre-rendering doesn't save the rendered results to the public directory, this only happens for `react-router build`.
149
+
150
+ ### Pre-rendering with `ssr:false`
151
+
152
+ The above examples assume you are deploying a runtime server but are pre-rendering some static pages to avoid hitting the server, resulting in faster loads.
153
+
154
+ To disable runtime SSR and configure pre-rendering to be served from a static file server, you can set the `ssr:false` config flag:
155
+
156
+ ```ts filename=react-router.config.ts
157
+ import type { Config } from "@react-router/dev/config";
158
+
159
+ export default {
160
+ ssr: false, // disable runtime server rendering
161
+ prerender: true, // pre-render all static routes
162
+ } satisfies Config;
163
+ ```
164
+
165
+ If you specify `ssr:false` without a `prerender` config, React Router refers to that as [SPA Mode](./spa). In SPA Mode, we render a single HTML file that is capable of hydrating for _any_ of your application paths. It can do this because it only renders the `root` route into the HTML file and then determines which child routes to load based on the browser URL during hydration. This means you can use a `loader` on the root route, but not on any other routes because we don't know which routes to load until hydration in the browser.
166
+
167
+ If you want to pre-render paths with `ssr:false`, those matched routes _can_ have loaders because we'll pre-render all of the matched routes for those paths, not just the root. You cannot include `actions` or `headers` functions in any routes when `ssr:false` is set because there will be no runtime server to run them on.
168
+
169
+ #### Pre-rendering with a SPA Fallback
170
+
171
+ If you want `ssr:false` but don't want to pre-render _all_ of your routes - that's fine too! You may have some paths where you need the performance/SEO benefits of pre-rendering, but other pages where a SPA would be fine.
172
+
173
+ You can do this using the combination of config options as well - just limit your `prerender` config to the paths that you want to pre-render and React Router will also output a "SPA Fallback" HTML file that can be served to hydrate any other paths (using the same approach as [SPA Mode](./spa)).
174
+
175
+ This will be written to one of the following paths:
176
+
177
+ - `build/client/index.html` - If the `/` path is not pre-rendered
178
+ - `build/client/__spa-fallback.html` - If the `/` path is pre-rendered
179
+
180
+ ```ts filename=react-router.config.ts
181
+ import type { Config } from "@react-router/dev/config";
182
+
183
+ export default {
184
+ ssr: false,
185
+
186
+ // SPA fallback will be written to build/client/index.html
187
+ prerender: ["/about-us"],
188
+
189
+ // SPA fallback will be written to build/client/__spa-fallback.html
190
+ prerender: ["/", "/about-us"],
191
+ } satisfies Config;
192
+ ```
193
+
194
+ You can configure your deployment server to serve this file for any path that otherwise would 404. Some hosts do this by default, but others don't. As an example, a host may support a `_redirects` file to do this:
195
+
196
+ ```
197
+ # If you did not pre-render the `/` route
198
+ /* /index.html 200
199
+
200
+ # If you pre-rendered the `/` route
201
+ /* /__spa-fallback.html 200
202
+ ```
203
+
204
+ If you're getting 404s at valid routes for your app, it's likely you need to configure your host.
205
+
206
+ Here's another example of how you can do this with the [`sirv-cli`](https://www.npmjs.com/package/sirv-cli#user-content-single-page-applications) tool:
207
+
208
+ ```sh
209
+ # If you did not pre-render the `/` route
210
+ sirv-cli build/client --single index.html
211
+
212
+ # If you pre-rendered the `/` route
213
+ sirv-cli build/client --single __spa-fallback.html
214
+ ```
215
+
216
+ #### Invalid Exports
217
+
218
+ When pre-rendering with `ssr:false`, React Router will error at build time if you have invalid exports to help prevent some mistakes that can be easily overlooked.
219
+
220
+ - `headers`/`action` functions are prohibited in all routes because there will be no runtime server on which to run them
221
+ - When using `ssr:false` without a `prerender` config (SPA Mode), a `loader` is permitted on the root route only
222
+ - When using `ssr:false` with a `prerender` config, a `loader` is permitted on any route matched by a `prerender` path
223
+ - If you are using a `loader` on a pre-rendered route that has child routes, you will need to make sure the parent `loaderData` can be determined at run-time properly by either:
224
+ - Pre-rendering all child routes so that the parent `loader` can be called at build-time for each child route path and rendered into a `.data` file, or
225
+ - Use a `clientLoader` on the parent that can be called at run-time for non-pre-rendered child paths
@@ -0,0 +1,103 @@
1
+ ---
2
+ title: Presets
3
+ ---
4
+
5
+ # Presets
6
+
7
+ [MODES: framework]
8
+
9
+ <br/>
10
+ <br/>
11
+
12
+ The [React Router config][react-router-config] supports a `presets` option to ease integration with other tools and hosting providers.
13
+
14
+ [Presets][preset-type] can only do two things:
15
+
16
+ - Configure React Router config options on your behalf
17
+ - Validate the resolved config
18
+
19
+ The config returned by each preset is merged in the order the presets were defined. Any config directly specified in your React Router config will be merged last. This means that your config will always take precedence over any presets.
20
+
21
+ ## Defining preset config
22
+
23
+ As a basic example, let's create a preset that configures a [server bundles function][server-bundles]:
24
+
25
+ ```ts filename=my-cool-preset.ts
26
+ import type { Preset } from "@react-router/dev/config";
27
+
28
+ export function myCoolPreset(): Preset {
29
+ return {
30
+ name: "my-cool-preset",
31
+ reactRouterConfig: () => ({
32
+ serverBundles: ({ branch }) => {
33
+ const isAuthenticatedRoute = branch.some((route) =>
34
+ route.id.split("/").includes("_authenticated"),
35
+ );
36
+
37
+ return isAuthenticatedRoute
38
+ ? "authenticated"
39
+ : "unauthenticated";
40
+ },
41
+ }),
42
+ };
43
+ }
44
+ ```
45
+
46
+ ## Validating config
47
+
48
+ Keep in mind that other presets and user config can still override the values returned from your preset.
49
+
50
+ In our example preset, the `serverBundles` function could be overridden with a different, conflicting implementation. If we want to validate that the final resolved config contains the `serverBundles` function from our preset, we can use the `reactRouterConfigResolved` hook:
51
+
52
+ ```ts filename=my-cool-preset.ts lines=[22-27]
53
+ import type {
54
+ Preset,
55
+ ServerBundlesFunction,
56
+ } from "@react-router/dev/config";
57
+
58
+ const serverBundles: ServerBundlesFunction = ({
59
+ branch,
60
+ }) => {
61
+ const isAuthenticatedRoute = branch.some((route) =>
62
+ route.id.split("/").includes("_authenticated"),
63
+ );
64
+
65
+ return isAuthenticatedRoute
66
+ ? "authenticated"
67
+ : "unauthenticated";
68
+ };
69
+
70
+ export function myCoolPreset(): Preset {
71
+ return {
72
+ name: "my-cool-preset",
73
+ reactRouterConfig: () => ({ serverBundles }),
74
+ reactRouterConfigResolved: ({ reactRouterConfig }) => {
75
+ if (
76
+ reactRouterConfig.serverBundles !== serverBundles
77
+ ) {
78
+ throw new Error("`serverBundles` was overridden!");
79
+ }
80
+ },
81
+ };
82
+ }
83
+ ```
84
+
85
+ The `reactRouterConfigResolved` hook should only be used when it would be an error to merge or override your preset's config.
86
+
87
+ ## Using a preset
88
+
89
+ Presets are designed to be published to npm and used within your React Router config.
90
+
91
+ ```ts filename=react-router.config.ts lines=[6]
92
+ import type { Config } from "@react-router/dev/config";
93
+ import { myCoolPreset } from "react-router-preset-cool";
94
+
95
+ export default {
96
+ // ...
97
+ presets: [myCoolPreset()],
98
+ } satisfies Config;
99
+ ```
100
+
101
+ [react-router-config]: https://api.reactrouter.com/v7/types/_react-router_dev.config.Config.html
102
+ [preset-type]: https://api.reactrouter.com/v7/types/_react-router_dev.config.Preset.html
103
+ [server-bundles]: ./server-bundles