From 8167f252f62b5a74f999bc97c838dc0e2f2728c8 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 24 Feb 2023 16:47:02 +0100 Subject: [PATCH 1/8] fix(endpoints): Add a warning when trying to set encoding and headers in SSR --- .changeset/wise-bikes-fix.md | 5 +++++ packages/astro/src/core/app/index.ts | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 .changeset/wise-bikes-fix.md diff --git a/.changeset/wise-bikes-fix.md b/.changeset/wise-bikes-fix.md new file mode 100644 index 000000000000..96628546e8a3 --- /dev/null +++ b/.changeset/wise-bikes-fix.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Add warning when using headers and encoding in endpoints in SSR diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index c9ec49bd3c20..dae05c0d57f7 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -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, warn, type LogOptions } from '../logger/core.js'; import { joinPaths, prependForwardSlash, removeTrailingForwardSlash } from '../path.js'; import { createEnvironment, @@ -240,6 +239,22 @@ export class App { } return result.response; } else { + if (result.hasOwnProperty('headers')) { + warn( + this.#logging, + 'ssr', + 'Setting headers is not supported when returning an object. Please return an instance of Response.' + ); + } + + if (result.encoding) { + warn( + this.#logging, + 'ssr', + '`encoding` is ignored in SSR. To return a charset other than utf-8, please return an instance of Response.' + ); + } + const body = result.body; const headers = new Headers(); const mimeType = mime.getType(url.pathname); From c9dd9133d51c5248a9ca18221ebfe0bc25543e31 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 24 Feb 2023 16:55:31 +0100 Subject: [PATCH 2/8] fix(endpoint): Oops, it'd be great if it actually worked --- packages/astro/src/core/app/index.ts | 20 ++--------------- packages/astro/src/core/build/generate.ts | 2 +- packages/astro/src/core/endpoint/dev/index.ts | 5 +++-- packages/astro/src/core/endpoint/index.ts | 22 ++++++++++++++++++- .../src/vite-plugin-astro-server/route.ts | 2 +- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index dae05c0d57f7..c97131b43b73 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -11,7 +11,7 @@ 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, warn, type LogOptions } from '../logger/core.js'; +import { error, type LogOptions } from '../logger/core.js'; import { joinPaths, prependForwardSlash, removeTrailingForwardSlash } from '../path.js'; import { createEnvironment, @@ -227,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') { @@ -239,22 +239,6 @@ export class App { } return result.response; } else { - if (result.hasOwnProperty('headers')) { - warn( - this.#logging, - 'ssr', - 'Setting headers is not supported when returning an object. Please return an instance of Response.' - ); - } - - if (result.encoding) { - warn( - this.#logging, - 'ssr', - '`encoding` is ignored in SSR. To return a charset other than utf-8, please return an instance of Response.' - ); - } - const body = result.body; const headers = new Headers(); const mimeType = mime.getType(url.pathname); diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 3b163611ae75..7ae4de6a1e23 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -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); diff --git a/packages/astro/src/core/endpoint/dev/index.ts b/packages/astro/src/core/endpoint/dev/index.ts index 889b82345fce..515b7aa41a8e 100644 --- a/packages/astro/src/core/endpoint/dev/index.ts +++ b/packages/astro/src/core/endpoint/dev/index.ts @@ -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], @@ -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); } diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 533bebddd2df..977d937bde66 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -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'; import { getParamsAndProps, GetParamsAndPropsError } from '../render/core.js'; const clientAddressSymbol = Symbol.for('astro.clientAddress'); @@ -71,7 +72,8 @@ function createAPIContext({ export async function call( mod: EndpointHandler, env: Environment, - ctx: RenderContext + ctx: RenderContext, + logging: LogOptions ): Promise { const paramsAndPropsResp = await getParamsAndProps({ mod: mod as any, @@ -111,6 +113,24 @@ export async function call( }; } + if (env.ssr) { + if (response.hasOwnProperty('headers')) { + warn( + logging, + 'ssr', + 'Setting headers is not supported when returning an object. Please return an instance of Response.' + ); + } + + 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.' + ); + } + } + return { type: 'simple', body: response.body, diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts index 85643034f995..d26fc62d70e5 100644 --- a/packages/astro/src/vite-plugin-astro-server/route.ts +++ b/packages/astro/src/vite-plugin-astro-server/route.ts @@ -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); From 1a7888316b2fceb731b70ae5ab2a148418a21e7a Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 24 Feb 2023 16:56:11 +0100 Subject: [PATCH 3/8] fix(endpoint): Fix import path --- packages/astro/src/core/endpoint/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 977d937bde66..ee96d7d9ef86 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -5,7 +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'; +import { LogOptions, warn } from '../logger/core.js'; import { getParamsAndProps, GetParamsAndPropsError } from '../render/core.js'; const clientAddressSymbol = Symbol.for('astro.clientAddress'); From c3754e43a60e52a517143e0eb7b01ef908ecaa39 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 24 Feb 2023 16:58:51 +0100 Subject: [PATCH 4/8] fix(endpoint): Add link to docs --- packages/astro/src/core/endpoint/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index ee96d7d9ef86..80592acb511d 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -118,7 +118,7 @@ export async function call( warn( logging, 'ssr', - 'Setting headers is not supported when returning an object. Please return an instance of Response.' + '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.' ); } @@ -126,7 +126,7 @@ export async function call( warn( logging, 'ssr', - '`encoding` is ignored in SSR. To return a charset other than utf-8, please return an instance of Response.' + '`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.' ); } } From f7a528209abebc1c3673ddf41d8344a7c0ccfab2 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Fri, 24 Feb 2023 18:22:33 +0100 Subject: [PATCH 5/8] Update packages/astro/src/core/endpoint/index.ts Co-authored-by: Emanuele Stoppa --- packages/astro/src/core/endpoint/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 80592acb511d..4cc658e8bd9b 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -126,7 +126,7 @@ export async function call( 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.' + '`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.' ); } } From bb8aa99dfc28084c8f8b3e9bac5f755eb15f2cfd Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Mon, 27 Feb 2023 16:32:18 +0100 Subject: [PATCH 6/8] fix: don't output encoding warning if endpoint is pre-rendered --- packages/astro/src/core/endpoint/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 4cc658e8bd9b..73bbb5211a98 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -122,7 +122,7 @@ export async function call( ); } - if (response.encoding) { + if (response.encoding && !mod.prerender) { warn( logging, 'ssr', From 621b6a4fc6a4dd48d1fe09a94ea9df5a1606d83c Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 27 Feb 2023 09:44:25 -0600 Subject: [PATCH 7/8] Update packages/astro/src/core/endpoint/index.ts --- packages/astro/src/core/endpoint/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 73bbb5211a98..2a6e581b2d1d 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -113,7 +113,7 @@ export async function call( }; } - if (env.ssr) { + if (env.ssr && !mod.prerender) { if (response.hasOwnProperty('headers')) { warn( logging, From d87a6cb56b2bdd13b191125591b320143808e5e5 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 27 Feb 2023 09:44:31 -0600 Subject: [PATCH 8/8] Update packages/astro/src/core/endpoint/index.ts --- packages/astro/src/core/endpoint/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 2a6e581b2d1d..fbfa8cfd40b3 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -122,7 +122,7 @@ export async function call( ); } - if (response.encoding && !mod.prerender) { + if (response.encoding) { warn( logging, 'ssr',