diff --git a/.changeset/new-boats-flash.md b/.changeset/new-boats-flash.md new file mode 100644 index 000000000000..4ef58bba4538 --- /dev/null +++ b/.changeset/new-boats-flash.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix adapter causing Netlify to break diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index 70997e40ea67..b84d5050c492 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -19,6 +19,26 @@ import { getComponentFromVirtualModulePageName, getVirtualModulePageName } from export const SSR_VIRTUAL_MODULE_ID = '@astrojs-ssr-virtual-entry'; export const RESOLVED_SSR_VIRTUAL_MODULE_ID = '\0' + SSR_VIRTUAL_MODULE_ID; +const ADAPTER_VIRTUAL_MODULE_ID = '@astrojs-ssr-adapter'; +const RESOLVED_ADAPTER_VIRTUAL_MODULE_ID = '\0' + ADAPTER_VIRTUAL_MODULE_ID; + +function vitePluginAdapter(adapter: AstroAdapter): VitePlugin { + return { + name: '@astrojs/vite-plugin-astro-adapter', + enforce: 'post', + resolveId(id) { + if (id === ADAPTER_VIRTUAL_MODULE_ID) { + return RESOLVED_ADAPTER_VIRTUAL_MODULE_ID; + } + }, + async load(id) { + if(id === RESOLVED_ADAPTER_VIRTUAL_MODULE_ID) { + return `export * from '${adapter.serverEntrypoint}';`; + } + }, + }; +} + function vitePluginSSR( internals: BuildInternals, adapter: AstroAdapter, @@ -39,7 +59,7 @@ function vitePluginSSR( const adapterServerEntrypoint = options.settings.adapter?.serverEntrypoint; if (adapterServerEntrypoint) { - inputs.add(adapterServerEntrypoint); + inputs.add(ADAPTER_VIRTUAL_MODULE_ID); } inputs.add(SSR_VIRTUAL_MODULE_ID); @@ -119,14 +139,19 @@ export function pluginSSR( targets: ['server'], hooks: { 'build:before': () => { - let vitePlugin = + const adapter = options.settings.adapter!; + let ssrPlugin = ssr && functionPerRouteEnabled === false - ? vitePluginSSR(internals, options.settings.adapter!, options) + ? vitePluginSSR(internals, adapter, options) : undefined; + const vitePlugin = [vitePluginAdapter(adapter)]; + if(ssrPlugin) { + vitePlugin.unshift(ssrPlugin); + } return { enforce: 'after-user-plugins', - vitePlugin, + vitePlugin: vitePlugin, }; }, 'build:post': async () => { @@ -231,10 +256,15 @@ export function pluginSSRSplit( targets: ['server'], hooks: { 'build:before': () => { - let vitePlugin = + const adapter = options.settings.adapter!; + let ssrPlugin = ssr && functionPerRouteEnabled - ? vitePluginSSRSplit(internals, options.settings.adapter!, options) + ? vitePluginSSRSplit(internals, adapter, options) : undefined; + const vitePlugin = [vitePluginAdapter(adapter)]; + if(ssrPlugin) { + vitePlugin.unshift(ssrPlugin); + } return { enforce: 'after-user-plugins', @@ -251,7 +281,7 @@ function generateSSRCode(settings: AstroSettings, adapter: AstroAdapter, middlew const imports = [ `import { renderers } from '${RENDERERS_MODULE_ID}';`, - `import * as serverEntrypointModule from '${adapter.serverEntrypoint}';`, + `import * as serverEntrypointModule from '${ADAPTER_VIRTUAL_MODULE_ID}';`, `import { manifest as defaultManifest } from '${SSR_MANIFEST_VIRTUAL_MODULE_ID}';`, edgeMiddleware ? `` : `import { onRequest as middleware } from '${middlewareId}';`, settings.config.experimental.serverIslands diff --git a/packages/astro/test/ssr-split-manifest.test.js b/packages/astro/test/ssr-split-manifest.test.js index 06a6fe8c6a5c..fc55ce1ba821 100644 --- a/packages/astro/test/ssr-split-manifest.test.js +++ b/packages/astro/test/ssr-split-manifest.test.js @@ -1,6 +1,5 @@ import assert from 'node:assert/strict'; import { existsSync, readFileSync } from 'node:fs'; -import { resolve } from 'node:path'; import { before, describe, it } from 'node:test'; import { fileURLToPath } from 'node:url'; import * as cheerio from 'cheerio'; @@ -62,12 +61,10 @@ describe('astro:ssr-manifest, split', () => { }); it('should correctly emit the the pre render page', async () => { - const text = readFileSync( - resolve('./test/fixtures/ssr-split-manifest/dist/client/prerender/index.html'), - { - encoding: 'utf8', - }, - ); + const indexUrl = new URL('./fixtures/ssr-split-manifest/dist/client/prerender/index.html', import.meta.url); + const text = readFileSync(indexUrl, { + encoding: 'utf8', + }); assert.equal(text.includes('