Skip to content

Commit 2402537

Browse files
committed
Fix js errors in customer data when component not initialized
1 parent ad0ba79 commit 2402537

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

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

Lines changed: 19 additions & 9 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,18 @@ define([
3032
url.setBaseUrl(window.BASE_URL);
3133
options.sectionLoadUrl = url.build('customer/section/load');
3234

35+
function initStorage() {
36+
$.cookieStorage.setConf({
37+
path: '/',
38+
expires: new Date(Date.now() + parseInt(options.cookieLifeTime, 10) * 1000)
39+
});
40+
storage = $.initNamespaceStorage('mage-cache-storage').localStorage;
41+
storageInvalidation = $.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage;
42+
}
43+
44+
// Initialize storage with default parameters to prevent JS errors while component still not initialized
45+
initStorage();
46+
3347
/**
3448
* @param {Object} invalidateOptions
3549
*/
@@ -216,14 +230,7 @@ define([
216230
/**
217231
* Storage init
218232
*/
219-
initStorage: function () {
220-
$.cookieStorage.setConf({
221-
path: '/',
222-
expires: new Date(Date.now() + parseInt(options.cookieLifeTime, 10) * 1000)
223-
});
224-
storage = $.initNamespaceStorage('mage-cache-storage').localStorage;
225-
storageInvalidation = $.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage;
226-
},
233+
initStorage: initStorage,
227234

228235
/**
229236
* Retrieve the list of sections that has expired since last page reload.
@@ -363,7 +370,10 @@ define([
363370
*/
364371
'Magento_Customer/js/customer-data': function (settings) {
365372
options = settings;
373+
374+
// re-init storage with a new settings
366375
customerData.initStorage();
376+
367377
invalidateCacheBySessionTimeOut(settings);
368378
invalidateCacheByCloseCookieSession();
369379
customerData.init();

0 commit comments

Comments
 (0)