You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
applyAction should navigate to action if action is different from current route #9999
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.
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
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.
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.
#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).
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
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?
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.
elliott-with-the-longest-name-on-github commented on May 22, 2023
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'sform 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.
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'sform 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.
Activity
elliott-with-the-longest-name-on-github commentedon May 21, 2023
Confirmed behavior is as reported -- looks like a 🐛
dummdidumm commentedon May 22, 2023
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.sveltewhich 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.paoloricciuti commentedon May 22, 2023
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
dummdidumm commentedon May 22, 2023
No, because
applyActionensures theformprop is updated which is crucial for error messages etc and sometimes for success cases, too.paoloricciuti commentedon May 22, 2023
So the actual recommended way is to manually goto?
paoloricciuti commentedon May 22, 2023
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?
dummdidumm commentedon May 22, 2023
You could pass a query string or save things in a cookie, or put it in
event.localsAt this point I'm wondering why the form is even on another page at all, what's the use case?
paoloricciuti commentedon May 22, 2023
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
/recpage ..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 onapplyAction?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
dummdidumm commentedon May 22, 2023
Honestly I think it's the wrong solution to (ab)use the fact that you're redirected to the
/recpage in the native case. What about error handling? That would also happen on/recthen which is not what this page is about.local.recap = ..; throw redirect('/rec');within the action sounds like a better solution to me.paoloricciuti commentedon May 22, 2023
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.
dummdidumm commentedon May 22, 2023
All good - I just wanted to point it out because sometimes feature requests are solved by doing things differently instead.
elliott-with-the-longest-name-on-github commentedon May 22, 2023
I'm with @paoloricciuti; it seems like a weird decision to make enhance incompatible with the normal HTML semantics in this one case.
dummdidumm commentedon May 22, 2023
#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).
Rich-Harris commentedon May 22, 2023
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:enhanceis supposed to emulate native browser behaviour, and not navigating seems like a very opinionated choice that clearly isn't universally beneficialpaoloricciuti commentedon May 22, 2023
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?
dummdidumm commentedon May 22, 2023
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.
elliott-with-the-longest-name-on-github commentedon May 22, 2023
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"
enhanceis 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):formformprop.formprop 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 unmodifiedenhanced form and a vanilla HTML one.paoloricciuti commentedon May 22, 2023
As a user I don't even mind calling
applyAction