Skip to content

Commit

Permalink
Allow 200 response for endpoints in build
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Oct 31, 2022
1 parent 0e043bb commit 512d5bf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-ducks-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allow 200 response for endpoints in build
21 changes: 11 additions & 10 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '../../core/path.js';
import { runHookBuildGenerated } from '../../integrations/index.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { call as callEndpoint } from '../endpoint/index.js';
import { call as callEndpoint, throwIfRedirectNotAllowed } from '../endpoint/index.js';
import { debug, info } from '../logger/core.js';
import { createEnvironment, createRenderContext, renderPage } from '../render/index.js';
import { callGetStaticPaths } from '../render/route-cache.js';
Expand Down Expand Up @@ -374,18 +374,19 @@ async function generatePath(
const result = await callEndpoint(endpointHandler, env, ctx);

if (result.type === 'response') {
throw new Error(`Returning a Response from an endpoint is not supported in SSG mode.`);
throwIfRedirectNotAllowed(result.response, opts.settings.config);
// If there's no body, do nothing
if (!result.response.body) return;
body = await result.response.text();
} else {
body = result.body;
encoding = result.encoding;
}
body = result.body;
encoding = result.encoding;
} else {
const response = await renderPage(mod, ctx, env);

// If there's a redirect or something, just do nothing.
if (response.status !== 200 || !response.body) {
return;
}

throwIfRedirectNotAllowed(response, opts.settings.config);
// If there's no body, do nothing
if (!response.body) return;
body = await response.text();
}

Expand Down
14 changes: 13 additions & 1 deletion packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { APIContext, EndpointHandler, Params } from '../../@types/astro';
import type { APIContext, AstroConfig, EndpointHandler, Params } from '../../@types/astro';
import type { Environment, RenderContext } from '../render/index';

import { renderEndpoint } from '../../runtime/server/index.js';
Expand Down Expand Up @@ -113,3 +113,15 @@ export async function call(
cookies: context.cookies,
};
}

function isRedirect(statusCode: number) {
return statusCode >= 300 && statusCode < 400;
}

export function throwIfRedirectNotAllowed(response: Response, config: AstroConfig) {
if (config.output !== 'server' && isRedirect(response.status)) {
throw new Error(
`Redirects are only available when using output: 'server'. Update your Astro config if you need SSR features.`
);
}
}
13 changes: 1 addition & 12 deletions packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { createRequest } from '../core/request.js';
import { createRouteManifest, matchAllRoutes } from '../core/routing/index.js';
import { resolvePages } from '../core/util.js';
import notFoundTemplate, { subpathNotUsedTemplate } from '../template/4xx.js';
import { throwIfRedirectNotAllowed } from '../core/endpoint/index.js';

interface AstroPluginOptions {
settings: AstroSettings;
Expand Down Expand Up @@ -293,18 +294,6 @@ async function handleRequest(
}
}

function isRedirect(statusCode: number) {
return statusCode >= 300 && statusCode < 400;
}

function throwIfRedirectNotAllowed(response: Response, config: AstroConfig) {
if (config.output !== 'server' && isRedirect(response.status)) {
throw new Error(
`Redirects are only available when using output: 'server'. Update your Astro config if you need SSR features.`
);
}
}

async function handleRoute(
matchedRoute: AsyncReturnType<typeof matchRoute>,
url: URL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const get = () => {
return new Response(
undefined,
{
status: 301,
headers: {
Location: 'https://example.com',
}
}
);
};

0 comments on commit 512d5bf

Please sign in to comment.