vikejs / vike

🔨 Flexible, lean, community-driven, dependable, fast Vite-based frontend framework.
https://vike.dev
MIT License
4.38k stars 350 forks source link

isHashNavigation is true when go back from history.pushState #1840

Closed pelly-ryu closed 2 months ago

pelly-ryu commented 2 months ago

Description

How to reproduce

Expected behavior

External

the page's content changes to the previous page's (result: shows result with no category selected.)

Internal

as URL hash changed(/seeks?category=etc->/seeks), isHashNavigation is false or null, so renderPageClientSide is called.

Actual behavior

External

the page's content doesn't change; shows the wrong(old) result.

Internal

Though it is not a hash navigation, isHashNavigation is true. So Vike sets only the scroll position.

Comment

  1. There's no issue when from triggeredBy=user to triggeredBy=user. It can be handled as the doc describes.
  2. It seems it happens because Vike got wrong globalObject.previousState.urlWithoutHash, and of course it did, because Vike had no way to store the previous URL which had been changed via history API.
  3. I think it would be solved if Vike has own function(or an option of navigate) wrapping history.pushState and remember urls.
  4. For my specific case, I can work around with implementing some logic in popstate event listener. Or I think I can consider replacing all pushStates to navigate, though need to test first if it is possible. But generally, I think it is a bug.
brillout commented 2 months ago

Can you publish a reproduction?

brillout commented 2 months ago

Actually, I ain't sure I understand your issue. Why not using navigate() instead of history.pushState()?

brillout commented 2 months ago

Closing, but let's continue the conversation and let's re-open if it turns out to be a Vike bug.

pelly-ryu commented 1 month ago

Oh I missed your comments, sorry for late response. Thanks for following up on this.

Summary:

  1. I couldn't reproduce the issue in its original context -> No bug.
  2. I've since changed my implementation(to use navigate), so I'm no longer experiencing the problem.

I just tried to reproduce it but couldn't. Back when I first reported this, not sure why, the isHashNavigation value seemed off, but that's not the case anymore. So either there's no bug or I couldn't recreate the environment where the bug occurred.

FYI, Here's what I initially tried to fix it : (source: https://github.com/pelly-ryu/vike-backward-nav )

https://github.com/user-attachments/assets/77376867-ac5b-43e0-83c3-5231637a56d1

There's still some quirky behavior, but it seems not to due to a Vike's bug. Given that I've moved to a different implementation, I won't be pursuing this further.