Skip to content

Commit 51071ac

Browse files
hi-ogawaclaude
andcommitted
docs: add preamble documentation for plugin-react-swc
- Add preamble section to plugin-react-swc README - Update CHANGELOG entries for both plugin-react and plugin-react-swc - Document the new virtual module feature for SSR HMR setup 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 37677e6 commit 51071ac

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

packages/plugin-react-swc/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Add `@vitejs/plugin-react-swc/preamble` virtual module for SSR HMR ([#890](https://github.com/vitejs/vite-plugin-react/pull/890))
6+
7+
SSR applications can now initialize HMR runtime by importing `@vitejs/plugin-react-swc/preamble` at the top of their client entry instead of manually calling `transformIndexHtml`. This simplifies SSR setup for applications that don't use the `transformIndexHtml` API.
8+
59
## 4.1.0 (2025-09-17)
610

711
### Set SWC cacheRoot options

packages/plugin-react-swc/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,38 @@ If set, disables the recommendation to use `@vitejs/plugin-react-oxc` (which is
125125
react({ disableOxcRecommendation: true })
126126
```
127127

128+
## `@vitejs/plugin-react-swc/preamble`
129+
130+
The package provides `@vitejs/plugin-react-swc/preamble` to initialize HMR runtime from client entrypoint for SSR applications which don't use [`transformIndexHtml` API](https://vite.dev/guide/api-javascript.html#vitedevserver). For example:
131+
132+
```js
133+
// [entry.client.js]
134+
import '@vitejs/plugin-react-swc/preamble'
135+
```
136+
137+
Alternatively, you can manually call `transformIndexHtml` during SSR, which sets up equivalent initialization code. Here's an example for an Express server:
138+
139+
```js
140+
app.get('/', async (req, res, next) => {
141+
try {
142+
let html = fs.readFileSync(path.resolve(root, 'index.html'), 'utf-8')
143+
144+
// Transform HTML using Vite plugins.
145+
html = await viteServer.transformIndexHtml(req.url, html)
146+
147+
res.send(html)
148+
} catch (e) {
149+
return next(e)
150+
}
151+
})
152+
```
153+
154+
Otherwise, you'll get the following error:
155+
156+
```
157+
Uncaught Error: @vitejs/plugin-react-swc can't detect preamble. Something is wrong.
158+
```
159+
128160
## Consistent components exports
129161

130162
For React refresh to work correctly, your file should only export React components. The best explanation I've read is the one from the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works).

packages/plugin-react/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Add `@vitejs/plugin-react/preamble` virtual module for SSR HMR ([#890](https://github.com/vitejs/vite-plugin-react/pull/890))
6+
7+
SSR applications can now initialize HMR runtime by importing `@vitejs/plugin-react/preamble` at the top of their client entry instead of manually calling `transformIndexHtml`. This simplifies SSR setup for applications that don't use the `transformIndexHtml` API.
8+
59
## 5.0.4 (2025-09-27)
610

711
### Perf: use native refresh wrapper plugin in rolldown-vite ([#881](https://github.com/vitejs/vite-plugin-react/pull/881))

0 commit comments

Comments
 (0)