Skip to content

Commit

Permalink
Fix prerendered 404 page handling in SSR (#6558)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Mar 16, 2023
1 parent 392ba3e commit 6c465e9
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-grapes-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix prerendered 404 page handling in SSR
4 changes: 3 additions & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export class App {
if (routeData.prerender) return undefined;
return routeData;
} else if (matchNotFound) {
return matchRoute('/404', this.#manifestData);
const notFoundRouteData = matchRoute('/404', this.#manifestData);
if (notFoundRouteData?.prerender) return undefined;
return notFoundRouteData;
} else {
return undefined;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-prerender-404/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-prerender-404",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
export const prerender = true
---

Page does not exist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
export const prerender = true;
const { searchParams } = Astro.url;
---

<html>
<head>
<title>Static Page</title>
<script>
console.log('hello world');
</script>
</head>
<body>
<h1 id="greeting">Hello world!</h1>
<div id="searchparams">{searchParams.get('q')}</div>
</body>
</html>
33 changes: 33 additions & 0 deletions packages/astro/test/ssr-prerender-404.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

describe('SSR: prerender 404', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-prerender-404/',
output: 'server',
adapter: testAdapter(),
experimental: {
prerender: true,
},
});
await fixture.build();
});

describe('Prerendering', () => {
it('Prerendered 404.astro page is not rendered', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/non-existent-page');
const response = await app.render(request);
expect(response.status).to.equal(404);
expect(response.statusText).to.equal(
'Not found',
'should be actual 404 response, not 404 page'
);
});
});
});
21 changes: 14 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6c465e9

Please sign in to comment.