Skip to content
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

Fix back button popstate not working on the first pushState #137

Merged
merged 1 commit into from
Jan 3, 2024

Conversation

fritx
Copy link
Collaborator

@fritx fritx commented Dec 29, 2023

Apperance:
The back button does not work right after the first link navigation, but begins to work after the second navigation.
( Tried Chrome 120.0.6099.129 and Safari 16.6 (17615.3.12.11.3, 17615) )

Reason:
The first popstate e.state=null, and getting path=undefined
image

Steps to reproduce:

cd simple-blog
# both
nue
# and
nue build --production
nue serve --production

Fix Approach:
Record the initial_path and use it when path is undefined.

Or maybe, just loadPage(location.pathname), regardless of e.state.path ? ref: https://github.com/fritx/silent/blob/2x/blog/vendor/blog.js#L409-L413

ref: window.onpopstate, event.state == null?
https://stackoverflow.com/questions/11092736/window-onpopstate-event-state-null

@fritx fritx changed the title Fix back button popstate not working for the first time Fix back button popstate not working on the first pushState Dec 29, 2023
@fritx fritx force-pushed the fix-backbtn-popstate branch 2 times, most recently from 5813697 to 04f4c1c Compare December 29, 2023 10:06
@tipiirai tipiirai merged commit 06e1336 into nuejs:master Jan 3, 2024
@tipiirai
Copy link
Contributor

tipiirai commented Jan 3, 2024

Thank you @fritx. These fixes are great!

@tanwwg
Copy link

tanwwg commented Feb 5, 2024

I don't think it's fixed yet, I get null null whenever I hit the back button on 0.3.3.

@fritx
Copy link
Collaborator Author

fritx commented Feb 7, 2024

Still: window.onpopstate, event.state == null?
https://stackoverflow.com/questions/11092736/window-onpopstate-event-state-null

Looks like calling history.pushState(initialState) twice is more reliable. Thoughts? cc @nobkd
I'm not sure as I tried it only once and it worked fine on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants