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

Add a warning when using encoding in SSR and headers on objects in endpoints #6358

Merged
merged 9 commits into from
Feb 27, 2023
5 changes: 5 additions & 0 deletions .changeset/wise-bikes-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add warning when using headers and encoding in endpoints in SSR
5 changes: 2 additions & 3 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import type {
RouteData,
SSRElement,
} from '../../@types/astro';
import type { LogOptions } from '../logger/core.js';
import type { RouteInfo, SSRManifest as Manifest } from './types';

import mime from 'mime';
import { attachToResponse, getSetCookiesFromResponse } from '../cookies/index.js';
import { call as callEndpoint } from '../endpoint/index.js';
import { consoleLogDestination } from '../logger/console.js';
import { error } from '../logger/core.js';
import { error, type LogOptions } from '../logger/core.js';
import { joinPaths, prependForwardSlash, removeTrailingForwardSlash } from '../path.js';
import {
createEnvironment,
Expand Down Expand Up @@ -228,7 +227,7 @@ export class App {
status,
});

const result = await callEndpoint(handler, this.#env, ctx);
const result = await callEndpoint(handler, this.#env, ctx, this.#logging);

if (result.type === 'response') {
if (result.response.headers.get('X-Astro-Response') === 'Not-Found') {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ async function generatePath(
let encoding: BufferEncoding | undefined;
if (pageData.route.type === 'endpoint') {
const endpointHandler = mod as unknown as EndpointHandler;
const result = await callEndpoint(endpointHandler, env, ctx);
const result = await callEndpoint(endpointHandler, env, ctx, logging);

if (result.type === 'response') {
throwIfRedirectNotAllowed(result.response, opts.settings.config);
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/src/core/endpoint/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { EndpointHandler } from '../../../@types/astro';
import type { LogOptions } from '../../logger/core';
import type { SSROptions } from '../../render/dev';
import { createRenderContext } from '../../render/index.js';
import { call as callEndpoint } from '../index.js';

export async function call(options: SSROptions) {
export async function call(options: SSROptions, logging: LogOptions) {
const {
env,
preload: [, mod],
Expand All @@ -17,5 +18,5 @@ export async function call(options: SSROptions) {
route: options.route,
});

return await callEndpoint(endpointHandler, env, ctx);
return await callEndpoint(endpointHandler, env, ctx, logging);
}
22 changes: 21 additions & 1 deletion packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { renderEndpoint } from '../../runtime/server/index.js';
import { ASTRO_VERSION } from '../constants.js';
import { AstroCookies, attachToResponse } from '../cookies/index.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { LogOptions, warn } from '../logger/core.js';
import { getParamsAndProps, GetParamsAndPropsError } from '../render/core.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');
Expand Down Expand Up @@ -71,7 +72,8 @@ function createAPIContext({
export async function call(
mod: EndpointHandler,
env: Environment,
ctx: RenderContext
ctx: RenderContext,
logging: LogOptions
): Promise<EndpointCallResult> {
const paramsAndPropsResp = await getParamsAndProps({
mod: mod as any,
Expand Down Expand Up @@ -111,6 +113,24 @@ export async function call(
};
}

if (env.ssr) {
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
if (response.hasOwnProperty('headers')) {
warn(
logging,
'ssr',
'Setting headers is not supported when returning an object. Please return an instance of Response. See https://docs.astro.build/en/core-concepts/endpoints/#server-endpoints-api-routes for more information.'
);
}

if (response.encoding) {
warn(
logging,
'ssr',
'`encoding` is ignored in SSR. To return a charset other than utf-8, please return an instance of Response. See https://docs.astro.build/en/core-concepts/endpoints/#server-endpoints-api-routes for more information.'
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

return {
type: 'simple',
body: response.body,
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export async function handleRoute(

// Route successfully matched! Render it.
if (route.type === 'endpoint') {
const result = await callEndpoint(options);
const result = await callEndpoint(options, logging);
if (result.type === 'response') {
if (result.response.headers.get('X-Astro-Response') === 'Not-Found') {
const fourOhFourRoute = await matchRoute('/404', env, manifest);
Expand Down