Skip to content

Commit 97183a3

Browse files
authored
docs: fix some mistakes in Environment API guides (#20000)
1 parent ddb948a commit 97183a3

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

guide/api-environment-frameworks.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Note that although the `FetchableDevEnvironment` is implemented as a class, it i
8484

8585
## Default `RunnableDevEnvironment`
8686

87-
Given a Vite server configured in middleware mode as described by the [SSR setup guide](/guide/ssr#setting-up-the-dev-server), let's implement the SSR middleware using the environment API. Error handling is omitted.
87+
Given a Vite server configured in middleware mode as described by the [SSR setup guide](/guide/ssr#setting-up-the-dev-server), let's implement the SSR middleware using the environment API. Remember that it doesn't have to be called `ssr`, so we'll name it `server` in this example. Error handling is omitted.
8888

8989
```js
9090
import fs from 'node:fs'
@@ -94,7 +94,7 @@ import { createServer } from 'vite'
9494

9595
const __dirname = path.dirname(fileURLToPath(import.meta.url))
9696

97-
const server = await createServer({
97+
const viteServer = await createServer({
9898
server: { middlewareMode: true },
9999
appType: 'custom',
100100
environments: {
@@ -106,7 +106,7 @@ const server = await createServer({
106106

107107
// You might need to cast this to RunnableDevEnvironment in TypeScript or
108108
// use isRunnableDevEnvironment to guard the access to the runner
109-
const environment = server.environments.node
109+
const serverEnvironment = viteServer.environments.server
110110

111111
app.use('*', async (req, res, next) => {
112112
const url = req.originalUrl
@@ -118,12 +118,14 @@ app.use('*', async (req, res, next) => {
118118
// 2. Apply Vite HTML transforms. This injects the Vite HMR client,
119119
// and also applies HTML transforms from Vite plugins, e.g. global
120120
// preambles from @vitejs/plugin-react
121-
template = await server.transformIndexHtml(url, template)
121+
template = await viteServer.transformIndexHtml(url, template)
122122

123123
// 3. Load the server entry. import(url) automatically transforms
124124
// ESM source code to be usable in Node.js! There is no bundling
125125
// required, and provides full HMR support.
126-
const { render } = await environment.runner.import('/src/entry-server.js')
126+
const { render } = await serverEnvironment.runner.import(
127+
'/src/entry-server.js',
128+
)
127129

128130
// 4. render the app HTML. This assumes entry-server.js's exported
129131
// `render` function calls appropriate framework SSR APIs,

guide/api-environment-instances.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ During dev, the available environments in a dev server can be accessed using `se
1919
// create the server, or get it from the configureServer hook
2020
const server = await createServer(/* options */)
2121

22-
const environment = server.environments.client
23-
environment.transformRequest(url)
22+
const clientEnvironment = server.environments.client
23+
clientEnvironment.transformRequest(url)
2424
console.log(server.environments.ssr.moduleGraph)
2525
```
2626

guide/api-environment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ For a simple SPA/MPA, no new APIs around environments are exposed to the config.
2222
When we move to a typical server-side rendered (SSR) app, we'll have two environments:
2323

2424
- `client`: runs the app in the browser.
25-
- `server`: runs the app in node (or other server runtimes) which renders pages before sending them to the browser.
25+
- `ssr`: runs the app in node (or other server runtimes) which renders pages before sending them to the browser.
2626

2727
In dev, Vite executes the server code in the same Node process as the Vite dev server, giving a close approximation to the production environment. However, it is also possible for servers to run in other JS runtimes, like [Cloudflare's workerd](https://github.com/cloudflare/workerd) which have different constraints. Modern apps may also run in more than two environments, e.g. a browser, a node server, and an edge server. Vite 5 didn't allow to properly represent these environments.
2828

0 commit comments

Comments
 (0)