sveltejs / sveltejs/kit

Prevent +error "redirecting" and form data loss in use:enhance

Open
#7,429 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

forms
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

We've all been annoyed when entering and for some reason losing form data. Basically the worst thing that can happen to a site is that a user loses its data at the last step of purchasing, subscribing, etc.

Unfortunately, this is the default behavior of use:enhance. If a server error occurs, it will jump straight to the nearest +error boundary, effectively removing the form and all data. This is not nice UX.

Describe the proposed solution

If the default behaviour is open to change, having it behave as described in the docs in the case of result.type being success or invalid would solve this issue straight away.

A status: number field could even be added to the error type of ActionResult, so you could detect specific errors returned from the server, not just 500. A 503 is useful to know if you should try again soon, for example. It's important for $page.status to be updated, so an error message could be displayed at the form based on the status, for a decent UX.

If the default behavior cannot change, or if it does but to keep the previous behavior optional, adding an option to update that won't render the error would be great:

<form
  method="POST"
  use:enhance={() => {
    return async ({ result, update }) => {
      // `result` is an `ActionResult` object
      // `update` is a function which triggers the logic that would be triggered if this callback wasn't set
      update({renderError: false})
    };
  }}
>

Right now quite complicated logic is required to make a generic workaround, so this simple addition would be very helpful.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

Let me know if it's a quick fix, or if I should make a PR for this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the form-actions documentation for progressive enhancement and the use:enhance entry point described in the issue. Determine how server errors currently reach the nearest +error boundary, then verify that the chosen behavior preserves form data and updates $page.status without unintended redirects.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.