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