Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: post API routes in SSG should warn or error during dev mode #4878

Merged
merged 8 commits into from Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-camels-study.md
@@ -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
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
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