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 the problem that scroll position can't restored at page refresh after PJAX enabled. #74

Merged
merged 2 commits into from
Aug 5, 2020

Conversation

gapplef
Copy link
Contributor

@gapplef gapplef commented Aug 2, 2020

PR Checklist

  • The commit message follows guidelines for NexT.
  • Tests for the changes was maked (for bug fixes / features).
    • Muse | Mist have been tested.
    • Pisces | Gemini have been tested.
  • Docs in NexT website have been added / updated (for features).

PR Type

  • Bugfix.
  • Feature.
  • Code style update (formatting, local variables).
  • Refactoring (no functional changes, no api changes).
  • Documentation.
  • Translation.
  • Other... Please describe:

What is the current behavior?

Issue resolved:
The scroll position can't restored at page refresh after PJAX enabled.
For reference, see MoOx/pjax#214

What is the new behavior?

  • Link to demo site with this changes:
  • Screenshots with this changes:

How to use?

In NexT _config.yml:

Fix the problem that scroll position can't restored at page refresh after PJAX enabled.
For reference, see MoOx/pjax#214
@welcome
Copy link

welcome bot commented Aug 2, 2020

Thanks so much for opening your first PR here!

@CLAassistant
Copy link

CLAassistant commented Aug 2, 2020

CLA assistant check
All committers have signed the CLA.

@stevenjoezhang
Copy link
Member

Thanks for your contribution!

@stevenjoezhang stevenjoezhang added this to the 8.0.0 milestone Aug 2, 2020
@stevenjoezhang
Copy link
Member

The cause of the problem is that the following code was not executed when the page was first loaded
https://github.com/MoOx/pjax/blob/480334b18253c721ba648675e90261f948e2bca0/index.js#L287-L289

The scrollRestoration option is confusing. See also MoOx/pjax#111

@stevenjoezhang stevenjoezhang merged commit 6814b16 into next-theme:master Aug 5, 2020
@welcome
Copy link

welcome bot commented Aug 5, 2020

Congrats on merging your first pull request here! 🎉 How awesome!

@PaperStrike
Copy link
Member

PaperStrike commented Apr 27, 2021

Browser's own scroll restoration has a drawback that the restoration usually happens before we can fetch to update our HTML structure, which is bad for our Pjax sites. For example, in a Pjax site, page A at a scroll position of 200, page B has a height of 80, we navigate from A to B and navigate back, then we have page A at a scroll position of 80. (You can try it by visiting our theme-next.js.org. Consider it as the page A, scroll down a lot, enter an article as page B and navigate back.)
Things go worse when the fetch failed as the scroll position changed without content updated.

I believe that's the reason why Pjax developed its own restoration.

My solution to this is setting history.scrollRestoration back to auto before page reload instead of setting scrollRestoration to false in Pjax option. It fixes all these problems. Like:

window.addEventListener('beforeunload', () => {
  history.scrollRestoration = 'auto';
});

Code above should be in next-theme/pjax if accepted.

lingyf pushed a commit to lingyf/hexo-theme-next that referenced this pull request Jan 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants