Skip to content

Commit

Permalink
Merge pull request #127 from vizath/fix-scrollRestoration
Browse files Browse the repository at this point in the history
Make sure scrollRestoration is writable.
  • Loading branch information
taion authored Feb 5, 2018
2 parents 849937c + 931513f commit 4f6f2a5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export default class ScrollBehavior {
!isMobileSafari()
) {
this._oldScrollRestoration = window.history.scrollRestoration;
window.history.scrollRestoration = 'manual';
try {
window.history.scrollRestoration = 'manual';
} catch (e) {
this._oldScrollRestoration = null;
}
} else {
this._oldScrollRestoration = null;
}
Expand Down Expand Up @@ -126,7 +130,11 @@ export default class ScrollBehavior {
stop() {
/* istanbul ignore if: not supported by any browsers on Travis */
if (this._oldScrollRestoration) {
window.history.scrollRestoration = this._oldScrollRestoration;
try {
window.history.scrollRestoration = this._oldScrollRestoration;
} catch (e) {
/* silence */
}
}

off(window, 'scroll', this._onWindowScroll);
Expand Down
23 changes: 23 additions & 0 deletions test/ScrollBehavior.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ describe('ScrollBehavior', () => {
},
]);
});

it('should not crash when history is not available', (done) => {
Object.defineProperty(window.history, 'scrollRestoration', {
value: 'auto',
// See https://github.com/taion/scroll-behavior/issues/126
writable: false,
enumerable: true,
configurable: true,
});

const history = withRoutes(withScroll(createHistory()));

unlisten = run(history, [
() => {
expect(scrollTop(window)).to.equal(0);

delete window.history.scrollRestoration;
window.history.scrollRestoration = 'auto';

done();
},
]);
});
});

describe('custom behavior', () => {
Expand Down

0 comments on commit 4f6f2a5

Please sign in to comment.