From 827e61f38edbfed496c3b99d5c32f0a08566e973 Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 15 Mar 2023 22:39:03 +0800 Subject: [PATCH] Fix prerendered 404 page handling in SSR --- .changeset/yellow-grapes-design.md | 5 +++ packages/astro/src/core/app/index.ts | 4 ++- .../fixtures/ssr-prerender-404/package.json | 8 +++++ .../ssr-prerender-404/src/pages/404.astro | 5 +++ .../ssr-prerender-404/src/pages/static.astro | 18 ++++++++++ packages/astro/test/ssr-prerender-404.test.js | 33 +++++++++++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 .changeset/yellow-grapes-design.md create mode 100644 packages/astro/test/fixtures/ssr-prerender-404/package.json create mode 100644 packages/astro/test/fixtures/ssr-prerender-404/src/pages/404.astro create mode 100644 packages/astro/test/fixtures/ssr-prerender-404/src/pages/static.astro create mode 100644 packages/astro/test/ssr-prerender-404.test.js diff --git a/.changeset/yellow-grapes-design.md b/.changeset/yellow-grapes-design.md new file mode 100644 index 000000000000..ba3594ce2cbd --- /dev/null +++ b/.changeset/yellow-grapes-design.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix prerendered 404 page handling in SSR diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index c97131b43b73..286314d4f200 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -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; } diff --git a/packages/astro/test/fixtures/ssr-prerender-404/package.json b/packages/astro/test/fixtures/ssr-prerender-404/package.json new file mode 100644 index 000000000000..fb2290464f26 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-prerender-404/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-prerender-404", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-prerender-404/src/pages/404.astro b/packages/astro/test/fixtures/ssr-prerender-404/src/pages/404.astro new file mode 100644 index 000000000000..749c7095496b --- /dev/null +++ b/packages/astro/test/fixtures/ssr-prerender-404/src/pages/404.astro @@ -0,0 +1,5 @@ +--- +export const prerender = true +--- + +Page does not exist diff --git a/packages/astro/test/fixtures/ssr-prerender-404/src/pages/static.astro b/packages/astro/test/fixtures/ssr-prerender-404/src/pages/static.astro new file mode 100644 index 000000000000..54680cfcb922 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-prerender-404/src/pages/static.astro @@ -0,0 +1,18 @@ +--- +export const prerender = true; + +const { searchParams } = Astro.url; +--- + + + + Static Page + + + +

Hello world!

+
{searchParams.get('q')}
+ + diff --git a/packages/astro/test/ssr-prerender-404.test.js b/packages/astro/test/ssr-prerender-404.test.js new file mode 100644 index 000000000000..8ebcb01c8355 --- /dev/null +++ b/packages/astro/test/ssr-prerender-404.test.js @@ -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' + ); + }); + }); +});