diff --git a/packages/astro/test/actions.test.ts b/packages/astro/test/actions.test.ts index 73a55be537c2..91affd5c5225 100644 --- a/packages/astro/test/actions.test.ts +++ b/packages/astro/test/actions.test.ts @@ -509,6 +509,58 @@ it('Works with adapter and all pages prerendered', async () => { await devServer.stop(); }); +describe('Astro Actions with adapter and all project pages prerendered', () => { + it('exposes action RPC routes in dev', async () => { + const fixture = await loadFixture({ + root: './fixtures/actions-all-prerendered/', + adapter: testAdapter(), + }); + const devServer = await fixture.startDevServer(); + assert.ok(devServer, 'Expected dev server to start'); + + try { + const res = await fixture.fetch('/_actions/ping', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ message: 'hello' }), + }); + + assert.equal(res.ok, true); + assert.equal(res.headers.get('Content-Type'), 'application/json+devalue'); + + const data = devalue.parse(await res.text()); + assert.equal(data.message, 'hello'); + } finally { + await devServer.stop(); + } + }); + + it('builds server output and exposes action RPC routes', async () => { + const fixture = await loadFixture({ + root: './fixtures/actions-all-prerendered/', + adapter: testAdapter(), + }); + await fixture.build(); + const app = await fixture.loadTestAdapterApp(); + const req = new Request('http://example.com/_actions/ping', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ message: 'hello' }), + }); + const res = await app.render(req); + + assert.equal(res.ok, true); + assert.equal(res.headers.get('Content-Type'), 'application/json+devalue'); + + const data = devalue.parse(await res.text()); + assert.equal(data.message, 'hello'); + }); +}); + it('Base path should be used', async () => { const fixture = await loadFixture({ root: './fixtures/actions/', diff --git a/packages/astro/test/fixtures/actions-all-prerendered/astro.config.mjs b/packages/astro/test/fixtures/actions-all-prerendered/astro.config.mjs new file mode 100644 index 000000000000..5fcf16f98722 --- /dev/null +++ b/packages/astro/test/fixtures/actions-all-prerendered/astro.config.mjs @@ -0,0 +1,8 @@ +import { defineConfig } from 'astro/config'; + +export default defineConfig({ + output: 'server', + security: { + checkOrigin: false, + }, +}); diff --git a/packages/astro/test/fixtures/actions-all-prerendered/package.json b/packages/astro/test/fixtures/actions-all-prerendered/package.json new file mode 100644 index 000000000000..916597b61fcd --- /dev/null +++ b/packages/astro/test/fixtures/actions-all-prerendered/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/actions-all-prerendered", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/actions-all-prerendered/src/actions/index.ts b/packages/astro/test/fixtures/actions-all-prerendered/src/actions/index.ts new file mode 100644 index 000000000000..d2ab0beb5977 --- /dev/null +++ b/packages/astro/test/fixtures/actions-all-prerendered/src/actions/index.ts @@ -0,0 +1,13 @@ +import { defineAction } from 'astro:actions'; +import { z } from 'astro/zod'; + +export const server = { + ping: defineAction({ + input: z.object({ + message: z.string(), + }), + handler: async ({ message }) => { + return { message }; + }, + }), +}; diff --git a/packages/astro/test/fixtures/actions-all-prerendered/src/pages/index.astro b/packages/astro/test/fixtures/actions-all-prerendered/src/pages/index.astro new file mode 100644 index 000000000000..f6e8d17cfdd5 --- /dev/null +++ b/packages/astro/test/fixtures/actions-all-prerendered/src/pages/index.astro @@ -0,0 +1,5 @@ +--- +export const prerender = true; +--- + +

All project pages are prerendered.

diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d50f0d8b24b4..b9f069601a72 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1901,6 +1901,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/actions-all-prerendered: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/alias: dependencies: '@astrojs/svelte':