Skip to content

Commit

Permalink
Return 404 status code for 404.astro in SSR (#4247)
Browse files Browse the repository at this point in the history
* Return 404 status code for 404.astro in SSR

* Adding a changeset
  • Loading branch information
matthewp authored Aug 10, 2022
1 parent 9d5ab55 commit 714a839
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-bottles-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Return 404 status code for 404.astro in SSR
5 changes: 5 additions & 0 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!;

if (routeData.type === 'page') {
Expand Down
13 changes: 7 additions & 6 deletions packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { RouteData } from '../../@types/astro';
import type { SerializedSSRManifest, SSRManifest } from './types';

import * as fs from 'fs';
import { IncomingMessage } from 'http';
import { deserializeManifest } from './common.js';
import { App } from './index.js';
import { App, MatchOptions } from './index.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');

Expand All @@ -24,10 +25,10 @@ function createRequestFromNodeRequest(req: IncomingMessage, body?: Uint8Array):
}

export class NodeApp extends App {
match(req: IncomingMessage | Request) {
return super.match(req instanceof Request ? req : createRequestFromNodeRequest(req));
match(req: IncomingMessage | Request, opts: MatchOptions = {}) {
return super.match(req instanceof Request ? req : createRequestFromNodeRequest(req), opts);
}
render(req: IncomingMessage | Request) {
render(req: IncomingMessage | Request, routeData?: RouteData) {
if ('on' in req) {
let body = Buffer.from([]);
let reqBodyComplete = new Promise((resolve, reject) => {
Expand All @@ -43,10 +44,10 @@ export class NodeApp extends App {
});

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);
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/ssr-404-500-pages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 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 () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/causes-error');
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export default function ({ provideAddress } = { provideAddress: true }) {
import { App } from 'astro/app';
class MyApp extends App {
render(request) {
render(request, routeData) {
${provideAddress ? `request[Symbol.for('astro.clientAddress')] = '0.0.0.0';` : ''}
return super.render(request);
return super.render(request, routeData);
}
}
Expand Down

0 comments on commit 714a839

Please sign in to comment.