diff options
Diffstat (limited to 'packages/astro/src/vite-plugin-hmr-reload/index.ts')
-rw-r--r-- | packages/astro/src/vite-plugin-hmr-reload/index.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/packages/astro/src/vite-plugin-hmr-reload/index.ts b/packages/astro/src/vite-plugin-hmr-reload/index.ts new file mode 100644 index 000000000..cf151eef6 --- /dev/null +++ b/packages/astro/src/vite-plugin-hmr-reload/index.ts @@ -0,0 +1,36 @@ +import type { EnvironmentModuleNode, Plugin } from 'vite'; + +/** + * The very last Vite plugin to reload the browser if any SSR-only module are updated + * which will require a full page reload. This mimics the behaviour of Vite 5 where + * it used to unconditionally reload for us. + */ +export default function hmrReload(): Plugin { + return { + name: 'astro:hmr-reload', + enforce: 'post', + hotUpdate: { + order: 'post', + handler({ modules, server, timestamp }) { + if (this.environment.name !== 'ssr') return; + + let hasSsrOnlyModules = false; + + const invalidatedModules = new Set<EnvironmentModuleNode>(); + for (const mod of modules) { + if (mod.id == null) continue; + const clientModule = server.environments.client.moduleGraph.getModuleById(mod.id); + if (clientModule != null) continue; + + this.environment.moduleGraph.invalidateModule(mod, invalidatedModules, timestamp, true); + hasSsrOnlyModules = true; + } + + if (hasSsrOnlyModules) { + server.ws.send({ type: 'full-reload' }); + return []; + } + }, + }, + }; +} |