Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prerendered 404 page handling in SSR #6558

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.