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:
parent
883ce82a11
commit
90c2072990
3 changed files with 17 additions and 2 deletions
5
.changeset/famous-camels-study.md
Normal file
5
.changeset/famous-camels-study.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
add warning for post routes called when output is not server
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue