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

Prevent externalized adapters from breaking build #11709

Merged
merged 4 commits into from
Aug 14, 2024
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
5 changes: 5 additions & 0 deletions .changeset/new-boats-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix adapter causing Netlify to break
44 changes: 37 additions & 7 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand Down
11 changes: 4 additions & 7 deletions packages/astro/test/ssr-split-manifest.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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('<title>Pre render me</title>'), true);
});

Expand Down
Loading