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

Avoid scroll on first load #169

Merged
merged 5 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export default Mixin.create({
const url = get(this, 'currentURL');
const hashElement = url ? document.getElementById(url.split('#').pop()) : null

if (get(this, 'service.isFirstLoad')) {
get(this, 'service').unsetFirstLoad();
Copy link
Collaborator Author

@snewcomer snewcomer Nov 5, 2018

Choose a reason for hiding this comment

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

So we are currently calling scrollTo on first load here - however has no effect on first load for some reason on fast pages 🤷‍♂️. As detailed in the referenced issue, on first load, we should not try to scroll the page when we first enter the application. On slow requests, this does show up from time to time.

So using a flag isFirstLoad, we can prevent unnecessary work.

https://github.com/dollarshaveclub/ember-router-scroll/blob/master/addon/index.js#L75

return;
}

let scrollPosition;

if(url && url.indexOf('#') > -1 && hashElement) {
Expand Down
20 changes: 11 additions & 9 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@ export default Service.extend({
return fastboot ? fastboot.get('isFastBoot') : false;
}),

key: null,
scrollElement: 'window',
targetElement: null,
delayScrollTop: false,
isFirstLoad: true,

init(...args) {
this._super(...args);
this._loadConfig();
set(this, 'scrollMap', { default: { x: 0, y: 0 }});
set(this, 'key', null);
},

unsetFirstLoad() {
set(this, 'isFirstLoad', false);
},

update() {
if (get(this, 'isFastBoot') || get(this, 'isFirstLoad')) {
return;
}

const scrollElement = get(this, 'scrollElement');
const targetElement = get(this, 'targetElement');
const scrollMap = get(this, 'scrollMap');
Expand All @@ -30,10 +39,6 @@ export default Service.extend({
let y;

if (targetElement) {
if (get(this, 'isFastBoot')) {
return;
}

let element = document.querySelector(targetElement);
if (element) {
x = element.offsetLeft;
Expand All @@ -47,10 +52,6 @@ export default Service.extend({
x = window.scrollX;
y = window.scrollY;
} else if ('#' === scrollElement.charAt(0)) {
if (get(this, 'isFastBoot')) {
return;
}

let element = document.getElementById(scrollElement.substring(1));

if (element) {
Expand All @@ -59,6 +60,7 @@ export default Service.extend({
}
}

// only a `key` present after first load
if (key && 'number' === typeOf(x) && 'number' === typeOf(y)) {
set(scrollMap, key, { x, y });
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-router-scroll",
"version": "1.0.0",
"version": "1.0.1",
"description": "Scroll to top with preserved browser history scroll position",
"keywords": [
"ember-addon",
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/services/router-scroll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module('service:router-scroll', function(hooks) {
});

test('updating will set `scrollMap` to the current scroll position', function(assert) {
const service = this.owner.lookup('service:router-scroll');
const service = this.owner.factoryFor('service:router-scroll').create({ isFirstLoad: false });

const expected = { x: window.scrollX, y: window.scrollY };
set(service, 'key', '123');
Expand Down Expand Up @@ -91,7 +91,7 @@ module('service:router-scroll', function(hooks) {
otherElem.setAttribute('id', 'other-elem');
const testing = document.querySelector('#ember-testing');
testing.appendChild(otherElem);
const service = this.owner.factoryFor('service:router-scroll').create({ scrollElement: '#other-elem' });
const service = this.owner.factoryFor('service:router-scroll').create({ isFirstLoad: false, scrollElement: '#other-elem' });
window.history.replaceState({ uuid: '123' }, null);

let expected = { x: 0, y: 0 };
Expand All @@ -107,7 +107,7 @@ module('service:router-scroll', function(hooks) {
otherElem.style.top = '100px';
const testing = document.querySelector('#ember-testing');
testing.appendChild(otherElem);
const service = this.owner.factoryFor('service:router-scroll').create({ targetElement: '#other-elem' });
const service = this.owner.factoryFor('service:router-scroll').create({ isFirstLoad: false, targetElement: '#other-elem' });
window.history.replaceState({ uuid: '123' }, null);

let expected = { x: 0, y: 0 };
Expand Down