If you you have been struggling to fix an issue of lax in section_id cookies.
If you have this issue on your website, 99% jQuery events stopped working which depends on this cookie section. As an example, If this error on cart page than Remove item will not be working.
Let’s find out solution for this.
First thing need to do is copy the original Magento_Customer/js/customer-data.js which is located at {root_dir}/vendor/magento/module-customer/view/frontend/web/js/customer-data.js to your active theme.
After that update below code to file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
/** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ /** * @api */ define([ 'jquery', 'underscore', 'ko', 'Magento_Customer/js/section-config', 'mage/url', 'mage/storage', 'jquery/jquery-storageapi' ], function ($, _, ko, sectionConfig, url) { 'use strict'; var options = {}, storage, storageInvalidation, invalidateCacheBySessionTimeOut, invalidateCacheByCloseCookieSession, dataProvider, buffer, customerData, deferred = $.Deferred(); url.setBaseUrl(window.BASE_URL); options.sectionLoadUrl = url.build('customer/section/load'); /** * @param {Object} invalidateOptions */ invalidateCacheBySessionTimeOut = function (invalidateOptions) { var date; if (new Date($.localStorage.get('mage-cache-timeout')) < new Date()) { storage.removeAll(); } date = new Date(Date.now() + parseInt(invalidateOptions.cookieLifeTime, 10) * 1000); $.localStorage.set('mage-cache-timeout', date); }; /** * Invalidate Cache By Close Cookie Session */ invalidateCacheByCloseCookieSession = function () { if (!$.cookieStorage.isSet('mage-cache-sessid')) { $.cookieStorage.set('mage-cache-sessid', true); storage.removeAll(); } }; dataProvider = { /** * @param {Object} sectionNames * @return {Object} */ getFromStorage: function (sectionNames) { var result = {}; _.each(sectionNames, function (sectionName) { result[sectionName] = storage.get(sectionName); }); return result; }, /** * @param {Object} sectionNames * @param {Boolean} forceNewSectionTimestamp * @return {*} */ getFromServer: function (sectionNames, forceNewSectionTimestamp) { var parameters; sectionNames = sectionConfig.filterClientSideSections(sectionNames); parameters = _.isArray(sectionNames) && sectionNames.indexOf('*') < 0 ? { sections: sectionNames.join(',') } : []; parameters['force_new_section_timestamp'] = forceNewSectionTimestamp; return $.getJSON(options.sectionLoadUrl, parameters).fail(function (jqXHR) { throw new Error(jqXHR); }); } }; /** * @param {Function} target * @param {String} sectionName * @return {*} */ ko.extenders.disposableCustomerData = function (target, sectionName) { var sectionDataIds, newSectionDataIds = {}; target.subscribe(function () { setTimeout(function () { storage.remove(sectionName); sectionDataIds = $.cookieStorage.get('section_data_ids') || {}; if (typeof sectionDataIds == 'string'){ sectionDataIds = sectionDataIds.replace('lax',''); sectionDataIds = JSON.parse(sectionDataIds); } _.each(sectionDataIds, function (data, name) { if (name != sectionName) { //eslint-disable-line eqeqeq newSectionDataIds[name] = data; } }); $.cookieStorage.set('section_data_ids', newSectionDataIds); }, 3000); }); return target; }; buffer = { data: {}, /** * @param {String} sectionName */ bind: function (sectionName) { this.data[sectionName] = ko.observable({}); }, /** * @param {String} sectionName * @return {Object} */ get: function (sectionName) { if (!this.data[sectionName]) { this.bind(sectionName); } return this.data[sectionName]; }, /** * @return {Array} */ keys: function () { return _.keys(this.data); }, /** * @param {String} sectionName * @param {Object} sectionData */ notify: function (sectionName, sectionData) { if (!this.data[sectionName]) { this.bind(sectionName); } this.data[sectionName](sectionData); }, /** * @param {Object} sections */ update: function (sections) { var sectionId = 0, sectionDataIds = $.cookieStorage.get('section_data_ids') || {}; if (typeof sectionDataIds == 'string'){ sectionDataIds = sectionDataIds.replace('lax',''); sectionDataIds = JSON.parse(sectionDataIds); } _.each(sections, function (sectionData, sectionName) { sectionId = sectionData['data_id']; sectionDataIds[sectionName] = sectionId; storage.set(sectionName, sectionData); storageInvalidation.remove(sectionName); buffer.notify(sectionName, sectionData); }); $.cookieStorage.set('section_data_ids', sectionDataIds); }, /** * @param {Object} sections */ remove: function (sections) { _.each(sections, function (sectionName) { storage.remove(sectionName); if (!sectionConfig.isClientSideSection(sectionName)) { storageInvalidation.set(sectionName, true); } }); } }; customerData = { /** * Customer data initialization */ init: function () { var expiredSectionNames = this.getExpiredSectionNames(); if (expiredSectionNames.length > 0) { _.each(dataProvider.getFromStorage(storage.keys()), function (sectionData, sectionName) { buffer.notify(sectionName, sectionData); }); this.reload(expiredSectionNames, false); } else { _.each(dataProvider.getFromStorage(storage.keys()), function (sectionData, sectionName) { buffer.notify(sectionName, sectionData); }); if (!_.isEmpty(storageInvalidation.keys())) { this.reload(storageInvalidation.keys(), false); } } if (!_.isEmpty($.cookieStorage.get('section_data_clean'))) { this.reload(sectionConfig.getSectionNames(), true); $.cookieStorage.set('section_data_clean', ''); } }, /** * 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; }, /** * Retrieve the list of sections that has expired since last page reload. * * Sections can expire due to lifetime constraints or due to inconsistent storage information * (validated by cookie data). * * @return {Array} */ getExpiredSectionNames: function () { var expiredSectionNames = [], cookieSectionTimestamps = $.cookieStorage.get('section_data_ids') || {}, sectionLifetime = options.expirableSectionLifetime * 60, currentTimestamp = Math.floor(Date.now() / 1000), sectionData; if (typeof sectionDataIds == 'string'){ sectionDataIds = sectionDataIds.replace('lax',''); sectionDataIds = JSON.parse(sectionDataIds); } // process sections that can expire due to lifetime constraints _.each(options.expirableSectionNames, function (sectionName) { sectionData = storage.get(sectionName); if (typeof sectionData === 'object' && sectionData['data_id'] + sectionLifetime <= currentTimestamp) { expiredSectionNames.push(sectionName); } }); // process sections that can expire due to storage information inconsistency _.each(cookieSectionTimestamps, function (cookieSectionTimestamp, sectionName) { sectionData = storage.get(sectionName); if (typeof sectionData === 'undefined' || typeof sectionData === 'object' && cookieSectionTimestamp != sectionData['data_id'] //eslint-disable-line ) { expiredSectionNames.push(sectionName); } }); //remove expired section names of previously installed/enable modules expiredSectionNames = _.intersection(expiredSectionNames, sectionConfig.getSectionNames()); return _.uniq(expiredSectionNames); }, /** * Check if some sections have to be reloaded. * * @deprecated Use getExpiredSectionNames instead. * * @return {Boolean} */ needReload: function () { var expiredSectionNames = this.getExpiredSectionNames(); return expiredSectionNames.length > 0; }, /** * Retrieve the list of expired keys. * * @deprecated Use getExpiredSectionNames instead. * * @return {Array} */ getExpiredKeys: function () { return this.getExpiredSectionNames(); }, /** * @param {String} sectionName * @return {*} */ get: function (sectionName) { return buffer.get(sectionName); }, /** * @param {String} sectionName * @param {Object} sectionData */ set: function (sectionName, sectionData) { var data = {}; data[sectionName] = sectionData; buffer.update(data); }, /** * Avoid using this function directly 'cause of possible performance drawbacks. * Each customer section reload brings new non-cached ajax request. * * @param {Array} sectionNames * @param {Boolean} forceNewSectionTimestamp * @return {*} */ reload: function (sectionNames, forceNewSectionTimestamp) { return dataProvider.getFromServer(sectionNames, forceNewSectionTimestamp).done(function (sections) { $(document).trigger('customer-data-reload', [sectionNames]); buffer.update(sections); }); }, /** * @param {Array} sectionNames */ invalidate: function (sectionNames) { var sectionDataIds, sectionsNamesForInvalidation; sectionsNamesForInvalidation = _.contains(sectionNames, '*') ? sectionConfig.getSectionNames() : sectionNames; $(document).trigger('customer-data-invalidate', [sectionsNamesForInvalidation]); buffer.remove(sectionsNamesForInvalidation); sectionDataIds = $.cookieStorage.get('section_data_ids') || {}; if (typeof sectionDataIds == 'string'){ sectionDataIds = sectionDataIds.replace('lax',''); sectionDataIds = JSON.parse(sectionDataIds); } // Invalidate section in cookie (increase version of section with 1000) _.each(sectionsNamesForInvalidation, function (sectionName) { if (!sectionConfig.isClientSideSection(sectionName)) { sectionDataIds[sectionName] += 1000; } }); $.cookieStorage.set('section_data_ids', sectionDataIds); }, /** * Checks if customer data is initialized. * * @returns {jQuery.Deferred} */ getInitCustomerData: function () { return deferred.promise(); }, /** * @param {Object} settings * @constructor */ 'Magento_Customer/js/customer-data': function (settings) { options = settings; customerData.initStorage(); invalidateCacheBySessionTimeOut(settings); invalidateCacheByCloseCookieSession(); customerData.init(); deferred.resolve(); } }; /** * Events listener */ $(document).on('ajaxComplete', function (event, xhr, settings) { var sections, redirects; if (settings.type.match(/post|put|delete/i)) { sections = sectionConfig.getAffectedSections(settings.url); if (sections) { customerData.invalidate(sections); redirects = ['redirect', 'backUrl']; if (_.isObject(xhr.responseJSON) && !_.isEmpty(_.pick(xhr.responseJSON, redirects))) { //eslint-disable-line return; } customerData.reload(sections, true); } } }); /** * Events listener */ $(document).on('submit', function (event) { var sections; if (event.target.method.match(/post|put|delete/i)) { sections = sectionConfig.getAffectedSections(event.target.action); if (sections) { customerData.invalidate(sections); } } }); return customerData; }); |
Now save and do static content deployment using command
1 |
php bin/magento s:s:d |
Working!! no more error. Cheers!!
What code we had updated is below where we for value of cookie is string or not as ideal value is json. Secondly, we replace the lax string with nothing and at last, we again convert string to json.
1 2 3 4 |
if (typeof sectionDataIds == 'string'){ sectionDataIds = sectionDataIds.replace('lax',''); sectionDataIds = JSON.parse(sectionDataIds); } |
If like efforts, Please share, comment and subscribe for future posts and inspire more.