Skip to content

Commit

Permalink
Fix missing CSS when 404 server Response redirects to a custom 404 pa…
Browse files Browse the repository at this point in the history
…ge (#7946)

* Fix missing css import on 404 redirect

* Chore: changeset

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
andremralves and natemoo-re committed Aug 7, 2023
1 parent f6845d9 commit 9d00700
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .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.
11 changes: 4 additions & 7 deletions packages/astro/src/core/app/index.ts
Expand Up @@ -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 ?? {});
Expand Down Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand Down Expand Up @@ -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);
Expand All @@ -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
);
Expand Down
@@ -0,0 +1,7 @@
---
title: Astro
---

## Index

Home page
@@ -1 +1,5 @@
---
import "../styles/main.css"
---

<h1>Something went horribly wrong!</h1>
@@ -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'
});
---
@@ -0,0 +1,3 @@
h1 {
color: red;
}
11 changes: 11 additions & 0 deletions packages/astro/test/ssr-404-500-pages.test.js
Expand Up @@ -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');
Expand Down

0 comments on commit 9d00700

Please sign in to comment.