Skip to content

Commit

Permalink
Fix customer data and sections configuration race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
krzksz committed Oct 4, 2019
1 parent 2cc9d29 commit 4ff04dc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

/** @var \Magento\Customer\Block\CustomerData $block */
?>
<script type="text/x-magento-init">
{
"*": {
"Magento_Customer/js/customer-data": {
"sectionLoadUrl": "<?= $block->escapeJs($block->escapeUrl($block->getCustomerDataUrl('customer/section/load'))) ?>",
"expirableSectionLifetime": <?= (int)$block->getExpirableSectionLifetime() ?>,
"expirableSectionNames": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($block->getExpirableSectionNames()) ?>,
"cookieLifeTime": "<?= $block->escapeJs($block->getCookieLifeTime()) ?>",
"updateSessionUrl": "<?= $block->escapeJs($block->escapeUrl($block->getCustomerDataUrl('customer/account/updateSession'))) ?>"
}
}
}
<script>
requirejs.config({
config: <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode(['Magento_Customer/js/customer-data' => [
'sectionLoadUrl' => $block->getCustomerDataUrl('customer/section/load'),
'expirableSectionLifetime' => $block->getExpirableSectionLifetime(),
'expirableSectionNames' => $block->getExpirableSectionNames(),
'cookieLifeTime' => $block->getCookieLifeTime(),
'updateSessionUrl' => $block->getCustomerDataUrl('customer/account/updateSession'),
]]); ?>

});
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@

/** @var \Magento\Customer\Block\SectionConfig $block */
?>
<script type="text/x-magento-init">
{
"*": {
"Magento_Customer/js/section-config": {
"sections": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($block->getSections()) ?>,
"clientSideSections": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($block->getClientSideSections()) ?>,
"baseUrls": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode(array_unique([
$block->getUrl(null, ['_secure' => true]),
$block->getUrl(null, ['_secure' => false]),
])) ?>,
"sectionNames": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)
->jsonEncode($block->getData('sectionNamesProvider')->getSectionNames()) ?>
}
}
}
<script>
requirejs.config({
config: <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode([
'Magento_Customer/js/section-config' => [
'sections' => $block->getSections(),
'clientSideSections' => $block->getClientSideSections(),
'baseUrls' => array_unique([
$block->getUrl(null, ['_secure' => true]),
$block->getUrl(null, ['_secure' => false]),
]),
]
]); ?>
});
</script>
21 changes: 8 additions & 13 deletions app/code/Magento/Customer/view/frontend/web/js/customer-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ define([
'underscore',
'ko',
'Magento_Customer/js/section-config',
'mage/url',
'module',
'mage/storage',
'jquery/jquery-storageapi'
], function ($, _, ko, sectionConfig, url) {
], function ($, _, ko, sectionConfig, module) {
'use strict';

var options = {},
var options = module.config(),
storage,
storageInvalidation,
invalidateCacheBySessionTimeOut,
Expand All @@ -26,9 +26,6 @@ define([
buffer,
customerData;

url.setBaseUrl(window.BASE_URL);
options.sectionLoadUrl = url.build('customer/section/load');

//TODO: remove global change, in this case made for initNamespaceStorage
$.cookieStorage.setConf({
path: '/',
Expand Down Expand Up @@ -337,17 +334,15 @@ define([
},

/**
* @param {Object} settings
* @constructor
*/
'Magento_Customer/js/customer-data': function (settings) {
options = settings;
invalidateCacheBySessionTimeOut(settings);
invalidateCacheByCloseCookieSession();
customerData.init();
}
'Magento_Customer/js/customer-data': function () {}
};

invalidateCacheBySessionTimeOut(options);
invalidateCacheByCloseCookieSession();
customerData.init();

/**
* Events listener
*/
Expand Down
17 changes: 8 additions & 9 deletions app/code/Magento/Customer/view/frontend/web/js/section-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
* See COPYING.txt for license details.
*/

define(['underscore'], function (_) {
define(['underscore', 'module'], function (_, module) {
'use strict';

var baseUrls, sections, clientSideSections, sectionNames, canonize;
var options = module.config(),
baseUrls = options.baseUrls,
sections = options.sections,
clientSideSections = options.clientSideSections,
sectionNames = options.sectionNames,
canonize;

/**
* @param {String} url
Expand Down Expand Up @@ -80,14 +85,8 @@ define(['underscore'], function (_) {
},

/**
* @param {Object} options
* @constructor
*/
'Magento_Customer/js/section-config': function (options) {
baseUrls = options.baseUrls;
sections = options.sections;
clientSideSections = options.clientSideSections;
sectionNames = options.sectionNames;
}
'Magento_Customer/js/section-config': function () {}
};
});

0 comments on commit 4ff04dc

Please sign in to comment.