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
Lazy load Cloudflare platform proxy on first dev server request when using the `cloudflareDevProxy` Vite plugin to avoid creating unnecessary workerd processes
Copy file name to clipboardExpand all lines: docs/how-to/pre-rendering.md
+30-16Lines changed: 30 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -4,43 +4,45 @@ title: Pre-Rendering
4
4
5
5
# Pre-Rendering
6
6
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.
7
+
Pre-Rendering allows you to speed up page loads for static content by rendering pages at build time instead of at runtime. Pre-rendering is enabled via the `prerender` config in `react-router.config.ts` and can be used in two ways based on the `ssr` config value:
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-without-a-runtime-ssr-server) section below.
9
+
- Alongside a runtime SSR server ith `ssr:true` (the default value)
10
+
- Deployed to a static file server with `ssr:false`
10
11
11
-
## Pre-rendering alongside a runtime SSR server
12
+
## Pre-rendering with `ssr:true`
12
13
13
14
### Configuration
14
15
15
16
Add the `prerender` option to your config, there are three signatures:
During development, pre-rendering doesn't save the rendered results to the public directory, this only happens for `react-router build`.
84
86
85
-
## Pre-rendering without a runtime SSR server
87
+
## Pre-rendering with `ssr:false`
86
88
87
89
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
90
89
-
To disable runtime SSR, you can set the `ssr:false` config flag:
91
+
To disable runtime SSR and configure pre-rendering to be served from a static file server, you can set the `ssr:false` config flag:
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:
131
+
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:
132
+
133
+
```
134
+
# If you did not pre-render the `/` route
135
+
/* /index.html 200
136
+
137
+
# If you pre-rendered the `/` route
138
+
/* /__spa-fallback.html 200
139
+
```
140
+
141
+
If you're getting 404s at valid routes for your app, it's likely you need to configure your host.
142
+
143
+
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:
Copy file name to clipboardExpand all lines: docs/how-to/spa.md
+48-27Lines changed: 48 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,22 @@ There are two ways to ship a single page app with React Router
9
9
-**as a library** - Instead of using React Router's framework features, you can use it as a library in your own SPA architecture. Refer to [React Router as a Library](../start/library/installation) guides.
10
10
-**as a framework** - This guide will focus here
11
11
12
-
## 1. Disable Server Rendering
12
+
## Overview
13
+
14
+
When using React Router as a framework, you can enable "SPA Mode" by setting `ssr:false` in your `react-router.config.ts` file. This will disable runtime server rendering and generate an `index.html` at build time that you can serve and hydrate as a SPA.
15
+
16
+
Typical Single Page apps send a mostly blank `index.html` template with little more than an empty `<div id="root"></div>`. In contrast, `react-router build` (in SPA Mode) pre-renders your root route at build time into an `index.html` file. This means you can:
17
+
18
+
- Send more than an empty `<div>`
19
+
- Use a root `loader` to load data for your application shell
20
+
- Use React components to generate the initial page users see (root `HydrateFallback`)
21
+
- Re-enable server rendering later without changing anything about your UI
22
+
23
+
It's important to note that setting `ssr:false` only disables _runtime server rendering_. React Router will still server render your root route at _build time_ to generate the `index.html` file. This is why your project still needs a dependency on `@react-router/node` and your routes need to be SSR-safe. That means you can't call `window` or other browser-only APIs during the initial render, even when server rendering is disabled.
24
+
25
+
<docs-info>SPA Mode is a special form of "Pre-Rendering" that allows you to serve all paths in your application from the same HTML file. Please refer to the [Pre-Rendering](./pre-rendering) guide if you want to do more extensive pre-rendering.</docs-info>
26
+
27
+
## 1. Disable Runtime Server Rendering
13
28
14
29
Server rendering is enabled by default. Set the `ssr` flag to `false` in `react-router.config.ts` to disable it.
15
30
@@ -23,31 +38,54 @@ export default {
23
38
24
39
With this set to false, the server build will no longer be generated.
25
40
26
-
## 2. Add a `HydrateFallback` to your root route
41
+
<docs-info>It's important to note that setting `ssr:false` only disables _runtime server rendering_. React Router will still server render your root route at _build time_ to generate the `index.html` file. This is why your project still needs a dependency on `@react-router/node` and your routes need to be SSR-safe. That means you can't call `window` or other browser-only APIs during the initial render, even when server rendering is disabled.</docs-info>
42
+
43
+
## 2. Add a `HydrateFallback` and optional `loader` to your root route
27
44
28
45
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
46
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.
47
+
To provide a better loading UI than an empty `<div>`, 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.
// Server-rendered at build time into `index.html` (inside `<Layout>`)
41
56
exportfunction HydrateFallback() {
42
-
return <AwesomeSpinner />;
57
+
return <LoadingScreen />;
43
58
}
44
59
45
60
exportdefaultfunction App() {
46
61
return <Outlet />;
47
62
}
48
63
```
49
64
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.
65
+
Because the root route is server-rendered at build time, you can also use a `loader` in your root route if you choose. This `loader` will be called at build time ans the data will be available via the optional `HydrateFallback``loaderData` prop.
66
+
67
+
```tsx filename=root.tsx lines=[5,10,14]
68
+
import { Route } from"./+types/root";
69
+
70
+
exportasyncfunction loader() {
71
+
return {
72
+
version: awaitgetVersion(),
73
+
};
74
+
}
75
+
76
+
exportfunction HydrateFallback({
77
+
loaderData,
78
+
}:Route.ComponentProps) {
79
+
return (
80
+
<div>
81
+
<h1>Loading version {loaderData.version}...</h1>
82
+
<AwesomeSpinner />
83
+
</div>
84
+
);
85
+
}
86
+
```
87
+
88
+
You cannot include a `loader` in any other routes in your app when using SPA Mode unless you are [pre-rendering those pages](./pre-rendering).
51
89
52
90
## 3. Use client loaders and client actions
53
91
@@ -71,11 +109,7 @@ export async function clientAction({
71
109
}
72
110
```
73
111
74
-
## 4. Pre-rendering
75
-
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.
77
-
78
-
## 5. Direct all URLs to index.html
112
+
## 4. Direct all URLs to index.html
79
113
80
114
After running `react-router build`, deploy the `build/client` directory to whatever static host you prefer.
81
115
@@ -86,16 +120,3 @@ Common to deploying any SPA, you'll need to configure your host to direct all UR
86
120
```
87
121
88
122
If you're getting 404s at valid routes for your app, it's likely you need to configure your host.
89
-
90
-
## Important Note
91
-
92
-
Typical Single Pages apps send a mostly blank `index.html` template with little more than an empty `<div id="root"></div>`.
93
-
94
-
In contrast `react-router build` (with server rendering disabled) pre-renders your root route at build time. This means you can:
95
-
96
-
- Send more than an empty div
97
-
- Use a root `loader` to load data for your application shell
98
-
- Use React components to generate the initial page users see (root `HydrateFallback`)
99
-
- Re-enable server rendering later without changing anything about your UI
100
-
101
-
Therefore, setting `ssr:false` only disables _runtime server rendering_. React Router will still server render your index route at _build time_ to generate the `index.html` file. This is why your project still needs a dependency on `@react-router/node` and your routes need to be SSR-safe. That means you can't call `window` or other browser-only APIs during the initial render, even when server rendering is disabled.
0 commit comments