Skip to content

Commit 32cb7e9

Browse files
ENGCOM-9098: Fix js errors in customer data when component not initialized #31940
2 parents 37f9218 + 116c379 commit 32cb7e9

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ define([
1717
], function ($, _, ko, sectionConfig, url) {
1818
'use strict';
1919

20-
var options = {},
20+
var options = {
21+
cookieLifeTime: 86400 //1 day by default
22+
},
2123
storage,
2224
storageInvalidation,
2325
invalidateCacheBySessionTimeOut,
@@ -30,6 +32,22 @@ define([
3032
url.setBaseUrl(window.BASE_URL);
3133
options.sectionLoadUrl = url.build('customer/section/load');
3234

35+
/**
36+
* Storage initialization
37+
*/
38+
function initStorage() {
39+
$.cookieStorage.setConf({
40+
path: '/',
41+
expires: new Date(Date.now() + parseInt(options.cookieLifeTime, 10) * 1000),
42+
samesite: 'lax'
43+
});
44+
storage = $.initNamespaceStorage('mage-cache-storage').localStorage;
45+
storageInvalidation = $.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage;
46+
}
47+
48+
// Initialize storage with default parameters to prevent JS errors while component still not initialized
49+
initStorage();
50+
3351
/**
3452
* @param {Object} invalidateOptions
3553
*/
@@ -216,15 +234,7 @@ define([
216234
/**
217235
* Storage init
218236
*/
219-
initStorage: function () {
220-
$.cookieStorage.setConf({
221-
path: '/',
222-
expires: new Date(Date.now() + parseInt(options.cookieLifeTime, 10) * 1000),
223-
samesite: 'lax'
224-
});
225-
storage = $.initNamespaceStorage('mage-cache-storage').localStorage;
226-
storageInvalidation = $.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage;
227-
},
237+
initStorage: initStorage,
228238

229239
/**
230240
* Retrieve the list of sections that has expired since last page reload.
@@ -389,7 +399,10 @@ define([
389399
*/
390400
'Magento_Customer/js/customer-data': function (settings) {
391401
options = settings;
402+
403+
// re-init storage with a new settings
392404
customerData.initStorage();
405+
393406
invalidateCacheBySessionTimeOut(settings);
394407
invalidateCacheByCloseCookieSession();
395408
customerData.init();

0 commit comments

Comments
 (0)