From 90c2072990952ff0331e8728e74abbcacc355fbf Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Tue, 27 Sep 2022 19:47:58 +0530 Subject: [PATCH] fix: post API routes in SSG should warn or error during dev mode (#4878) * Update endpoint.ts * add warning for post routes called when output is not server * Update famous-camels-study.md * Update endpoint.ts * If not get * Resolve changes --- .changeset/famous-camels-study.md | 5 +++++ packages/astro/src/core/endpoint/index.ts | 2 +- packages/astro/src/runtime/server/endpoint.ts | 12 +++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .changeset/famous-camels-study.md diff --git a/.changeset/famous-camels-study.md b/.changeset/famous-camels-study.md new file mode 100644 index 000000000..e20300446 --- /dev/null +++ b/.changeset/famous-camels-study.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +add warning for post routes called when output is not server diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 73c96ae64..7fee0c428 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -41,7 +41,7 @@ export async function call( } const [params] = paramsAndPropsResp; - const response = await renderEndpoint(mod, opts.request, params); + const response = await renderEndpoint(mod, opts.request, params, opts.ssr); if (response instanceof Response) { return { diff --git a/packages/astro/src/runtime/server/endpoint.ts b/packages/astro/src/runtime/server/endpoint.ts index 1a285bab0..31a0069db 100644 --- a/packages/astro/src/runtime/server/endpoint.ts +++ b/packages/astro/src/runtime/server/endpoint.ts @@ -18,9 +18,19 @@ function getHandlerFromModule(mod: EndpointHandler, method: string) { } /** Renders an endpoint request to completion, returning the body. */ -export async function renderEndpoint(mod: EndpointHandler, request: Request, params: Params) { +export async function renderEndpoint( + mod: EndpointHandler, + request: Request, + params: Params, + ssr?: boolean +) { const chosenMethod = request.method?.toLowerCase(); const handler = getHandlerFromModule(mod, chosenMethod); + if (!ssr && ssr === false && chosenMethod && chosenMethod !== 'get') { + // eslint-disable-next-line no-console + console.warn(` +${chosenMethod} requests are not available when building a static site. Update your config to output: 'server' to handle ${chosenMethod} requests.`); + } if (!handler || typeof handler !== 'function') { // No handler found, so this should be a 404. Using a custom header // to signal to the renderer that this is an internal 404 that should