diff --git a/.changeset/beige-hairs-design.md b/.changeset/beige-hairs-design.md new file mode 100644 index 000000000..df606eba5 --- /dev/null +++ b/.changeset/beige-hairs-design.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix HMR regression diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index c250e8ca5..8aedb08b9 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -22,7 +22,7 @@ const ALWAYS_EXTERNAL = new Set([ 'shiki', 'shorthash', 'unified', - 'whatwg-url', + 'whatwg-url' ]); const ALWAYS_NOEXTERNAL = new Set([ 'astro', // This is only because Vite's native ESM doesn't resolve "exports" correctly. diff --git a/packages/astro/src/core/ssr/index.ts b/packages/astro/src/core/ssr/index.ts index d3f71a7b6..a8ff5910b 100644 --- a/packages/astro/src/core/ssr/index.ts +++ b/packages/astro/src/core/ssr/index.ts @@ -225,7 +225,9 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO tags.push({ tag: 'script', attrs: { type: 'module' }, - children: `import 'astro/runtime/client/hmr.js';`, + // HACK: inject the direct contents of our `astro/runtime/client/hmr.js` to ensure + // `import.meta.hot` is properly handled by Vite + children: await getHmrScript(), injectTo: 'head', }); } @@ -255,6 +257,15 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO return html; } +let hmrScript: string; +async function getHmrScript() { + if (hmrScript) return hmrScript; + const filePath = fileURLToPath(new URL('../../runtime/client/hmr.js', import.meta.url)); + const content = await fs.promises.readFile(filePath); + hmrScript = content.toString(); + return hmrScript; +} + export async function ssr(ssrOpts: SSROptions): Promise { try { const [renderers, mod] = await preload(ssrOpts);