Skip to content

Commit badc929

Browse files
fix(astro): include styles in custom 500 error pages (#14488)
Co-authored-by: Florian Lefebvre <[email protected]>
1 parent ae73aa4 commit badc929

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

.changeset/shaggy-chairs-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Fixes a case where styles on the custom 500 error page would not be included

packages/astro/src/core/build/page-data.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export function collectPagesData(opts: CollectPagesDataOptions): CollectPagesDat
2929
// and is then cached across all future SSR builds. In the past, we've had trouble
3030
// with parallelized builds without guaranteeing that this is called first.
3131
for (const route of manifest.routes) {
32-
// There's special handling in SSR
32+
// There's special handling in SSR for default components like 404
33+
// However, we need to include 500 pages so they can be properly built with their styles
3334
if (DEFAULT_COMPONENTS.some((component) => route.component === component)) {
3435
continue;
3536
}

packages/astro/src/core/build/plugins/plugin-ssr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ function vitePluginSSR(
9191
imports.push(`const ${variable} = () => import("${virtualModuleName}");`);
9292

9393
const pageData2 = internals.pagesByKeys.get(pageData.key);
94-
if (pageData2) {
95-
pageMap.push(`[${JSON.stringify(pageData2.component)}, ${variable}]`);
96-
}
94+
// Always add to pageMap even if pageData2 is missing from internals
95+
// This ensures error pages like 500.astro are included in the build
96+
pageMap.push(`[${JSON.stringify(pageData2?.component || pageData.component)}, ${variable}]`);
9797
i++;
9898
}
9999
}

packages/astro/test/custom-500.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,29 @@ describe('Custom 500', () => {
137137
const html = await response.text();
138138
assert.equal(html, '');
139139
});
140+
141+
it('renders custom 500 with styles', async () => {
142+
fixture = await loadFixture({
143+
root: './fixtures/custom-500/',
144+
output: 'server',
145+
adapter: testAdapter(),
146+
});
147+
await fixture.build();
148+
app = await fixture.loadTestAdapterApp();
149+
150+
const request = new Request('http://example.com/');
151+
const response = await app.render(request);
152+
assert.equal(response.status, 500);
153+
154+
const html = await response.text();
155+
const $ = cheerio.load(html);
156+
157+
// Check that styles from 500.astro are included
158+
// Note: Colors are minified (#ffcccc -> #fcc, #cc0000 -> #c00)
159+
const styles = $('style').text();
160+
assert.match(styles, /background-color.*#fcc/);
161+
assert.match(styles, /color.*#c00/);
162+
assert.match(styles, /font-family.*monospace/);
163+
});
140164
});
141165
});

packages/astro/test/fixtures/custom-500/src/pages/500.astro

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ const { error } = Astro.props
99
<html lang="en">
1010
<head>
1111
<title>Server error - Custom 500</title>
12+
<style>
13+
body {
14+
background-color: #ffcccc;
15+
color: #cc0000;
16+
}
17+
h1 {
18+
font-family: monospace;
19+
}
20+
</style>
1221
</head>
1322
<body>
1423
<h1>Server error</h1>

0 commit comments

Comments
 (0)