sveltejs / sveltejs/kit

Automatically reset `use:enhance` form after redirect

Open
#11,694 0 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

In response to #7163, commit f8a8428e35459e38bf93d5cd3d8b7a0e02a89c8c was made, which automatically resets a progressively-enhanced form if the result type was success. Love it.

In the case of a redirect back to the same page, though, the form does not get reset.

I have to add code to manually check for a redirect result if I want this behavior:

  <form
    action="?/askQuestion"
    method="post"
    use:enhance={({ formElement }) => {
      return ({ result, update }) => {
        if (['success', 'redirect'].includes(result.type)) {
          formElement.reset();
        }
        update();
      };
    }}
Describe the proposed solution

I'd like to request that the form also reset when the result type is redirect. (When either use:enhance was called with no arguments or when update was called.)

The reason: Redirect after post is a common pattern, and after a successful form post I like to redirect(303, url.pathname) in order to remove the ?/actionName queryparam from the URL.

Thanks for considering it.

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

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 at the use:enhance implementation and its handling of success, redirect, and update results. Reproduce or add coverage for a progressively enhanced form submitted with a redirect, and consider the work done when the form resets for redirect results both with default behavior and when update is called.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.