Search code examples
sveltekit

SvelteKit form actions and redirect


In my SvelteKit app I have a form that uses a form action:

<form action="/login?/logout" method="POST">
    <button type="submit">Logout</button>
</form>

In this action the final step is to redirect:

export const actions: Actions = {
    logout: async (event) => {
        // ...
        throw redirect(307, '/login');
    }
}

This was working as I intended. After updating to SvelteKit 1.0.0 the behavior changed: The action is called (as before) but when redirecting I get the following error:

Error: No action with name 'default' found

Any ideas what needs to be changed or how this should be done?


Solution

  • Status code 307 will not change the method and body of the request, so the browser will try to POST /login.

    You could change the status code to 303 instead to change the method to GET:

    export const actions: Actions = {
        logout: async (event) => {
            // ...
            throw redirect(303, '/login');
        }
    }