From 898845402cd82995bd4878c93d3ccfcce89ebf27 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 22 Jun 2022 11:35:54 -0400 Subject: [PATCH] Include all client build artifacts in SSRManifest (#3678) * Include all client build artifacts in SSRManifest * Adds a changeset --- .changeset/unlucky-otters-agree.md | 5 ++++ .../astro/src/core/build/vite-plugin-ssr.ts | 16 ++++++----- .../fixtures/ssr-scripts/astro.config.mjs | 7 +++++ .../test/fixtures/ssr-scripts/package.json | 9 +++++++ .../ssr-scripts/src/components/Hello.jsx | 6 +++++ .../ssr-scripts/src/pages/index.astro | 11 ++++++++ packages/astro/test/ssr-scripts.test.js | 27 +++++++++++++++++++ pnpm-lock.yaml | 8 ++++++ 8 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 .changeset/unlucky-otters-agree.md create mode 100644 packages/astro/test/fixtures/ssr-scripts/astro.config.mjs create mode 100644 packages/astro/test/fixtures/ssr-scripts/package.json create mode 100644 packages/astro/test/fixtures/ssr-scripts/src/components/Hello.jsx create mode 100644 packages/astro/test/fixtures/ssr-scripts/src/pages/index.astro create mode 100644 packages/astro/test/ssr-scripts.test.js diff --git a/.changeset/unlucky-otters-agree.md b/.changeset/unlucky-otters-agree.md new file mode 100644 index 000000000..2da789e51 --- /dev/null +++ b/.changeset/unlucky-otters-agree.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix regression with SSRManifest and client assets diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index e543e925e..ce386872b 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -70,12 +70,6 @@ if(_start in adapter) { return void 0; }, async generateBundle(_opts, bundle) { - internals.staticFiles = new Set( - await glob('**/*', { - cwd: fileURLToPath(buildOpts.buildConfig.client), - }) - ); - // Add assets from this SSR chunk as well. for (const [_chunkName, chunk] of Object.entries(bundle)) { if (chunk.type === 'asset') { @@ -101,6 +95,16 @@ export async function injectManifest(buildOpts: StaticBuildOptions, internals: B throw new Error(`Did not generate an entry chunk for SSR`); } + // Add assets from the client build. + const clientStatics = new Set( + await glob('**/*', { + cwd: fileURLToPath(buildOpts.buildConfig.client), + }) + ); + for(const file of clientStatics) { + internals.staticFiles.add(file); + } + const staticFiles = internals.staticFiles; const manifest = buildManifest(buildOpts, internals, Array.from(staticFiles)); await runHookBuildSsr({ config: buildOpts.astroConfig, manifest }); diff --git a/packages/astro/test/fixtures/ssr-scripts/astro.config.mjs b/packages/astro/test/fixtures/ssr-scripts/astro.config.mjs new file mode 100644 index 000000000..08916b1fe --- /dev/null +++ b/packages/astro/test/fixtures/ssr-scripts/astro.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from 'astro/config'; +import preact from '@astrojs/preact'; + +// https://astro.build/config +export default defineConfig({ + integrations: [preact()], +}); diff --git a/packages/astro/test/fixtures/ssr-scripts/package.json b/packages/astro/test/fixtures/ssr-scripts/package.json new file mode 100644 index 000000000..2050dfbec --- /dev/null +++ b/packages/astro/test/fixtures/ssr-scripts/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/ssr-scripts", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/preact": "workspace:", + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-scripts/src/components/Hello.jsx b/packages/astro/test/fixtures/ssr-scripts/src/components/Hello.jsx new file mode 100644 index 000000000..70279a587 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-scripts/src/components/Hello.jsx @@ -0,0 +1,6 @@ + +export default function() { + return ( +
Hello world
+ ) +} diff --git a/packages/astro/test/fixtures/ssr-scripts/src/pages/index.astro b/packages/astro/test/fixtures/ssr-scripts/src/pages/index.astro new file mode 100644 index 000000000..8b27cdd3b --- /dev/null +++ b/packages/astro/test/fixtures/ssr-scripts/src/pages/index.astro @@ -0,0 +1,11 @@ +--- +import Hello from '../components/Hello.jsx'; +--- + + + Astro + + + + + diff --git a/packages/astro/test/ssr-scripts.test.js b/packages/astro/test/ssr-scripts.test.js new file mode 100644 index 000000000..f44099e41 --- /dev/null +++ b/packages/astro/test/ssr-scripts.test.js @@ -0,0 +1,27 @@ +import { expect } from 'chai'; +import { loadFixture } from './test-utils.js'; +import testAdapter from './test-adapter.js'; + +describe('SSR Hydrated component scripts', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/ssr-scripts/', + experimental: { + ssr: true, + }, + adapter: testAdapter(), + }); + await fixture.build(); + }); + + it('Are included in the manifest.assets so that an adapter can know to serve static', async () => { + const app = await fixture.loadTestAdapterApp(); + + /** @type {Set} */ + const assets = app.manifest.assets; + expect(assets.size).to.be.greaterThan(0); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 72634a6a1..254f0bcb6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1588,6 +1588,14 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/ssr-scripts: + specifiers: + '@astrojs/preact': 'workspace:' + astro: workspace:* + dependencies: + '@astrojs/preact': link:../../../../integrations/preact + astro: link:../../.. + packages/astro/test/fixtures/static-build: specifiers: '@astrojs/preact': workspace:*