It's funny... I modified the code a bit in order to make further tests. First I fixated the navbar on the top of the viewport, and then even when I was navigating normally I encountered the same error. Then I changed the logic of the "scroll restoration", by removing the ready-to-use component from react router and implementing the useEffect/useLocation logic to scroll up in a parent component. After that I took @elegantseagulls's suggestion and explicitly called ScrollTrigger.refresh(), after the the gsap context inside the same useLayoutEffect that contains all the animations and the problem completely disappeared.
But when I do the same in my actual project, it is still not working...
@elegantseagulls I did the demo in incognito mode so it wouldn't change the "real" code, and I observed no change, but it wasn't saved though... Sorry! If it is really that important for this issue, I can create another demo and post here just with these changes.
@Rodrigo Thanks for the reply! What was going on, and maybe I should have explained that better, is that every time the user is looking at the second, third or fourth sections on the second page, and then pressing 'return' on the browser (that should bring the user back to the first page), although the page would scroll up to the top as intended, the color of the background, that should be the '#99f' of the first section, was another color (the same as one of the sections below).
But now the demo is working, and I don't really understand why - isn't the gsap context supposed to "restart" every ScrollTrigger whenever it is remounted? Why calling ScrollTrigger.refresh() right after the context did the trick? Anyway, my real code is still not working, but the demo posted here is, so I will try investigate some more about why one is working and the other is not, and in the meanwhile I would really appreciate if someone here could explain to me what's really going on with this logic...