From 6c465e958e088ff55e5b895e67c64c0dfd4277a6 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 16 Mar 2023 23:03:17 +0800 Subject: [PATCH] Fix prerendered 404 page handling in SSR (#6558) --- .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 +++++++++++++++++++ pnpm-lock.yaml | 21 ++++++++---- 7 files changed, 86 insertions(+), 8 deletions(-) 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' + ); + }); + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5c460b4b7544..380705f8142c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2540,6 +2540,12 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/ssr-prerender-404: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/ssr-prerender-get-static-paths: specifiers: astro: workspace:* @@ -4190,14 +4196,15 @@ packages: - react dev: false - /@astrojs/markdown-remark/2.0.1_astro@packages+astro: - resolution: {integrity: sha512-xQF1rXGJN18m+zZucwRRtmNehuhPMMhZhi6HWKrtpEAKnHSPk8lqf1GXgKH7/Sypglu8ivdECZ+EGs6kOYVasQ==} + /@astrojs/markdown-remark/2.1.0_astro@packages+astro: + resolution: {integrity: sha512-w9T5o3UWQIfMcCkM2nLWrlfVQazh/7mw+2N/85QGcSUkZy6oNJoyy8Xz/ZkDhHLx8HPO0RT9fABR0B/H+aDaEw==} peerDependencies: astro: '*' dependencies: - '@astrojs/prism': 2.0.0 + '@astrojs/prism': 2.1.1 astro: link:packages/astro github-slugger: 1.5.0 + image-size: 1.0.2 import-meta-resolve: 2.2.1 rehype-raw: 6.1.1 rehype-stringify: 9.0.3 @@ -4217,8 +4224,8 @@ packages: resolution: {integrity: sha512-mol57cw1jJMcQgKMRGn7p6cewajq6JTNtqj5aAZgROWam/phVDSOCbXj/WU3O9+3qFnyKtpczoufQKwJTQltAw==} engines: {node: '>=16.12.0'} dependencies: - '@astrojs/markdown-remark': 2.0.1_astro@packages+astro - '@astrojs/prism': 2.0.0 + '@astrojs/markdown-remark': 2.1.0_astro@packages+astro + '@astrojs/prism': 2.1.1 '@mdx-js/mdx': 2.3.0 '@mdx-js/rollup': 2.3.0 acorn: 8.8.2 @@ -4262,8 +4269,8 @@ packages: - supports-color dev: false - /@astrojs/prism/2.0.0: - resolution: {integrity: sha512-YgeoeEPqsxaEpg0rwe/bUq3653LqSQnMjrLlpYwrbQQMQQqz6Y5yXN+RX3SfLJ6ppNb4+Fu2+Z49EXjk48Ihjw==} + /@astrojs/prism/2.1.1: + resolution: {integrity: sha512-Gnwnlb1lGJzCQEg89r4/WqgfCGPNFC7Kuh2D/k289Cbdi/2PD7Lrdstz86y1itDvcb2ijiRqjqWnJ5rsfu/QOA==} engines: {node: '>=16.12.0'} dependencies: prismjs: 1.29.0