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
Enhance invalid export detection when using `ssr:false`
6
+
7
+
-`headers`/`action` are prohibited in all routes with `ssr:false` because there will be no runtime server on which to run them
8
+
-`loader` functions are more nuanced and depend on whether a given route is prerendered
9
+
- When using `ssr:false` without a `prerender` config, only the `root` route can have a `loader`
10
+
- This is "SPA mode" which generates a single `index.html` file with the root route `HydrateFallback` so it is capable of hydrating for any path in your application - therefore we can only call a root route `loader` at build time
11
+
- When using `ssr:false` with a `prerender` config, you can export a `loader` from routes matched by one of the `prerender` paths because those routes will be server rendered at build time
12
+
- Exporting a `loader` from a route that is never matched by a `prerender` path will throw a build time error because there will be no runtime server to ever run the loader
Generate a "SPA fallback" HTML file for scenarios where applications are prerendering the `/` route with `ssr:false`
6
+
7
+
- If you specify `ssr:false` without a `prerender` config, this is considered "SPA Mode" and the generated `index.html` file will only render down to the root route and will be able to hydrate for any valid application path
8
+
- If you specify `ssr:false` with a `prerender` config but _do not_ include the `/` path (i.e., `prerender: ['/blog/post']`), then we still generate a "SPA Mode" `index.html` file that can hydrate for any path in the application
9
+
- However, previously if you specified `ssr:false` and included the `/` path in your `prerender` config, we would prerender the `/` route into `index.html` as a non-SPA page
10
+
- The generated HTML would include the root index route which prevented hydration for any other paths
11
+
- With this change, we now generate a "SPA Mode" file in `__spa-fallback.html` that will allow you to hydrate for any non-prerendered paths
12
+
- You can serve this file from your static file server for any paths that would otherwise 404 if you only want to pre-render _some_ routes in your `ssr:false` app and serve the others as a SPA
Copy file name to clipboardExpand all lines: docs/how-to/pre-rendering.md
+65-6
Original file line number
Diff line number
Diff line change
@@ -4,16 +4,23 @@ 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 server to speed up pages loads for static content.
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
-
## Configuration
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.
10
+
11
+
## Pre-rendering with ssr:true
12
+
13
+
### Configuration
10
14
11
15
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
+
85
+
## Pre-rendering with `ssr:false`
86
+
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
+
89
+
To disable runtime SSR, you can set the `ssr:false` config flag:
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
+
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.
103
+
104
+
### Pre-rendering with a SPA Fallback
105
+
106
+
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.
107
+
108
+
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)).
109
+
110
+
This will be written to one of the following paths:
111
+
112
+
-`build/client/index.html` - If the `/` path is not pre-rendered
113
+
-`build/client/__spa-fallback.html` - If the `/` path is pre-rendered
@@ -65,10 +65,11 @@ If you're getting 404s at valid routes for your app, it's likely you need to con
65
65
66
66
Typical Single Pages apps send a mostly blank `index.html` template with little more than an empty `<div id="root"></div>`.
67
67
68
-
In contrast `react-router build` (with server rendering disabled) pre-renders your root and index routes. This means you can:
68
+
In contrast `react-router build` (with server rendering disabled) pre-renders your root route at build time. This means you can:
69
69
70
70
- Send more than an empty div
71
-
- Use React components to generate the initial page users see
71
+
- Use a root `loader` to load data for your application shell
72
+
- Use React components to generate the initial page users see (root `HydrateFallback`)
72
73
- Re-enable server rendering later without changing anything about your UI
73
74
74
-
React Router will still server render your index route to generate that`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.
75
+
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