Nausika
Nausika

Reputation: 755

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?

Upvotes: 5

Views: 11149

Answers (1)

Tholle
Tholle

Reputation: 112887

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');
    }
}

Upvotes: 17

Related Questions