Skip to content

Commit 730dadf

Browse files
ENGCOM-3373: [Backport] #17813 - Huge 'product_data_storage' in localStorage hangs the shop #19014
- Merge Pull Request #19014 from omiroshnichenko/magento2:17813 - Merged commits: 1. 57a85b8
2 parents 20a3ebe + 57a85b8 commit 730dadf

File tree

5 files changed

+64
-28
lines changed

5 files changed

+64
-28
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: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ define([
4444
});
4545
});
4646

47+
afterEach(function () {
48+
try {
49+
injector.clean();
50+
injector.remove();
51+
} catch (e) {}
52+
window.localStorage.clear();
53+
});
54+
4755
describe('Magento_Catalog/js/product/storage/data-storage', function () {
4856
describe('"initCustomerDataInvalidateListener" method', function () {
4957
it('check returned value', function () {
@@ -81,18 +89,20 @@ define([
8189
};
8290
});
8391

92+
afterEach(function () {
93+
window.localStorage.clear();
94+
});
95+
8496
it('check calls "dataHandler" method with data', function () {
8597
var data = {
8698
property: 'value'
8799
};
88100

89101
obj.dataHandler(data);
90-
expect(obj.localStorage.set).toHaveBeenCalledWith(data);
91-
expect(obj.localStorage.removeAll).not.toHaveBeenCalled();
102+
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(data));
92103
});
93104
it('check calls "dataHandler" method with empty data', function () {
94105
obj.dataHandler({});
95-
expect(obj.localStorage.set).not.toHaveBeenCalled();
96106
expect(obj.localStorage.removeAll).toHaveBeenCalled();
97107
});
98108
});
@@ -307,13 +317,13 @@ define([
307317
});
308318
describe('"hasIdsInSentRequest" method', function () {
309319
var ids = {
310-
'1': {
311-
id: '1'
312-
},
313-
'2': {
314-
id: '2'
315-
}
316-
};
320+
'1': {
321+
id: '1'
322+
},
323+
'2': {
324+
id: '2'
325+
}
326+
};
317327

318328
beforeEach(function () {
319329
obj.request = {

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ define([
2222
});
2323
});
2424

25+
afterEach(function () {
26+
try {
27+
injector.clean();
28+
injector.remove();
29+
} catch (e) {}
30+
window.localStorage.clear();
31+
});
32+
2533
describe('Magento_Catalog/js/product/storage/ids-storage', function () {
2634
describe('"getDataFromLocalStorage" method', function () {
2735
it('check calls localStorage get method', function () {
@@ -33,13 +41,6 @@ define([
3341
expect(obj.localStorage.get).toHaveBeenCalled();
3442
});
3543
});
36-
describe('"cachesDataFromLocalStorage" method', function () {
37-
it('check calls localStorage get method', function () {
38-
obj.getDataFromLocalStorage = jasmine.createSpy().and.returnValue({});
39-
40-
expect(obj.localStorage.get).toHaveBeenCalled();
41-
});
42-
});
4344
describe('"initLocalStorage" method', function () {
4445
it('check returned value', function () {
4546
obj.namespace = 'test';
@@ -73,17 +74,16 @@ define([
7374
it('check calls with data that equal with data in localStorage', function () {
7475
obj.internalDataHandler(data);
7576

76-
expect(obj.localStorage.get).toHaveBeenCalled();
77-
expect(obj.localStorage.set).not.toHaveBeenCalled();
77+
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(data));
7878
});
7979

8080
it('check calls with data that not equal with data in localStorage', function () {
8181
var emptyData = {};
8282

83+
obj.internalDataHandler(data);
8384
obj.internalDataHandler(emptyData);
8485

85-
expect(obj.localStorage.get).toHaveBeenCalled();
86-
expect(obj.localStorage.set).toHaveBeenCalledWith(emptyData);
86+
expect(window.localStorage.getItem(obj.namespace)).toBe(JSON.stringify(emptyData));
8787
});
8888
});
8989
describe('"externalDataHandler" method', function () {

0 commit comments

Comments
 (0)