Skip to content

applyAction should navigate to action if action is different from current route #9999

Closed

Description

@paoloricciuti

Describe the bug

If a form has an action to another route and you use enhance as per doc it doesn't automatically navigate to the new route.

But from the doc it seems like using applyAction should apply the default browser behavior.

This doesn't seem to be the case: the action gets called but the routing doesn't happen.

Reproduction

  1. Go to https://www.sveltelab.dev/j9dffhs0vu1rqub
  2. Try to submit the form
  3. Observe that the navigation doesn't happen
  4. Try to remove the use:enhance
  5. Submit the form
  6. Observe that the browser correctly redirect there

Logs

No response

System Info

System:  OS: Linux 5.0 undefined CPU: (8) x64 Intel(R) Core(TM) i9-9880 H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries:  Node: 16.14.2 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 7.17.0 - /usr/local/bin/npm npmPackages:  @sveltejs/adapter-auto: "2.0.0 => 2.1. 
0 
@sveltejs/kit: "1.5.0 => 1.18.0 svelte: "3.54.0 => 3.59.1 vite: "4.1.4 => 4.3.8

Severity

serious, but I can work around it

Additional Information

To be clear this is only present when the action return a success, not when it return a redirect

Activity

  1. elliott-with-the-longest-name-on-github commented on May 21, 2023

    @elliott-with-the-longest-name-on-github
    Contributor

    Confirmed behavior is as reported -- looks like a 🐛

  2. added
    bug
    forms
    on May 21, 2023
  3. added
    documentation
    and removed
    bug
    on May 22, 2023
  4. dummdidumm commented on May 22, 2023

    @dummdidumm
    Member

    This is not a bug, it's expected behavior. If you have a form on page X that references actions on page Y you don't navigate there on success. That allows you to implement things like a newsletter signup inside a +layout.svelte which is present on all pages and where you can handle the success message/errors on the page you're currently on, not losing context. It should probably made clearer in the docs.

  5. paoloricciuti commented on May 22, 2023

    @paoloricciuti
    MemberAuthor

    Shouldn't this behavior be already covered by not calling applyAction? I feel like applyAction should have the same behavior of a non enhanced form

  6. dummdidumm commented on May 22, 2023

    @dummdidumm
    Member

    No, because applyAction ensures the form prop is updated which is crucial for error messages etc and sometimes for success cases, too.

  7. paoloricciuti commented on May 22, 2023

    @paoloricciuti
    MemberAuthor

    So the actual recommended way is to manually goto?

  8. paoloricciuti commented on May 22, 2023

    @paoloricciuti
    MemberAuthor

    Actually now that I think about it manually goto is not an option if you want to use the form object in the new page right?

  9. dummdidumm commented on May 22, 2023

    @dummdidumm
    Member

    You could pass a query string or save things in a cookie, or put it in event.locals

    At this point I'm wondering why the form is even on another page at all, what's the use case?

  10. paoloricciuti commented on May 22, 2023

    @paoloricciuti
    MemberAuthor

    It was for helping a folk in the svelte discord...he wanted to do something with the form data and than show a recap in the /rec page ..it seems a pretty reasonable use case honestly and given that's how the platform usually behave it doesn't seem wrong to thing you could do this. But I get the reasoning. Maybe it could be an option on applyAction?

    Again, it's not something that I need...is just something that seemed off to me, totally ok if you prefer to leave it like this

  11. dummdidumm commented on May 22, 2023

    @dummdidumm
    Member

    Honestly I think it's the wrong solution to (ab)use the fact that you're redirected to the /rec page in the native case. What about error handling? That would also happen on /rec then which is not what this page is about. local.recap = ..; throw redirect('/rec'); within the action sounds like a better solution to me.

  12. paoloricciuti commented on May 22, 2023

    @paoloricciuti
    MemberAuthor

    Again it's not a matter of what's better...I would probably do something like this myself if that's something that I would need. It was only a fact of being completely different from the platform.

    That's it.

  13. dummdidumm commented on May 22, 2023

    @dummdidumm
    Member

    All good - I just wanted to point it out because sometimes feature requests are solved by doing things differently instead.

  14. elliott-with-the-longest-name-on-github commented on May 22, 2023

    @elliott-with-the-longest-name-on-github
    Contributor

    I'm with @paoloricciuti; it seems like a weird decision to make enhance incompatible with the normal HTML semantics in this one case.

  15. dummdidumm commented on May 22, 2023

    @dummdidumm
    Member

    #9999 (comment) explains why we need this behavior, we can't satisfy both, and getting rid of the ability of using forms in layout files without jumping through hoops is worse IMO (and a breaking change).

  16. Rich-Harris commented on May 22, 2023

    @Rich-Harris
    Member

    Honestly this seems like a bug to me too. I don't recall what discussions (if any) led to the current behaviour, but it absolutely seems like we should be navigating to the page with the action. use:enhance is supposed to emulate native browser behaviour, and not navigating seems like a very opinionated choice that clearly isn't universally beneficial

  17. paoloricciuti commented on May 22, 2023

    @paoloricciuti
    MemberAuthor

    What if we let the user opt in (if we want to avoid breaking changes) or opt out (if we want to better mimick the browser by default) of this behavior with an extra parameter on applyAction?

  18. dummdidumm commented on May 22, 2023

    @dummdidumm
    Member

    Looking at the code and its comments and the docs which mention this case indirectly it's pretty clear we made a conscious decision to do it the way it works today.

  19. elliott-with-the-longest-name-on-github commented on May 22, 2023

    @elliott-with-the-longest-name-on-github
    Contributor

    I'm abandoning the discussion on what would / wouldn't be a breaking change (I want to focus on what the right decision for the framework is -- we can come back to when it lands later). IMO, even if we did make the conscious decision that the way it works today is the way it should work, it was the wrong decision. The point of a "naked" enhance is to emulate native behavior using the client-side router so that users can sprinkle on interactivity without fully abandoning what will happen to JS-less users. The behavior should be (roughly):

    • If POST-ing to an action on the same page, use the current behavior; emulate a reload of the page by invalidating dependencies and populating form
    • If POST-ing to an action on a different page, redirect to that page on success. Populate that page's form prop.
    • Simpler?: Populate the form prop of whatever page happens to be rendered?

    That's a very basic outline. I think whatever we do, we should chart out the possible results like a state machine and decide what the results for each state should be -- then write tests to assert that. We should be able to create a relatively-small tree (basically the cartesian product of (success, redirect, error) and (current page action, different page action). I also think that enhance, when unmodified, should be as close to native behavior as possible -- on a fast internet connection with no laggy datafetches, I shouldn't be able to tell the difference between an unmodified enhanced form and a vanilla HTML one.

  20. paoloricciuti commented on May 22, 2023

    @paoloricciuti
    MemberAuthor

    I'm abandoning the discussion on what would / wouldn't be a breaking change (I want to focus on what the right decision for the framework is -- we can come back to when it lands later). IMO, even if we did make the conscious decision that the way it works today is the way it should work, it was the wrong decision. The point of a "naked" enhance is to emulate native behavior using the client-side router so that users can sprinkle on interactivity without fully abandoning what will happen to JS-less users. The behavior should be (roughly):

    • If POST-ing to an action on the same page, use the current behavior; emulate a reload of the page by invalidating dependencies and populating form
    • If POST-ing to an action on a different page, redirect to that page on success. Populate that page's form prop.
    • Simpler?: Populate the form prop of whatever page happens to be rendered?

    That's a very basic outline. I think whatever we do, we should chart out the possible results like a state machine and decide what the results for each state should be -- then write tests to assert that. We should be able to create a relatively-small tree (basically the cartesian product of (success, redirect, error) and (current page action, different page action). I also think that enhance, when unmodified, should be as close to native behavior as possible -- on a fast internet connection with no laggy datafetches, I shouldn't be able to tell the difference between an unmodified enhanced form and a vanilla HTML one.

    As a user I don't even mind calling applyAction

  21. added this to the 3.0 milestone on Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

documentationforms

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions