Skip to content

use:enhance does not emulate native browser behaviour of redirecting to the action #10847

@teemingc

Description

@teemingc

Discussed in #10842

Native form submission always navigates to the route specified in the form action attribute. However, use:enhance doesn’t reflect this since it only navigates to another page if you explicitly throw a redirect in your action in +page.server.js.

We should document this in https://kit.svelte.dev/docs/form-actions#progressive-enhancement-use-enhance

Another thing we could document is how to emulate the navigation to the action route after a successful response with a goto call https://kit.svelte.dev/docs/modules#$app-navigation-goto

<form
  action="/somewhere/else"
  method="post"
	use:enhance={({ formElement, formData, action, cancel }) => {

		return async ({ result }) => {
			// emulate native browser form redirect
			if (result.type === "success") {
				await goto(action);
			}
			await applyAction(result);
		};
	}}
>
  <button type="submit">
    Submit
  </button>
</form>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions