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

actions: Throw error on missing server output #11028

Merged
merged 5 commits into from
May 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = {
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved
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
Loading