magento2: JS error in the customer data

Originally this issue was reported in https://github.com/magento/magento2/commit/5983e1733d56b1b2f15fb4f0e64094a3a4f3145d#commitcomment-44080850 by @HenKun.

The following commit https://github.com/magento/magento2/commit/5983e1733d56b1b2f15fb4f0e64094a3a4f3145d leads to similar issues described in https://github.com/magento/magento2/pull/24847.

Preconditions (*)

  1. Magento 2.4.1, or 2.4-develop

Steps to reproduce (*)

  1. Add the following code to the page on frontend:
<script>
    require(['Magento_Customer/js/customer-data'], function (customerData) {
        console.log(customerData.getExpiredSectionNames());
    });
</script>
  1. Go to the frontend, see the console

Expected result (*)

  1. In the console you should see an empty array, like this: image

Actual result (*)

  1. in the console we see the following error: image

Additional info from Engcom

Add the script to, for example, /app/code/Magento/Customer/view/frontend/templates/form/edit.phtml script

  1. In the Storefront, Sign In as a Customer
  2. Go to My Account
  3. Click Edit under Contact Information
  4. Observe the dev console

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 20 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@gkopec The fix for this issue was accidentally removed in magento 2.4.4 and 2.4.5. I’ve created a pull request for that: https://github.com/magento/magento2/pull/36177

You can use this patch for 2.4.5: https://github.com/magento/magento2/commit/847c7997143d3c90b26f94480a9fe202f499faf7.diff

Hi @ihor-sviziev. Thank you for your report. The issue has been fixed in #31940 by @ihor-sviziev in 2.4-develop branch Related commit(s):

The fix will be available with the upcoming 2.4.3 release.

@gabrieldagama The fix didn’t merge into 2.4.3 yet, so it does not available in 2.4.3

After this was fixed, the fix was removed by this commit: https://github.com/magento/magento2/commit/405ee6ebdfb9a3df7455153eb11af2f91fdba562

So in version 2.4.5 the bug is back again… 🤦🏻‍♂️

up, 2.4.x-develop seems to still produce the error (tested with firefox).

@gkopec, I created this from the PR:

index 213aa105b..6bff87d67 100644
--- a/vendor/magento/module-customer/view/frontend/web/js/customer-data.js
+++ b/vendor/magento/module-customer/view/frontend/web/js/customer-data.js
@@ -17,7 +17,9 @@ define([
 ], function ($, _, ko, sectionConfig, url) {
     'use strict';
 
-    var options = {},
+    var options = {
+            cookieLifeTime: 86400 //1 day by default
+        },
         storage,
         storageInvalidation,
         invalidateCacheBySessionTimeOut,
@@ -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
      */
@@ -217,14 +235,7 @@ define([
         /**
          * Storage init
          */
-        initStorage: function () {
-            $.cookieStorage.setConf({
-                path: '/',
-                expires: new Date(Date.now() + parseInt(options.cookieLifeTime, 10) * 1000)
-            });
-            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.
@@ -389,7 +400,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();

@ihor-sviziev yep I see it.

Thanks