0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

fix(dev): dont reroute endpoint responses (#9830)

* fix(dev): dont reroute endpoint responses

* factor out header name as a const

* add test case

* add changeset
This commit is contained in:
Arsh 2024-01-25 18:06:30 +00:00 committed by GitHub
parent e5276f097b
commit f3d22136e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 28 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes an issue where 404 responses from endpoints were replaced with contents of 404.astro in dev mode.

View file

@ -6,6 +6,7 @@ import type {
SSRManifest, SSRManifest,
} from '../../@types/astro.js'; } from '../../@types/astro.js';
import { createI18nMiddleware, i18nPipelineHook } from '../../i18n/middleware.js'; import { createI18nMiddleware, i18nPipelineHook } from '../../i18n/middleware.js';
import { REROUTE_DIRECTIVE_HEADER } from '../../runtime/server/consts.js';
import type { SinglePageBuiltModule } from '../build/types.js'; import type { SinglePageBuiltModule } from '../build/types.js';
import { getSetCookiesFromResponse } from '../cookies/index.js'; import { getSetCookiesFromResponse } from '../cookies/index.js';
import { consoleLogDestination } from '../logger/console.js'; import { consoleLogDestination } from '../logger/console.js';
@ -278,7 +279,7 @@ export class App {
if ( if (
REROUTABLE_STATUS_CODES.has(response.status) && REROUTABLE_STATUS_CODES.has(response.status) &&
response.headers.get('X-Astro-Reroute') !== 'no' response.headers.get(REROUTE_DIRECTIVE_HEADER) !== 'no'
) { ) {
return this.#renderError(request, { return this.#renderError(request, {
response, response,
@ -286,8 +287,8 @@ export class App {
}); });
} }
if (response.headers.has('X-Astro-Reroute')) { if (response.headers.has(REROUTE_DIRECTIVE_HEADER)) {
response.headers.delete('X-Astro-Reroute'); response.headers.delete(REROUTE_DIRECTIVE_HEADER);
} }
if (addCookieHeader) { if (addCookieHeader) {

View file

@ -0,0 +1 @@
export const REROUTE_DIRECTIVE_HEADER = 'X-Astro-Reroute';

View file

@ -1,4 +1,5 @@
import { bold } from 'kleur/colors'; import { bold } from 'kleur/colors';
import { REROUTE_DIRECTIVE_HEADER } from './consts.js';
import type { APIContext, EndpointHandler } from '../../@types/astro.js'; import type { APIContext, EndpointHandler } from '../../@types/astro.js';
import type { Logger } from '../../core/logger/core.js'; import type { Logger } from '../../core/logger/core.js';
@ -41,6 +42,6 @@ export async function renderEndpoint(
const response = await handler.call(mod, context); const response = await handler.call(mod, context);
// Endpoints explicitly returning 404 or 500 response status should // Endpoints explicitly returning 404 or 500 response status should
// NOT be subject to rerouting to 404.astro or 500.astro. // NOT be subject to rerouting to 404.astro or 500.astro.
response.headers.set('X-Astro-Reroute', 'no'); response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
return response; return response;
} }

View file

@ -35,6 +35,7 @@ import { preload } from './index.js';
import { getComponentMetadata } from './metadata.js'; import { getComponentMetadata } from './metadata.js';
import { handle404Response, writeSSRResult, writeWebResponse } from './response.js'; import { handle404Response, writeSSRResult, writeWebResponse } from './response.js';
import { getScriptsForURL } from './scripts.js'; import { getScriptsForURL } from './scripts.js';
import { REROUTE_DIRECTIVE_HEADER } from '../runtime/server/consts.js';
const clientLocalsSymbol = Symbol.for('astro.locals'); const clientLocalsSymbol = Symbol.for('astro.locals');
@ -342,7 +343,7 @@ export async function handleRoute({
}) })
); );
} }
if (response.status === 404 && has404Route(manifestData)) { if (response.status === 404 && has404Route(manifestData) && response.headers.get(REROUTE_DIRECTIVE_HEADER) !== "no") {
const fourOhFourRoute = await matchRoute('/404', manifestData, pipeline); const fourOhFourRoute = await matchRoute('/404', manifestData, pipeline);
if (options && fourOhFourRoute?.route !== options.route) if (options && fourOhFourRoute?.route !== options.route)
return handleRoute({ return handleRoute({

View file

@ -246,6 +246,14 @@ describe('Development Routing', () => {
expect(body.title).to.equal('data [slug]'); expect(body.title).to.equal('data [slug]');
}); });
it('error responses are served untouched', async () => {
const response = await fixture.fetch('/not-ok');
expect(response.status).to.equal(404);
expect(response.headers.get("Content-Type")).to.equal("text/plain;charset=UTF-8");
const body = await response.text();
expect(body).to.equal("Text from pages/not-ok.ts");
})
it('correct MIME type when loading /home.json (static route)', async () => { it('correct MIME type when loading /home.json (static route)', async () => {
const response = await fixture.fetch('/home.json'); const response = await fixture.fetch('/home.json');
expect(response.headers.get('content-type')).to.match(/application\/json/); expect(response.headers.get('content-type')).to.match(/application\/json/);

View file

@ -0,0 +1 @@
<h1>Text from pages/404.astro</h1>

View file

@ -0,0 +1,5 @@
export async function GET() {
return new Response("Text from pages/not-ok.ts", {
status: 404,
});
}