Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: is self accepting pt 2 module graph boogaloo #2872

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/wise-garlics-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/preact': patch
---

Fix `isSelfAccepting` errors when using the Preact integration with the Astro dev server
18 changes: 5 additions & 13 deletions packages/astro/src/core/render/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,13 @@ 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<string, SSRLoadedRenderer['ssr']>();

async function loadRenderer(viteServer: vite.ViteDevServer, renderer: AstroRenderer): Promise<SSRLoadedRenderer> {
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);
// Vite modules can be out-of-date when using an un-resolved url
// We also encountered inconsistencies when using the resolveUrl and resolveId helpers
// We've found that pulling the ID directly from the urlToModuleMap is the most stable!
const id = viteServer.moduleGraph.urlToModuleMap.get(renderer.serverEntrypoint)?.id ?? renderer.serverEntrypoint;
const mod = (await viteServer.ssrLoadModule(id)) as { default: SSRLoadedRenderer['ssr'] };
Comment on lines +45 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much more straight-forward. I'm pleased if all works well!

return { ...renderer, ssr: mod.default };
}

Expand Down
2 changes: 0 additions & 2 deletions packages/integrations/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
"homepage": "https://astro.build",
"exports": {
".": "./dist/index.js",
"./client": "./client",
"./client.js": "./client.js",
"./server": "./server",
"./server.js": "./server.js",
"./package.json": "./package.json"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/integrations/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { AstroIntegration } from 'astro';
function getRenderer() {
return {
name: '@astrojs/preact',
clientEntrypoint: '@astrojs/preact/client',
serverEntrypoint: '@astrojs/preact/server',
clientEntrypoint: '@astrojs/preact/client.js',
serverEntrypoint: '@astrojs/preact/server.js',
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved
jsxImportSource: 'preact',
jsxTransformOptions: async () => {
const {
Expand All @@ -21,8 +21,8 @@ function getRenderer() {
function getViteConfiguration() {
return {
optimizeDeps: {
include: ['@astrojs/preact/client', 'preact', 'preact/jsx-runtime', 'preact-render-to-string'],
exclude: ['@astrojs/preact/server'],
include: ['@astrojs/preact/client.js', 'preact', 'preact/jsx-runtime', 'preact-render-to-string'],
exclude: ['@astrojs/preact/server.js'],
},
ssr: {
external: ['preact-render-to-string'],
Expand Down