Skip to content

Commit 771d1f7

Browse files
authoredMay 14, 2024··
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
1 parent ad9227c commit 771d1f7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
 

‎.changeset/many-news-rescue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Throw on missing server output when using Astro Actions.

‎packages/astro/src/actions/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import { mkdir, readFile, writeFile } from 'node:fs/promises';
22
import type { Plugin as VitePlugin } from 'vite';
33
import type { AstroIntegration } from '../@types/astro.js';
4-
import { viteID } from '../core/util.js';
4+
import { viteID, isServerLikeOutput } from '../core/util.js';
55
import { ACTIONS_TYPES_FILE, RESOLVED_VIRTUAL_MODULE_ID, VIRTUAL_MODULE_ID } from './consts.js';
6+
import { AstroError } from '../core/errors/errors.js';
7+
import { ActionsWithoutServerOutputError } from '../core/errors/errors-data.js';
68

79
export default function astroActions(): AstroIntegration {
810
return {
911
name: VIRTUAL_MODULE_ID,
1012
hooks: {
1113
async 'astro:config:setup'(params) {
14+
if (!isServerLikeOutput(params.config)) {
15+
const error = new AstroError(ActionsWithoutServerOutputError);
16+
error.stack = undefined;
17+
throw error;
18+
}
19+
1220
const stringifiedActionsImport = JSON.stringify(
1321
viteID(new URL('./actions', params.config.srcDir))
1422
);

‎packages/astro/src/core/errors/errors-data.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,21 @@ export const DuplicateContentEntrySlugError = {
14931493
},
14941494
} satisfies ErrorData;
14951495

1496+
/**
1497+
* @docs
1498+
* @see
1499+
* - [On-demand rendering](https://docs.astro.build/en/basics/rendering-modes/#on-demand-rendered)
1500+
* @description
1501+
* Your project must have a server output to create backend functions with Actions.
1502+
*/
1503+
export const ActionsWithoutServerOutputError = {
1504+
name: 'ActionsWithoutServerOutputError',
1505+
title: 'Actions must be used with server output.',
1506+
message:
1507+
'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.',
1508+
hint: 'Learn about on-demand rendering: https://docs.astro.build/en/basics/rendering-modes/#on-demand-rendered',
1509+
} satisfies ErrorData;
1510+
14961511
/**
14971512
* @docs
14981513
* @see

0 commit comments

Comments
 (0)
Please sign in to comment.