Skip to content

Commit

Permalink
actions: Throw error on missing server output (#11028)
Browse files Browse the repository at this point in the history
* feat: throw error on missing server output

* chore: changeset

* refactor: use isServerLikeOutput

* feat: add errors-data on actions build output

* chore: add jsdoc
  • Loading branch information
bholmesdev authored May 14, 2024
1 parent ad9227c commit 771d1f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/many-news-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Throw on missing server output when using Astro Actions.
10 changes: 9 additions & 1 deletion packages/astro/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -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))
);
Expand Down
15 changes: 15 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 771d1f7

Please sign in to comment.