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/guide/ssr.md
+29-16
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Server-Side Rendering
2
2
3
-
Vite provides built-in support for server-side rendering (SSR).
3
+
:::warning Experimental
4
+
SSR support is still experimental and you may encounter bugs and unsupported use cases. Proceed at your own risk.
5
+
:::
4
6
5
7
:::tip Note
6
8
SSR specifically refers to front-end frameworks (for example React, Preact, Vue, and Svelte) that support running the same application in Node.js, pre-rendering it to HTML, and finally hydrating it on the client. If you are looking for integration with traditional server-side frameworks, check out the [Backend Integration guide](./backend-integration) instead.
@@ -10,7 +12,7 @@ The following guide also assumes prior experience working with SSR in your frame
10
12
11
13
## Example Projects
12
14
13
-
The Vite playground contains example SSR setups for Vue 3 and React, which can be used as references for this guide:
15
+
Vite provides built-in support for server-side rendering (SSR). The Vite playground contains example SSR setups for Vue 3 and React, which can be used as references for this guide:
@@ -36,9 +38,17 @@ The `index.html` will need to reference `entry-client.js` and include a placehol
36
38
37
39
You can use any placeholder you prefer instead of `<!--app-html-->`, as long as it can be precisely replaced.
38
40
39
-
:::tip
40
-
If you need to perform conditional logic based on SSR vs. client, you can use `import.meta.env.SSR`. This is statically replaced during build so it will allow tree-shaking of unused branches.
41
-
:::
41
+
## Conditional Logic
42
+
43
+
If you need to perform conditional logic based on SSR vs. client, you can use
44
+
45
+
```js
46
+
if (import.meta.env.SSR) {
47
+
// ... server only logic
48
+
}
49
+
```
50
+
51
+
This is statically replaced during build so it will allow tree-shaking of unused branches.
42
52
43
53
## Setting Up the Dev Server
44
54
@@ -158,26 +168,25 @@ Then, in `server.js` we need to add some production specific logic by checking `
158
168
159
169
Refer to the [Vue](https://github.com/vitejs/vite/tree/main/packages/playground/ssr-vue) and [React](https://github.com/vitejs/vite/tree/main/packages/playground/ssr-react) demos for working setup.
160
170
161
-
### Generating Preload Directives
162
-
163
-
> This section is Vue-specific.
171
+
## Generating Preload Directives
164
172
165
-
`@vitejs/plugin-vue` automatically registers component module IDs that are instantiated during a request render to the associated Vue SSR context. This information can be used to infer async chunks and assets that should be preloaded for a given route.
166
-
167
-
In order to leverage this, add the `--ssrManifest` flag to the client build script (Yes, the SSR manifest is generated from the client build because we want to map module IDs to client files):
173
+
`vite build` supports the `--ssrManifest` flag which will generate `ssr-manifest.json` in build output directory:
This will generate a `dist/client/ssr-manifest.json` file that contains mappings of module IDs to their associated chunks and asset files.
180
+
The above script will now generate `dist/client/ssr-manifest.json` for the client build (Yes, the SSR manifest is generated from the client build because we want to map module IDs to client files). The manifest contains mappings of module IDs to their associated chunks and asset files.
181
+
182
+
To leverage the manifest, frameworks need to provide a way to collect the module IDs of the components that were used during a server render call.
175
183
176
-
Then, in `src/entry-server.js`:
184
+
`@vitejs/plugin-vue` supports this out of the box and automatically registers used component module IDs on to the associated Vue SSR context:
// ctx.modules is now a Set of module IDs that were used during the render
182
191
```
183
192
@@ -187,7 +196,7 @@ We need to now read and pass the manifest to the `render` function exported by `
187
196
188
197
If the routes and the data needed for certain routes are known ahead of time, we can pre-render these routes into static HTML using the same logic as production SSR. This is also known as Static-Site Generation (SSG). See [demo pre-render script](https://github.com/vitejs/vite/blob/main/packages/playground/ssr-vue/prerender.js) for working example.
189
198
190
-
## Externals Heuristics
199
+
## SSR Externals
191
200
192
201
Many dependencies ship both ESM and CommonJS files. When running SSR, a dependency that provides CommonJS builds can be "externalized" from Vite's SSR transform / module system to speed up both dev and build. For example, instead of pulling in the pre-bundled ESM version of React and then transforming it back to be Node.js-compatible, it is more efficient to simply `require('react')` instead. It also greatly improves the speed of the SSR bundle build.
193
202
@@ -201,6 +210,10 @@ If this heuristics leads to errors, you can manually adjust SSR externals using
201
210
202
211
In the future, this heuristics will likely improve to also externalize dependencies that ship Node-compatible ESM builds (and `import()` them during SSR module load).
203
212
213
+
:::warning Working with Aliases
214
+
If you have configured alises that redirects one package to another, you may want to alias the actual `node_modules` packages instead in order to make it work for SSR externalized dependencies. Both [Yarn](https://classic.yarnpkg.com/en/docs/cli/add/#toc-yarn-add-alias) and [pnpm](https://pnpm.js.org/en/aliases) support aliasing via the `npm:` prefix.
215
+
:::
216
+
204
217
## SSR-specific Plugin Logic
205
218
206
219
Some frameworks such as Vue or Svelte compiles components into different formats based on client vs. SSR. To support conditional transforms, Vite passes an additional `ssr` argument to the following plugin hooks:
@@ -222,4 +235,4 @@ export function mySSRPlugin() {
0 commit comments