From 771d1f7654e18b657c3eacfabae52ed88c76fa99 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Tue, 14 May 2024 13:29:27 -0400 Subject: [PATCH] actions: Throw error on missing server output (#11028) * feat: throw error on missing server output * chore: changeset * refactor: use isServerLikeOutput * feat: add errors-data on actions build output * chore: add jsdoc --- .changeset/many-news-rescue.md | 5 +++++ packages/astro/src/actions/index.ts | 10 +++++++++- packages/astro/src/core/errors/errors-data.ts | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 .changeset/many-news-rescue.md diff --git a/.changeset/many-news-rescue.md b/.changeset/many-news-rescue.md new file mode 100644 index 000000000000..038ad499dea0 --- /dev/null +++ b/.changeset/many-news-rescue.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Throw on missing server output when using Astro Actions. diff --git a/packages/astro/src/actions/index.ts b/packages/astro/src/actions/index.ts index e251ed72f68d..e2f58c2fba25 100644 --- a/packages/astro/src/actions/index.ts +++ b/packages/astro/src/actions/index.ts @@ -1,14 +1,22 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises'; import type { Plugin as VitePlugin } from 'vite'; import type { AstroIntegration } from '../@types/astro.js'; -import { viteID } from '../core/util.js'; +import { viteID, isServerLikeOutput } from '../core/util.js'; import { ACTIONS_TYPES_FILE, RESOLVED_VIRTUAL_MODULE_ID, VIRTUAL_MODULE_ID } from './consts.js'; +import { AstroError } from '../core/errors/errors.js'; +import { ActionsWithoutServerOutputError } from '../core/errors/errors-data.js'; export default function astroActions(): AstroIntegration { return { name: VIRTUAL_MODULE_ID, hooks: { async 'astro:config:setup'(params) { + if (!isServerLikeOutput(params.config)) { + const error = new AstroError(ActionsWithoutServerOutputError); + error.stack = undefined; + throw error; + } + const stringifiedActionsImport = JSON.stringify( viteID(new URL('./actions', params.config.srcDir)) ); diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index 761160565cc5..cd75acc150ca 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -1493,6 +1493,21 @@ export const DuplicateContentEntrySlugError = { }, } satisfies ErrorData; +/** + * @docs + * @see + * - [On-demand rendering](https://docs.astro.build/en/basics/rendering-modes/#on-demand-rendered) + * @description + * Your project must have a server output to create backend functions with Actions. + */ +export const ActionsWithoutServerOutputError = { + name: 'ActionsWithoutServerOutputError', + title: 'Actions must be used with server output.', + message: + 'Actions enabled without setting a server build output. A server is required to create callable backend functions. To deploy routes to a server, add a server adapter to your astro config.', + hint: 'Learn about on-demand rendering: https://docs.astro.build/en/basics/rendering-modes/#on-demand-rendered', +} satisfies ErrorData; + /** * @docs * @see