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: '', }); }