Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix endpoint response with immutable header #9876

Merged
merged 11 commits into from
Jan 30, 2024
5 changes: 5 additions & 0 deletions .changeset/silver-peaches-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Updated `renderEndpoint` implementation to only append the reroute directive if the response HTTP status code is 404 or 500.
friedemannsommer marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 7 additions & 1 deletion packages/astro/src/runtime/server/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export async function renderEndpoint(
const response = await handler.call(mod, context);
// Endpoints explicitly returning 404 or 500 response status should
// NOT be subject to rerouting to 404.astro or 500.astro.
response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
if (response.status === 404 || response.status === 500) {
// Only `Response.redirect` headers are immutable, therefore a `try..catch` is not necessary.
// Note: `Response.redirect` can only constructed with HTTP status codes: 301, 302, 303, 307, 308.
// Source: https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect_static#parameters
response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
}

return response;
}
61 changes: 61 additions & 0 deletions packages/astro/test/units/routing/endpoints.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
createBasicSettings,
createFs,
createRequestAndResponse,
defaultLogger,
} from '../test-utils.js';
import { fileURLToPath } from 'node:url';
import { expect } from 'chai';
import { createContainer } from '../../../dist/core/dev/container.js';
import testAdapter from '../../test-adapter.js';

const root = new URL('../../fixtures/api-routes/', import.meta.url);
const fileSystem = {
'/src/pages/response-redirect.ts': `export const GET = ({ url }) => Response.redirect("https://example.com/destination", 307)`,
'/src/pages/response.ts': `export const GET = ({ url }) => new Response(null, { headers: { Location: "https://example.com/destination" }, status: 307 })`,
};

describe('endpoints', () => {
let container;
let settings;

before(async () => {
const fs = createFs(fileSystem, root);
settings = await createBasicSettings({
root: fileURLToPath(root),
output: 'server',
adapter: testAdapter(),
});
container = await createContainer({
fs,
settings,
logger: defaultLogger,
});
});

after(async () => {
await container.close();
});

it('should return a redirect response with location header', async () => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/response-redirect',
});
container.handle(req, res);
await done;
expect(res.getHeaders()).to.deep.include({ location: 'https://example.com/destination' });
expect(res.statusCode).to.equal(307);
});

it('should return a response with location header', async () => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/response',
});
container.handle(req, res);
await done;
expect(res.getHeaders()).to.deep.include({ location: 'https://example.com/destination' });
expect(res.statusCode).to.equal(307);
});
});
Loading