diff options
7 files changed, 43 insertions, 7 deletions
diff --git a/.changeset/great-spoons-try.md b/.changeset/great-spoons-try.md new file mode 100644 index 000000000..e7d0b9009 --- /dev/null +++ b/.changeset/great-spoons-try.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix: missing CSS import when 404 server Response redirects to a custom 404 page. diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index fbf4d93ea..37d6fb72e 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -145,7 +145,7 @@ export class App { routeData = this.match(request); } if (!routeData) { - return this.#renderError(request, { routeData, status: 404 }); + return this.#renderError(request, { status: 404 }); } Reflect.set(request, clientLocalsSymbol, locals ?? {}); @@ -173,13 +173,12 @@ export class App { ); } catch (err: any) { error(this.#logging, 'ssr', err.stack || err.message || String(err)); - return this.#renderError(request, { routeData, status: 500 }); + return this.#renderError(request, { status: 500 }); } if (isResponse(response, routeData.type)) { if (STATUS_CODES.has(response.status)) { return this.#renderError(request, { - routeData, response, status: response.status as 404 | 500, }); @@ -190,7 +189,6 @@ export class App { if (response.type === 'response') { if (response.response.headers.get('X-Astro-Response') === 'Not-Found') { return this.#renderError(request, { - routeData, response: response.response, status: 404, }); @@ -286,7 +284,7 @@ export class App { */ async #renderError( request: Request, - { routeData, status, response: originalResponse }: RenderErrorOptions + { status, response: originalResponse }: RenderErrorOptions ) { const errorRouteData = matchRoute('/' + status, this.#manifestData); const url = new URL(request.url); @@ -296,13 +294,12 @@ export class App { const response = await fetch(statusURL.toString()); return this.#mergeResponses(response, originalResponse); } - const finalRouteData = routeData ?? errorRouteData; const mod = await this.#getModuleForRoute(errorRouteData); try { const newRenderContext = await this.#createRenderContext( url, request, - finalRouteData, + errorRouteData, mod, status ); diff --git a/packages/astro/test/fixtures/ssr-api-route-custom-404/src/content/pages/index.md b/packages/astro/test/fixtures/ssr-api-route-custom-404/src/content/pages/index.md new file mode 100644 index 000000000..c98c9ad9e --- /dev/null +++ b/packages/astro/test/fixtures/ssr-api-route-custom-404/src/content/pages/index.md @@ -0,0 +1,7 @@ +--- +title: Astro +--- + +## Index + +Home page
\ No newline at end of file diff --git a/packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/404.astro b/packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/404.astro index 71a4a4d2c..c64eee468 100644 --- a/packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/404.astro +++ b/packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/404.astro @@ -1 +1,5 @@ +--- +import "../styles/main.css" +--- + <h1>Something went horribly wrong!</h1> diff --git a/packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/blog/[...ssrPath].astro b/packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/blog/[...ssrPath].astro new file mode 100644 index 000000000..aa40e4d8c --- /dev/null +++ b/packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/blog/[...ssrPath].astro @@ -0,0 +1,9 @@ +--- +import { getEntry } from 'astro:content'; +const { ssrPath } = Astro.params; +const page = await getEntry('pages', ssrPath === undefined ? 'index' : ssrPath); +if (!page) return new Response(null, { + status: 404, + statusText: 'Not found' +}); +--- diff --git a/packages/astro/test/fixtures/ssr-api-route-custom-404/src/styles/main.css b/packages/astro/test/fixtures/ssr-api-route-custom-404/src/styles/main.css new file mode 100644 index 000000000..4717ad4b9 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-api-route-custom-404/src/styles/main.css @@ -0,0 +1,3 @@ +h1 { + color: red; +}
\ No newline at end of file diff --git a/packages/astro/test/ssr-404-500-pages.test.js b/packages/astro/test/ssr-404-500-pages.test.js index 10e311ef9..ac747a16c 100644 --- a/packages/astro/test/ssr-404-500-pages.test.js +++ b/packages/astro/test/ssr-404-500-pages.test.js @@ -62,6 +62,17 @@ describe('404 and 500 pages', () => { expect($('h1').text()).to.equal('Something went horribly wrong!'); }); + it('404 page returned when a route does not match and imports are included', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/blog/fake/route'); + const routeData = app.match(request); + const response = await app.render(request, routeData); + expect(response.status).to.equal(404); + const html = await response.text(); + const $ = cheerio.load(html); + expect($('head link')).to.have.a.lengthOf(1); + }); + it('404 page returned when there is an 404 response returned from route', async () => { const app = await fixture.loadTestAdapterApp(); const request = new Request('http://example.com/causes-404'); |