Skip to content

Commit 18b9b31

Browse files
authored
docs(hmr): improve handleHotUpdate and add further reading (#15996)
1 parent f3b195c commit 18b9b31

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

docs/guide/api-hmr.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,9 @@ Send custom events back to Vite's dev server.
206206
If called before connected, the data will be buffered and sent once the connection is established.
207207
208208
See [Client-server Communication](/guide/api-plugin.html#client-server-communication) for more details.
209+
210+
## Further Reading
211+
212+
If you'd like to learn more about how to use the HMR API and how it works under-the-hood. Check out these resources:
213+
214+
- [Hot Module Replacement is Easy](https://bjornlu.com/blog/hot-module-replacement-is-easy)

docs/guide/api-plugin.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
402402
### `handleHotUpdate`
403403

404404
- **Type:** `(ctx: HmrContext) => Array<ModuleNode> | void | Promise<Array<ModuleNode> | void>`
405+
- **See also:** [HMR API](./api-hmr)
405406

406407
Perform custom HMR update handling. The hook receives a context object with the following signature:
407408

@@ -423,10 +424,31 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
423424

424425
- Filter and narrow down the affected module list so that the HMR is more accurate.
425426

426-
- Return an empty array and perform complete custom HMR handling by sending custom events to the client (example uses `server.hot` which was introduced in Vite 5.1, it is recommended to also use `server.ws` if you support lower versions):
427+
- Return an empty array and perform a full reload:
428+
429+
```js
430+
handleHotUpdate({ server, modules, timestamp }) {
431+
// Also use `server.ws.send` to support Vite <5.1 if needed
432+
server.hot.send({ type: 'full-reload' })
433+
// Invalidate modules manually
434+
const invalidatedModules = new Set()
435+
for (const mod of modules) {
436+
server.moduleGraph.invalidateModule(
437+
mod,
438+
invalidatedModules,
439+
timestamp,
440+
true
441+
)
442+
}
443+
return []
444+
}
445+
```
446+
447+
- Return an empty array and perform complete custom HMR handling by sending custom events to the client:
427448

428449
```js
429450
handleHotUpdate({ server }) {
451+
// Also use `server.ws.send` to support Vite <5.1 if needed
430452
server.hot.send({
431453
type: 'custom',
432454
event: 'special-update',

0 commit comments

Comments
 (0)