Skip to content

Commit

Permalink
Fix base not working with server actions (#12280)
Browse files Browse the repository at this point in the history
Co-authored-by: Fredrik Löwenhamn <[email protected]>
  • Loading branch information
lowet84 and Fredrik Löwenhamn authored Oct 30, 2024
1 parent 413503e commit ea85546
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/astro/src/actions/runtime/virtual/get-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function getAction(
path: string,
): Promise<ActionClient<unknown, ActionAccept, ZodType>> {
const pathKeys = path
.replace('/_actions/', '')
.replace(/^.*\/_actions\//, '')
.split('.')
.map((key) => decodeURIComponent(key));
// @ts-expect-error virtual module
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/templates/actions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit ea85546

Please sign in to comment.