Skip to content

Commit

Permalink
fix: post API routes in SSG should warn or error during dev mode (#4878)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Rishi Raj Jain authored Sep 27, 2022
1 parent 883ce82 commit 90c2072
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-camels-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

add warning for post routes called when output is not server
2 changes: 1 addition & 1 deletion packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion packages/astro/src/runtime/server/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 90c2072

Please sign in to comment.