fix: HMR regression (#1858)

* fix: HMR regression

* fix: inject HMR script directly
This commit is contained in:
Nate Moore 2021-11-17 14:10:05 -06:00 committed by GitHub
parent e1e12eb7fe
commit dbc49ed62c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix HMR regression

View file

@ -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.

View file

@ -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<string> {
try {
const [renderers, mod] = await preload(ssrOpts);