I have a POST form (simple HTML, no JS involved in submission) which is partially validated server-side. When I submit it, server may return a redirect if there are any errors. Let’s say, the form is on /myform URI, but if the server found errors in data it will redirect to /myform?error. After this is done several times and finally correctly submitted, user may want to use his browser’s back button. But it makes user to go through all the steps of failed submission attempts, e.g. /myform?success -> /myform?error -> /myform -> /home.
How do I prevent this? My guess is to remove last History entries that have same URI. How do I achieve that? Or is there another better way? I use server side rendering with Spring (Java) so dynamic validation is not really an option for the use-case. Thank you in advance!
For security and privacy reasons, you can't view or modify a user's browser history.
However, if you're willing to use JavaScript, you can use the History API to replace the current page's state on successful submit:
history.replaceState(null, 'Page title', '/form-url');
https://developer.mozilla.org/en-US/docs/Web/API/History_API