You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/how-to/pre-rendering.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,9 @@ title: Pre-Rendering
6
6
7
7
Pre-rendering allows you to render pages at build time instead of on a runtime server to speed up page loads for static content.
8
8
9
-
In some cases, you'll serve these pages _alongside_ a runtime SSR server. If you wish to pre-render pages and deploy them _without_ a runtime SSR server, please see the [Pre-rendering with `ssr:false`](#Pre-rendering-with-ssrfalse) section below.
9
+
In some cases, you'll serve these pages _alongside_ a runtime SSR server. If you wish to pre-render pages and deploy them _without_ a runtime SSR server, please see the [Pre-rendering with `ssr:false`](#pre-rendering-without-a-runtime-ssr-server) section below.
During development, pre-rendering doesn't save the rendered results to the public directory, this only happens for `react-router build`.
84
84
85
-
## Pre-rendering with `ssr:false`
85
+
## Pre-rendering without a runtime SSR server
86
86
87
87
The above examples assume you are deploying a runtime server, but are pre-rendering some static pages in order to serve them faster and avoid hitting the server.
88
88
@@ -99,7 +99,7 @@ export default {
99
99
100
100
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.
101
101
102
-
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. Usually, with `prerender:true`, you'll be pre-rendering all of your application routes into a full SSG setup.
102
+
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.
103
103
104
104
### Pre-rendering with a SPA Fallback
105
105
@@ -126,10 +126,12 @@ export default {
126
126
} satisfiesConfig;
127
127
```
128
128
129
-
You can configure your deployment server to serve this file for any path that otherwise would 404.
130
-
131
-
Here's an example of how you can do this with the [`sirv-cli`](https://www.npmjs.com/package/sirv-cli#user-content-single-page-applications) tool:
129
+
You can configure your deployment server to serve this file for any path that otherwise would 404. Here's an example of how you can do this with the [`sirv-cli`](https://www.npmjs.com/package/sirv-cli#user-content-single-page-applications) tool:
Copy file name to clipboardExpand all lines: docs/how-to/spa.md
+29-3Lines changed: 29 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,33 @@ export default {
23
23
24
24
With this set to false, the server build will no longer be generated.
25
25
26
-
## 2. Use client loaders and client actions
26
+
## 2. Add a `HydrateFallback` to your root route
27
+
28
+
SPA Mode will generate an `index.html` file at build-time that you can serve as the entry point for your SPA. This will only render the root route so that it is capable of hydrating at runtime for any path in your application.
29
+
30
+
To provide a better/faster loading UI, you can add a `HydrateFallback` component to your root route to render your loading UI into the `index.html` at build time. This way, it will be shown to users immediately while the SPA is loading/hydrating.
31
+
32
+
```tsx filename=root.tsx
33
+
import { Route } from"./+types/root";
34
+
importAwesomeSpinnerfrom"./components/spinner";
35
+
36
+
exportfunction Layout() {
37
+
return <html>{/*...*/}</html>;
38
+
}
39
+
40
+
// Server-rendered at build time into `index.html` (inside `<Layout>`)
41
+
exportfunction HydrateFallback() {
42
+
return <AwesomeSpinner />;
43
+
}
44
+
45
+
exportdefaultfunction App() {
46
+
return <Outlet />;
47
+
}
48
+
```
49
+
50
+
Because the root route is server-rendered at build time, you can also use a `loader` in your root route if you choose, and access the data via the optional `HydrateFallback``loaderData` prop. You cannot in include a loader in any other routes in your app when using SPA Mode.
51
+
52
+
## 3. Use client loaders and client actions
27
53
28
54
With server rendering disabled, you can still use `clientLoader` and `clientAction` to manage route data and mutations.
29
55
@@ -45,11 +71,11 @@ export async function clientAction({
45
71
}
46
72
```
47
73
48
-
## 3. Pre-rendering
74
+
## 4. Pre-rendering
49
75
50
76
Pre-rendering can be configured for paths with static data known at build time for faster initial page loads. Refer to [Pre-rendering](./pre-rendering) to set it up.
51
77
52
-
## 4. Direct all URLs to index.html
78
+
## 5. Direct all URLs to index.html
53
79
54
80
After running `react-router build`, deploy the `build/client` directory to whatever static host you prefer.
0 commit comments