Skip to content

Commit 10273e0

Browse files
fix: 404 status in ssr (#14884)
1 parent 6751a2e commit 10273e0

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

.changeset/stale-webs-tie.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 setting the status of a page to `404` in ssr would show an empty page (or `404.astro` page if provided) instead of using the current page

packages/astro/src/core/app/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,9 @@ export class App {
559559

560560
if (
561561
REROUTABLE_STATUS_CODES.includes(response.status) &&
562+
// If the body isn't null, that means the user sets the 404 status
563+
// but uses the current route to handle the 404
564+
response.body === null &&
562565
response.headers.get(REROUTE_DIRECTIVE_HEADER) !== 'no'
563566
) {
564567
return this.#renderError(request, {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Custom 404</title>
4+
</head>
5+
<body>
6+
<h1>Custom 404</h1>
7+
</body>
8+
</html>

packages/astro/test/ssr-response.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ describe('Using Astro.response in SSR', () => {
3838
assert.equal(headers.get('one-two'), 'three');
3939
});
4040

41+
it('Returns the page, not the custom 404.astro', async () => {
42+
const app = await fixture.loadTestAdapterApp();
43+
const request = new Request('http://example.com/status-code');
44+
const response = await app.render(request);
45+
const html = await response.text()
46+
assert.equal(html.includes('<h1>Testing</h1>'), true);
47+
assert.equal(html.includes('<h1>Custom 404</h1>'), false);
48+
});
49+
4150
it('Can add headers', async () => {
4251
const app = await fixture.loadTestAdapterApp();
4352
const request = new Request('http://example.com/some-header');

0 commit comments

Comments
 (0)