From 96372e6beb976b57a8c52fd7c65f126899325160 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Mon, 21 Mar 2022 23:14:58 -0400 Subject: [PATCH] Fix: `isSelfAccepting`? More like `isBanishedToTheShadowRealm` (#2852) * fix: restore renderer caching strategy * fix: restore old URL constructor for HMR * docs: comment why we need the rendererCache * refactor: remove needless "else" * chore: changeset --- .changeset/forty-badgers-relate.md | 5 +++++ packages/astro/src/core/render/dev/index.ts | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changeset/forty-badgers-relate.md diff --git a/.changeset/forty-badgers-relate.md b/.changeset/forty-badgers-relate.md new file mode 100644 index 0000000000..ebe258be4e --- /dev/null +++ b/.changeset/forty-badgers-relate.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix "isSelfAccepting" exception when using the new @astrojs/react integration in development diff --git a/packages/astro/src/core/render/dev/index.ts b/packages/astro/src/core/render/dev/index.ts index 09abb7d7bb..0cf909caf8 100644 --- a/packages/astro/src/core/render/dev/index.ts +++ b/packages/astro/src/core/render/dev/index.ts @@ -40,10 +40,21 @@ export type ComponentPreload = [SSRLoadedRenderer[], ComponentInstance]; export type RenderResponse = { type: 'html'; html: string } | { type: 'response'; response: Response }; const svelteStylesRE = /svelte\?svelte&type=style/; +// Cache renderers to avoid re-resolving the module using Vite's `ssrLoadModule` +// This prevents an odd exception trying to resolve the same server-side module +// Multiple times. See `isSelfAccepting` issue: https://github.com/withastro/astro/pull/2852 +const rendererCache = new Map(); async function loadRenderer(viteServer: vite.ViteDevServer, renderer: AstroRenderer): Promise { const { url } = await viteServer.moduleGraph.ensureEntryFromUrl(renderer.serverEntrypoint); + + const cachedRenderer = rendererCache.get(url); + if (cachedRenderer) { + return { ...renderer, ssr: cachedRenderer }; + } + const mod = (await viteServer.ssrLoadModule(url)) as { default: SSRLoadedRenderer['ssr'] }; + rendererCache.set(url, mod.default); return { ...renderer, ssr: mod.default }; } @@ -75,7 +86,7 @@ export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInsta children: '', }); scripts.add({ - props: { type: 'module', src: '/@id/astro/client/hmr.js' }, + props: { type: 'module', src: new URL('../../../runtime/client/hmr.js', import.meta.url).pathname }, children: '', }); }