Skip to content

Commit cdab0a7

Browse files
committed
docs: ssr docs edits
1 parent 8ec2d6f commit cdab0a7

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

docs/config/index.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,11 @@ export default ({ command, mode }) => {
524524

525525
## SSR Options
526526

527-
- **Related:** [SSR Externals Heuristics](/guide/ssr#externals-heuristics)
527+
:::warning Experimental
528+
SSR options may be adjusted in minor releases.
529+
:::
530+
531+
- **Related:** [SSR Externals](/guide/ssr#ssr-externals)
528532

529533
### ssr.external
530534

docs/guide/ssr.md

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Server-Side Rendering
22

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+
:::
46

57
:::tip Note
68
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
1012

1113
## Example Projects
1214

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:
1416

1517
- [Vue 3](https://github.com/vitejs/vite/tree/main/packages/playground/ssr-vue)
1618
- [React](https://github.com/vitejs/vite/tree/main/packages/playground/ssr-react)
@@ -36,9 +38,17 @@ The `index.html` will need to reference `entry-client.js` and include a placehol
3638

3739
You can use any placeholder you prefer instead of `<!--app-html-->`, as long as it can be precisely replaced.
3840

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.
4252
4353
## Setting Up the Dev Server
4454
@@ -158,26 +168,25 @@ Then, in `server.js` we need to add some production specific logic by checking `
158168
159169
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.
160170
161-
### Generating Preload Directives
162-
163-
> This section is Vue-specific.
171+
## Generating Preload Directives
164172
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:
168174
169175
```diff
170176
- "build:client": "vite build --outDir dist/client",
171177
+ "build:client": "vite build --outDir dist/client --ssrManifest",
172178
```
173179
174-
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.
175183
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:
177185
178186
```js
187+
// src/entry-server.js
179188
const ctx = {}
180-
const html = await renderToString(app, ctx)
189+
const html = await vueServerRenderer.renderToString(app, ctx)
181190
// ctx.modules is now a Set of module IDs that were used during the render
182191
```
183192
@@ -187,7 +196,7 @@ We need to now read and pass the manifest to the `render` function exported by `
187196
188197
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.
189198
190-
## Externals Heuristics
199+
## SSR Externals
191200
192201
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.
193202
@@ -201,6 +210,10 @@ If this heuristics leads to errors, you can manually adjust SSR externals using
201210
202211
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).
203212
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+
204217
## SSR-specific Plugin Logic
205218
206219
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() {
222235
}
223236
}
224237
}
225-
```
238+
```

0 commit comments

Comments
 (0)