Skip to content

Fix js errors in customer data when component not initialized #31940

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

Merged
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
33 changes: 23 additions & 10 deletions app/code/Magento/Customer/view/frontend/web/js/customer-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ define([
], function ($, _, ko, sectionConfig, url) {
'use strict';

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

/**
* Storage initialization
*/
function initStorage() {
$.cookieStorage.setConf({
path: '/',
expires: new Date(Date.now() + parseInt(options.cookieLifeTime, 10) * 1000),
samesite: 'lax'
});
storage = $.initNamespaceStorage('mage-cache-storage').localStorage;
storageInvalidation = $.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage;
}

// Initialize storage with default parameters to prevent JS errors while component still not initialized
initStorage();

/**
* @param {Object} invalidateOptions
*/
Expand Down Expand Up @@ -216,15 +234,7 @@ define([
/**
* Storage init
*/
initStorage: function () {
$.cookieStorage.setConf({
path: '/',
expires: new Date(Date.now() + parseInt(options.cookieLifeTime, 10) * 1000),
samesite: 'lax'
});
storage = $.initNamespaceStorage('mage-cache-storage').localStorage;
storageInvalidation = $.initNamespaceStorage('mage-cache-storage-section-invalidation').localStorage;
},
initStorage: initStorage,

/**
* Retrieve the list of sections that has expired since last page reload.
Expand Down Expand Up @@ -364,7 +374,10 @@ define([
*/
'Magento_Customer/js/customer-data': function (settings) {
options = settings;

// re-init storage with a new settings
customerData.initStorage();

invalidateCacheBySessionTimeOut(settings);
invalidateCacheByCloseCookieSession();
customerData.init();
Expand Down