Skip to content

Reduce changes to session history entry #230

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface SwitchesResult {
}

interface State {
scrollPos: [number, number];
scrollPos?: [number, number];
}

export interface History {
Expand Down Expand Up @@ -94,15 +94,6 @@ class Pjax {
constructor(options: Partial<Options> = {}) {
Object.assign(this.options, options);

if (this.options.scrollRestoration) {
window.history.scrollRestoration = 'manual';

// Browsers' own restoration is faster and more stable on reload.
window.addEventListener('beforeunload', () => {
window.history.scrollRestoration = 'auto';
});
}

const { defaultTrigger } = this.options;
if (defaultTrigger === true || (defaultTrigger !== false && defaultTrigger.enable !== false)) {
new DefaultTrigger(this).register();
Expand All @@ -117,26 +108,45 @@ class Pjax {
* using a custom library seems to be the only choice.
*/

// Store scroll position and then update the lazy state.
// Ignore non-Pjax history entries.
if (!(event.state && 'pjax' in event.state)) return;

// Store history state and then update.
this.storeHistory();
this.history.pull();

// hashchange events trigger popstate with a null `event.state`.
if (event.state === null) return;

const overrideOptions: Partial<Options> = {};
if (this.options.scrollRestoration && this.history.state) {

if (this.history.state?.scrollPos) {
overrideOptions.scrollTo = this.history.state.scrollPos;

// Read current scroll restoration behaviour.
const restoration = window.history.scrollRestoration;

// Use manual scroll restoration.
if (restoration !== 'manual') {
window.history.scrollRestoration = 'manual';

// Restore original scroll restoration behaviour.
setTimeout(() => {
window.history.scrollRestoration = restoration;
});
}
}

this.load(window.location.href, overrideOptions).catch(() => {});
});
}

storeHistory(): void {
this.history.state = {
scrollPos: [window.scrollX, window.scrollY],
};
const state: State = {};

// Store scroll position if required.
if (this.options.scrollRestoration) {
state.scrollPos = [window.scrollX, window.scrollY];
}

this.history.state = state;
}

/**
Expand Down