From ea855464870f70af01eba195d8593b39a581d942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20L=C3=B6wenhamn?= Date: Wed, 30 Oct 2024 12:55:39 +0100 Subject: [PATCH] Fix base not working with server actions (#12280) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Löwenhamn --- .../src/actions/runtime/virtual/get-action.ts | 2 +- packages/astro/templates/actions.mjs | 2 +- packages/astro/test/actions.test.js | 24 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/actions/runtime/virtual/get-action.ts b/packages/astro/src/actions/runtime/virtual/get-action.ts index 59db34cbc0..7cd260f866 100644 --- a/packages/astro/src/actions/runtime/virtual/get-action.ts +++ b/packages/astro/src/actions/runtime/virtual/get-action.ts @@ -12,7 +12,7 @@ export async function getAction( path: string, ): Promise> { const pathKeys = path - .replace('/_actions/', '') + .replace(/^.*\/_actions\//, '') .split('.') .map((key) => decodeURIComponent(key)); // @ts-expect-error virtual module diff --git a/packages/astro/templates/actions.mjs b/packages/astro/templates/actions.mjs index 349693eba3..d030c36379 100644 --- a/packages/astro/templates/actions.mjs +++ b/packages/astro/templates/actions.mjs @@ -92,7 +92,7 @@ async function handleAction(param, path, context) { headers.set('Content-Length', '0'); } } - const rawResult = await fetch(`/_actions/${path}`, { + const rawResult = await fetch(`${import.meta.env.BASE_URL.replace(/\/$/, "")}/_actions/${path}`, { method: 'POST', body, headers, diff --git a/packages/astro/test/actions.test.js b/packages/astro/test/actions.test.js index 793e6ebe77..2c2d3a2c31 100644 --- a/packages/astro/test/actions.test.js +++ b/packages/astro/test/actions.test.js @@ -466,6 +466,30 @@ describe('Astro Actions', () => { }); }); +it('Base path should be used', async () => { + const fixture = await loadFixture({ + root: './fixtures/actions/', + adapter: testAdapter(), + base: "/base" + }); + const devServer = await fixture.startDevServer(); + const formData = new FormData(); + formData.append('channel', 'bholmesdev'); + formData.append('comment', 'Hello, World!'); + const res = await fixture.fetch('/base/_actions/comment', { + method: 'POST', + body: formData, + }); + + 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.channel, 'bholmesdev'); + assert.equal(data.comment, 'Hello, World!'); + await devServer.stop() +}); + /** * Follow an expected redirect response. *