Skip to content
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/modern-years-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: handle redirect thrown in handle hook in response to form action
17 changes: 12 additions & 5 deletions packages/kit/src/runtime/server/page/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ export async function handle_action_json_request(event, options, server) {
const err = normalize_error(e);

if (err instanceof Redirect) {
return action_json({
type: 'redirect',
status: err.status,
location: err.location
});
return action_json_redirect(err);
}

return action_json(
Expand All @@ -100,6 +96,17 @@ function check_incorrect_fail_use(error) {
: error;
}

/**
* @param {import('types').Redirect} redirect
*/
export function action_json_redirect(redirect) {
return action_json({
type: 'redirect',
status: redirect.status,
location: redirect.location
});
}

/**
* @param {import('types').ActionResult} data
* @param {ResponseInit} [init]
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from '../../utils/exports.js';
import { get_option } from '../../utils/options.js';
import { error, json, text } from '../../exports/index.js';
import { action_json_redirect, is_action_json_request } from './page/actions.js';

/* global __SVELTEKIT_ADAPTER_NAME__ */

Expand Down Expand Up @@ -309,6 +310,8 @@ export async function respond(request, options, manifest, state) {
if (e instanceof Redirect) {
const response = is_data_request
? redirect_json_response(e)
: route?.page && is_action_json_request(event)
? action_json_redirect(e)
: redirect_response(e.status, e.location);
add_cookies_to_headers(response.headers, Object.values(cookies_to_add));
return response;
Expand Down
7 changes: 7 additions & 0 deletions packages/kit/test/apps/basics/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export const handle = sequence(
return event.fetch('/prerendering/prerendered-endpoint/api');
}

return resolve(event);
},
async ({ event, resolve }) => {
if (event.url.pathname === '/actions/redirect-in-handle' && event.request.method === 'POST') {
throw redirect(303, '/actions/enhance');
}

return resolve(event);
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('./$types').Actions} */
export const actions = {
default: async () => {
throw new Error('should never get here');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import { enhance } from '$app/forms';
</script>

<form method="post" use:enhance>
<button>Submit</button>
</form>
23 changes: 23 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,29 @@ test.describe('Actions', () => {
expect(page.url()).toContain('/actions/enhance');
});

test('redirect in handle', async ({ page, javaScriptEnabled }) => {
await page.goto('/actions/redirect-in-handle');

page.click('button');

const [redirect] = await Promise.all([
page.waitForResponse('/actions/redirect-in-handle'),
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');
});

test('$page.status reflects error status', async ({ page, app }) => {
await page.goto('/actions/enhance');

Expand Down