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
Let’s say I have a /user/:userId route, and within that route, I use a fetcher form to toggle the user’s activity. I use fetcher to prevent navigation.
However, when JavaScript is disabled and I submit the fetcher form, the UI shows the changes as expected, but when I click the back button, the server processes the form submission again. This happens because I didn’t follow the "redirect after POST" rule, so the browser tries to re-submit the form data.
On the other hand, if I do a redirect inside the action, then with JavaScript enabled, the fetcher causes a navigation instead of staying on the same /user/:userId page—which defeats the purpose of using a fetcher.
My proposed solution is to include a hidden input using the useHydrated hook. This input would only be added when JavaScript is enabled. Then, on the backend, I check for the presence of that field in formData:
If it exists (i.e., JS is on), I return null to stay on the same page.
If it doesn’t (i.e., JS is off), I redirect after the form submission to avoid re-submission on back button.
Does this sound like a valid solution, or am I missing something?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Let’s say I have a /user/:userId route, and within that route, I use a fetcher form to toggle the user’s activity. I use fetcher to prevent navigation.
However, when JavaScript is disabled and I submit the fetcher form, the UI shows the changes as expected, but when I click the back button, the server processes the form submission again. This happens because I didn’t follow the "redirect after POST" rule, so the browser tries to re-submit the form data.
On the other hand, if I do a redirect inside the action, then with JavaScript enabled, the fetcher causes a navigation instead of staying on the same /user/:userId page—which defeats the purpose of using a fetcher.
My proposed solution is to include a hidden input using the useHydrated hook. This input would only be added when JavaScript is enabled. Then, on the backend, I check for the presence of that field in formData:
If it exists (i.e., JS is on), I return null to stay on the same page.
If it doesn’t (i.e., JS is off), I redirect after the form submission to avoid re-submission on back button.
Does this sound like a valid solution, or am I missing something?
Beta Was this translation helpful? Give feedback.
All reactions