New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug in Resetting the Form DOM on Browser Back/Forth #6128
Comments
It is interesting that when using pure SSR without Next.js, it gives same result. Here is demo: https://github.com/doniyor2109/select-bug |
It‘s probably related to the Back-Forward/Page-Cache in Chrome. The problem in a react app is that the internal state does not match what is shown on screen. I guess next.js would need to rerender the current state after the page gets navigated to, maybe using the |
@fabb Yes. It seems Chrome implements page caching. However it works on Safari as expected. |
pasting a private conversation I had with Tim:
|
I took this issue thinking that it could be a simple one, oh boy I was wrong. After a bazillion hours testing stuff on different setups, I came to the conclusion that browsers need to grow up and do similar stuff when it comes to bfcache. Right now I'm getting like at least 10 different inconsistencies between what Chrome, Edge, Safari and Firefox do. Sometimes, they are not even respectful of their own documentation. Here's some of the highlights of my research:
So I came up with the PR that you can see above, haven't added tests nor docs yet but I hope it's a step in the right direction! I fully recommend folks jumping into random issues, I ended up learning a ton of stuff related to how the History API works! |
I created a reproduction of the bug in Next.js and added it to the Chrome bug that @stefanpenner opened (mentioned in the pull request): https://bugs.chromium.org/p/chromium/issues/detail?id=1143298#c7 |
I ran into this today. The workaround of adding a key to the component seemed to fix the issue for me: |
I don't use Next.js, just React and had the same issue. After trying many solutions I just set the field name to a random string. On each page load, even back/forward the name is different, so the browser doesn't try to restore it. This is one infuriating bug. I never noticed it before as I use Firefox, had a coworker point it out. |
I run into this as well using just react + vite with this functional approach instead: function App() {
const [myradio, setMyRadio] = useState(undefined);
const [myselect, setMySelect] = useState("one");
const onChangeRadio = event => {
setMyRadio(event.target.value);
};
const onChangeSelect = event => {
setMySelect(event.target.value);
};
return (
<div>
<input
type="radio"
value="one"
name="myradio"
required
onChange={onChangeRadio}
checked={myradio === "one"}
/>
<input
type="radio"
value="two"
name="myradio"
required
checked={myradio === "two"}
onChange={onChangeRadio}
/>
<select
name="myselect"
required
value={myselect}
onChange={onChangeSelect}
>
<option value="">Please choose...</option>
<option value="one">One</option>
<option value="two">Two</option>
</select>
</div>
);
} But, i dont think this is a bug, because it can be solved with useEffect(() => {
// Store the current state in the browser history when the component unmounts
const state = { myradio, myselect };
window.history.pushState(state, null);
return () => {
// Remove the state from the browser history when the component mounts again
window.history.replaceState({}, null);
};
}, [myradio, myselect]);
useEffect(() => {
// Restore the state from the browser history when the component mounts
const state = window.history.state || {};
setMyRadio(state.myradio || undefined);
setMySelect(state.myselect || "one");
}, []); But i dont know if is the developer responsability to manage state persistance using back and forward buttons, or of this should be react defaults behaviour, but anyways i think this issue should be closed because is not nextjs specific. |
Bug report
Describe the bug
In Chrome, when page navigating back/forth to a next.js page that contains a form with previously selected radio and select values, these selected values are still shown, but do not match the react component state. This leads to business logic errors, since the fields are expected to be empty or set to some other initial values.
In a create-react-app this works fine, so I guess it has something to do with how next.js handles the
popstate
event.To Reproduce
I have created a full example repo here: https://github.com/fabb/select-bug
npx create-next-app select-next-test
npm run dev
Actual Buggy Behavior
Expected behavior
Caveat
checked
andvalue
attributes, even though they are correctly set when doingconsole.log
.Screenshots
System information
The text was updated successfully, but these errors were encountered: