Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Fixed bug: restore origin history state when set history. #456

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions src/js/smooth-scroll/smooth-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,18 +269,24 @@
var setHistory = function (options) {

// Make sure this should run
if (!history.replaceState || !options.updateURL || history.state) return;
if (!history.replaceState || !options.updateURL || (history.state && history.state.smoothScroll)) return;

// Get the hash to use
var hash = window.location.hash;
hash = hash ? hash : window.pageYOffset;

var historyState = {
smoothScroll: JSON.stringify(options),
anchor: hash ? hash : window.pageYOffset
};
if (history.state) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the bit above, but can you explain to me why this is needed?

Copy link
Contributor Author

@dingyi1993 dingyi1993 Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at this screen shot below, this is a SPA which is using vue and vue router.

When I push a new route, history state will automatically set by vue router, if you just replace history state, vue router will not work correctly.

If you use react router or set some state by yourself, this has the same problem.

image

for (var key in history.state) {
historyState[key] = history.state[key];
}
}
// Set a default history
history.replaceState(
{
smoothScroll: JSON.stringify(options),
anchor: hash ? hash : window.pageYOffset
},
historyState,
document.title,
hash ? hash : window.location.href
);
Expand Down