From 89b8d94b1b20d586e1ca525c30d07587c3f2d8f2 Mon Sep 17 00:00:00 2001 From: Cory Virok Date: Tue, 20 Dec 2022 01:41:30 -0800 Subject: [PATCH] [fix] redirects from actions don't honor the provided status code (#8210) fixes #8209 Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --- .changeset/tidy-pandas-report.md | 5 +++++ packages/kit/src/runtime/server/page/index.js | 2 +- packages/kit/test/apps/basics/test/test.js | 17 +++++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 .changeset/tidy-pandas-report.md diff --git a/.changeset/tidy-pandas-report.md b/.changeset/tidy-pandas-report.md new file mode 100644 index 000000000000..d328468ff264 --- /dev/null +++ b/.changeset/tidy-pandas-report.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Fix form action redirect status code diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js index 84c712d4fd68..463e72d01d71 100644 --- a/packages/kit/src/runtime/server/page/index.js +++ b/packages/kit/src/runtime/server/page/index.js @@ -62,7 +62,7 @@ export async function render_page(event, route, page, options, state, resolve_op // (this also determines status code) action_result = await handle_action_request(event, leaf_node.server); if (action_result?.type === 'redirect') { - return redirect_response(303, action_result.location); + return redirect_response(action_result.status, action_result.location); } if (action_result?.type === 'error') { const error = action_result.error; diff --git a/packages/kit/test/apps/basics/test/test.js b/packages/kit/test/apps/basics/test/test.js index 0193fbbada92..0a8476d163ea 100644 --- a/packages/kit/test/apps/basics/test/test.js +++ b/packages/kit/test/apps/basics/test/test.js @@ -2010,12 +2010,25 @@ test.describe('Actions', () => { ); }); - test('redirect', async ({ page }) => { + test('redirect', async ({ page, javaScriptEnabled }) => { await page.goto('/actions/redirect'); page.click('button'); - await Promise.all([page.waitForResponse('/actions/redirect'), page.waitForNavigation()]); + const [redirect] = await Promise.all([ + page.waitForResponse('/actions/redirect'), + page.waitForNavigation() + ]); + if (javaScriptEnabled) { + expect(await redirect.json()).toEqual({ + type: 'redirect', + location: '/actions/enhance', + status: 303 + }); + } else { + expect(redirect.status()).toBe(303); + expect(redirect.headers()['location']).toBe('/actions/enhance'); + } expect(page.url()).toContain('/actions/enhance'); });