Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 3001311

Browse files
author
Joan He
authored
Merge pull request #3230 from magento-trigger/MC-4277
[trigger] MC-4277: Address Product Page slowness due to 'product_data_storage' handling
2 parents 32219a9 + 8e5d2a0 commit 3001311

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

app/code/Magento/Catalog/view/frontend/web/js/product/storage/data-storage.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ define([
3434
};
3535
}
3636

37+
/**
38+
* Set data to localStorage with support check.
39+
*
40+
* @param {String} namespace
41+
* @param {Object} data
42+
*/
43+
function setLocalStorageItem(namespace, data) {
44+
try {
45+
window.localStorage.setItem(namespace, JSON.stringify(data));
46+
} catch (e) {
47+
console.warn('localStorage is unavailable - skipping local caching of product data');
48+
console.error(e);
49+
}
50+
}
51+
3752
return {
3853

3954
/**
@@ -118,7 +133,7 @@ define([
118133
if (_.isEmpty(data)) {
119134
this.localStorage.removeAll();
120135
} else {
121-
this.localStorage.set(data);
136+
setLocalStorageItem(this.namespace, data);
122137
}
123138
},
124139

app/code/Magento/Catalog/view/frontend/web/js/product/storage/ids-storage.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ define([
1111
], function ($, _, ko, utils) {
1212
'use strict';
1313

14+
/**
15+
* Set data to localStorage with support check.
16+
*
17+
* @param {String} namespace
18+
* @param {Object} data
19+
*/
20+
function setLocalStorageItem(namespace, data) {
21+
try {
22+
window.localStorage.setItem(namespace, JSON.stringify(data));
23+
} catch (e) {
24+
console.warn('localStorage is unavailable - skipping local caching of product data');
25+
console.error(e);
26+
}
27+
}
28+
1429
return {
1530

1631
/**
@@ -94,11 +109,7 @@ define([
94109
* Initializes handler to "data" property update
95110
*/
96111
internalDataHandler: function (data) {
97-
var localStorage = this.localStorage.get();
98-
99-
if (!utils.compare(data, localStorage).equal) {
100-
this.localStorage.set(data);
101-
}
112+
setLocalStorageItem(this.namespace, data);
102113
},
103114

104115
/**

app/code/Magento/Catalog/view/frontend/web/js/product/storage/storage-service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ define([
4747
* @param {*} data
4848
*/
4949
add: function (data) {
50-
if (!_.isEmpty(data) && !utils.compare(data, this.data()).equal) {
50+
if (!_.isEmpty(data)) {
5151
this.data(_.extend(utils.copy(this.data()), data));
5252
}
5353
},

dev/tests/js/jasmine/tests/app/code/Magento/Catalog/frontend/js/product/storage/data-storage.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ define([
4949
injector.clean();
5050
injector.remove();
5151
} catch (e) {}
52+
window.localStorage.clear();
5253
});
5354

5455
describe('Magento_Catalog/js/product/storage/data-storage', function () {
@@ -88,18 +89,20 @@ define([
8889
};
8990
});
9091

92+
afterEach(function () {
93+
window.localStorage.clear();
94+
});
95+
9196
it('check calls "dataHandler" method with data', function () {
9297
var data = {
9398
property: 'value'
9499
};
95100

96101
obj.dataHandler(data);
97-
expect(obj.localStorage.set).toHaveBeenCalledWith(data);
98-
expect(obj.localStorage.removeAll).not.toHaveBeenCalled();
102+
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(data));
99103
});
100104
it('check calls "dataHandler" method with empty data', function () {
101105
obj.dataHandler({});
102-
expect(obj.localStorage.set).not.toHaveBeenCalled();
103106
expect(obj.localStorage.removeAll).toHaveBeenCalled();
104107
});
105108
});

dev/tests/js/jasmine/tests/app/code/Magento/Catalog/frontend/js/product/storage/ids-storage.test.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ define([
2727
injector.clean();
2828
injector.remove();
2929
} catch (e) {}
30+
window.localStorage.clear();
3031
});
3132

3233
describe('Magento_Catalog/js/product/storage/ids-storage', function () {
@@ -40,13 +41,6 @@ define([
4041
expect(obj.localStorage.get).toHaveBeenCalled();
4142
});
4243
});
43-
describe('"cachesDataFromLocalStorage" method', function () {
44-
it('check calls localStorage get method', function () {
45-
obj.getDataFromLocalStorage = jasmine.createSpy().and.returnValue({});
46-
47-
expect(obj.localStorage.get).toHaveBeenCalled();
48-
});
49-
});
5044
describe('"initLocalStorage" method', function () {
5145
it('check returned value', function () {
5246
obj.namespace = 'test';
@@ -80,17 +74,16 @@ define([
8074
it('check calls with data that equal with data in localStorage', function () {
8175
obj.internalDataHandler(data);
8276

83-
expect(obj.localStorage.get).toHaveBeenCalled();
84-
expect(obj.localStorage.set).not.toHaveBeenCalled();
77+
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(data));
8578
});
8679

8780
it('check calls with data that not equal with data in localStorage', function () {
8881
var emptyData = {};
8982

83+
obj.internalDataHandler(data);
9084
obj.internalDataHandler(emptyData);
9185

92-
expect(obj.localStorage.get).toHaveBeenCalled();
93-
expect(obj.localStorage.set).toHaveBeenCalledWith(emptyData);
86+
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(emptyData));
9487
});
9588
});
9689
describe('"externalDataHandler" method', function () {

0 commit comments

Comments
 (0)