mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
Render 404 page content when a Response
with status 404 is returned from a page (#7143)
This commit is contained in:
parent
d880293f79
commit
b41963b775
4 changed files with 30 additions and 9 deletions
5
.changeset/tough-pots-visit.md
Normal file
5
.changeset/tough-pots-visit.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Render 404 page content when a `Response` with status 404 is returned from a page
|
|
@ -144,19 +144,19 @@ export class App {
|
||||||
if (routeData.type === 'page') {
|
if (routeData.type === 'page') {
|
||||||
let response = await this.#renderPage(request, routeData, mod, defaultStatus);
|
let response = await this.#renderPage(request, routeData, mod, defaultStatus);
|
||||||
|
|
||||||
// If there was a 500 error, try sending the 500 page.
|
// If there was a known error code, try sending the according page (e.g. 404.astro / 500.astro).
|
||||||
if (response.status === 500) {
|
if (response.status === 500 || response.status === 404) {
|
||||||
const fiveHundredRouteData = matchRoute('/500', this.#manifestData);
|
const errorPageData = matchRoute('/' + response.status, this.#manifestData);
|
||||||
if (fiveHundredRouteData) {
|
if (errorPageData && errorPageData.route !== routeData.route) {
|
||||||
mod = await this.#manifest.pageMap.get(fiveHundredRouteData.component)!();
|
mod = await this.#manifest.pageMap.get(errorPageData.component)!();
|
||||||
try {
|
try {
|
||||||
let fiveHundredResponse = await this.#renderPage(
|
let errorResponse = await this.#renderPage(
|
||||||
request,
|
request,
|
||||||
fiveHundredRouteData,
|
errorPageData,
|
||||||
mod,
|
mod,
|
||||||
500
|
response.status
|
||||||
);
|
);
|
||||||
return fiveHundredResponse;
|
return errorResponse;
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/causes-404.astro
vendored
Normal file
6
packages/astro/test/fixtures/ssr-api-route-custom-404/src/pages/causes-404.astro
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
return new Response(null, {
|
||||||
|
status: 404,
|
||||||
|
statusText: 'Not found',
|
||||||
|
});
|
||||||
|
---
|
|
@ -62,6 +62,16 @@ describe('404 and 500 pages', () => {
|
||||||
expect($('h1').text()).to.equal('Something went horribly wrong!');
|
expect($('h1').text()).to.equal('Something went horribly wrong!');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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');
|
||||||
|
const response = await app.render(request);
|
||||||
|
expect(response.status).to.equal(404);
|
||||||
|
const html = await response.text();
|
||||||
|
const $ = cheerio.load(html);
|
||||||
|
expect($('h1').text()).to.equal('Something went horribly wrong!');
|
||||||
|
});
|
||||||
|
|
||||||
it('500 page returned when there is an error', async () => {
|
it('500 page returned when there is an error', async () => {
|
||||||
const app = await fixture.loadTestAdapterApp();
|
const app = await fixture.loadTestAdapterApp();
|
||||||
const request = new Request('http://example.com/causes-error');
|
const request = new Request('http://example.com/causes-error');
|
||||||
|
|
Loading…
Reference in a new issue