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
This commit is contained in:
Rishi Raj Jain 2022-09-27 19:47:58 +05:30 committed by GitHub
parent 883ce82a11
commit 90c2072990
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
add warning for post routes called when output is not server

View file

@ -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 {

View file

@ -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