diff --git a/.changeset/deep-spies-end.md b/.changeset/deep-spies-end.md new file mode 100644 index 000000000000..01447813eeca --- /dev/null +++ b/.changeset/deep-spies-end.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Populates styles in the SSR manifest for prerendered routes. Previously, prerendered routes had `styles: []` in the manifest, making it impossible for workers or middleware to discover which CSS files a prerendered page uses. diff --git a/.github/workflows/preview-release.yml b/.github/workflows/preview-release.yml index 873b1e222335..8d091ab563e5 100644 --- a/.github/workflows/preview-release.yml +++ b/.github/workflows/preview-release.yml @@ -2,7 +2,7 @@ name: Preview release on: pull_request: - branches: [main] + branches: [main, 5-legacy] types: [labeled] concurrency: diff --git a/packages/astro/src/core/build/plugins/plugin-manifest.ts b/packages/astro/src/core/build/plugins/plugin-manifest.ts index 0612522fb9be..e036c3406579 100644 --- a/packages/astro/src/core/build/plugins/plugin-manifest.ts +++ b/packages/astro/src/core/build/plugins/plugin-manifest.ts @@ -242,11 +242,21 @@ async function buildManifest( const outFolder = getOutFolder(opts.settings, route.pathname, route); const outFile = getOutFile(opts.settings.config, outFolder, route.pathname, route); const file = outFile.toString().replace(opts.settings.config.build.client.toString(), ''); + + const pageData = internals.pagesByKeys.get(makePageDataKey(route.route, route.component)); + const styles = pageData + ? pageData.styles + .sort(cssOrder) + .map(({ sheet }) => sheet) + .map((s) => (s.type === 'external' ? { ...s, src: prefixAssetPath(s.src) } : s)) + .reduce(mergeInlineCss, []) + : []; + routes.push({ file, links: [], scripts: [], - styles: [], + styles, routeData: serializeRouteData(route, settings.config.trailingSlash), }); staticFiles.push(file); diff --git a/packages/astro/test/fixtures/ssr-manifest/src/pages/prerendered.astro b/packages/astro/test/fixtures/ssr-manifest/src/pages/prerendered.astro new file mode 100644 index 000000000000..cb67f09c3177 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-manifest/src/pages/prerendered.astro @@ -0,0 +1,9 @@ +--- +export const prerender = true; +import '../styles/global.css'; +--- + + +Prerendered +

Hello

+ diff --git a/packages/astro/test/fixtures/ssr-manifest/src/styles/global.css b/packages/astro/test/fixtures/ssr-manifest/src/styles/global.css new file mode 100644 index 000000000000..991912d894b3 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-manifest/src/styles/global.css @@ -0,0 +1 @@ +body { color: red; } diff --git a/packages/astro/test/ssr-manifest.test.js b/packages/astro/test/ssr-manifest.test.js index 254ea304cd12..7227763a2f3e 100644 --- a/packages/astro/test/ssr-manifest.test.js +++ b/packages/astro/test/ssr-manifest.test.js @@ -28,6 +28,18 @@ describe('astro:ssr-manifest', () => { assert.equal(app.manifest.compressHTML, true); }); + it('includes styles for prerendered routes in manifest', async () => { + const app = await fixture.loadTestAdapterApp(); + const prerenderedRoute = app.manifest.routes.find( + (route) => route.routeData.route === '/prerendered', + ); + assert.ok(prerenderedRoute, 'prerendered route should be in manifest'); + assert.ok( + prerenderedRoute.styles.length > 0, + 'prerendered route should have styles in manifest', + ); + }); + it('includes correct routes', async () => { const app = await fixture.loadTestAdapterApp(); // NOTE: `app.manifest` is actually a private property