Return 404 status code for 404.astro in SSR (#4247)
* Return 404 status code for 404.astro in SSR * Adding a changeset
This commit is contained in:
parent
9d5ab55086
commit
714a8399e2
5 changed files with 30 additions and 8 deletions
5
.changeset/empty-bottles-allow.md
Normal file
5
.changeset/empty-bottles-allow.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Return 404 status code for 404.astro in SSR
|
|
@ -82,6 +82,11 @@ export class App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use the 404 status code for 404.astro components
|
||||||
|
if(routeData.route === '/404') {
|
||||||
|
defaultStatus = 404;
|
||||||
|
}
|
||||||
|
|
||||||
let mod = this.#manifest.pageMap.get(routeData.component)!;
|
let mod = this.#manifest.pageMap.get(routeData.component)!;
|
||||||
|
|
||||||
if (routeData.type === 'page') {
|
if (routeData.type === 'page') {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
|
import type { RouteData } from '../../@types/astro';
|
||||||
import type { SerializedSSRManifest, SSRManifest } from './types';
|
import type { SerializedSSRManifest, SSRManifest } from './types';
|
||||||
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import { IncomingMessage } from 'http';
|
import { IncomingMessage } from 'http';
|
||||||
import { deserializeManifest } from './common.js';
|
import { deserializeManifest } from './common.js';
|
||||||
import { App } from './index.js';
|
import { App, MatchOptions } from './index.js';
|
||||||
|
|
||||||
const clientAddressSymbol = Symbol.for('astro.clientAddress');
|
const clientAddressSymbol = Symbol.for('astro.clientAddress');
|
||||||
|
|
||||||
|
@ -24,10 +25,10 @@ function createRequestFromNodeRequest(req: IncomingMessage, body?: Uint8Array):
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NodeApp extends App {
|
export class NodeApp extends App {
|
||||||
match(req: IncomingMessage | Request) {
|
match(req: IncomingMessage | Request, opts: MatchOptions = {}) {
|
||||||
return super.match(req instanceof Request ? req : createRequestFromNodeRequest(req));
|
return super.match(req instanceof Request ? req : createRequestFromNodeRequest(req), opts);
|
||||||
}
|
}
|
||||||
render(req: IncomingMessage | Request) {
|
render(req: IncomingMessage | Request, routeData?: RouteData) {
|
||||||
if ('on' in req) {
|
if ('on' in req) {
|
||||||
let body = Buffer.from([]);
|
let body = Buffer.from([]);
|
||||||
let reqBodyComplete = new Promise((resolve, reject) => {
|
let reqBodyComplete = new Promise((resolve, reject) => {
|
||||||
|
@ -43,10 +44,10 @@ export class NodeApp extends App {
|
||||||
});
|
});
|
||||||
|
|
||||||
return reqBodyComplete.then(() => {
|
return reqBodyComplete.then(() => {
|
||||||
return super.render(req instanceof Request ? req : createRequestFromNodeRequest(req, body));
|
return super.render(req instanceof Request ? req : createRequestFromNodeRequest(req, body), routeData);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return super.render(req instanceof Request ? req : createRequestFromNodeRequest(req));
|
return super.render(req instanceof Request ? req : createRequestFromNodeRequest(req), routeData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,17 @@ 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 a route does not match and passing routeData', async () => {
|
||||||
|
const app = await fixture.loadTestAdapterApp();
|
||||||
|
const request = new Request('http://example.com/some/fake/route');
|
||||||
|
const routeData = app.match(request, { matchNotFound: true });
|
||||||
|
const response = await app.render(request, routeData);
|
||||||
|
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');
|
||||||
|
|
|
@ -27,9 +27,9 @@ export default function ({ provideAddress } = { provideAddress: true }) {
|
||||||
import { App } from 'astro/app';
|
import { App } from 'astro/app';
|
||||||
|
|
||||||
class MyApp extends App {
|
class MyApp extends App {
|
||||||
render(request) {
|
render(request, routeData) {
|
||||||
${provideAddress ? `request[Symbol.for('astro.clientAddress')] = '0.0.0.0';` : ''}
|
${provideAddress ? `request[Symbol.for('astro.clientAddress')] = '0.0.0.0';` : ''}
|
||||||
return super.render(request);
|
return super.render(request, routeData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue