Magento2: Session messages not removed properly - persist across multiple requests

Created on 30 Jun 2017  路  8Comments  路  Source: magento/magento2

Preconditions

Magento CE 2.1.5 (experienced in 2.1.7 as well) with sample data in Google Chrome

Steps to reproduce

  1. Customer opens a product page
  2. Customer sets quantity to a high number
  3. Customer clicks "Add to cart"
  4. Page refreshes, and a message is displayed "We don't have as many ... as you requested"
  5. Customer refreshes page again, or navigates to another page (category, product, homepage)

Expected result

Message _"We don't have as many ... as you requested"_ should not be displayed on the next request

Actual result

Message _"We don't have as many ... as you requested"_ is displayed on the next request

Workflow that causes this issue:

  1. POST request to checkout/cart/add is made
  2. since there is not enough of the item in the stock, backend logic performs a redirect back to product page (vendor/magento/module-checkout/Controller/Cart/Add.php:149), and a cookie "mage-messages" is set with message: _"We don't have as many ... as you requested"_
  3. redirect performs a GET request to the same URL (product page)
  4. response is received, and message is displayed to a customer
  5. XHR GET request is performed to "review/product/listAjax/id/[some_id]" and its request headers contain "mage-messages" cookie, with message in it
  6. vendor/magento/module-theme/view/frontend/web/js/view/messages.js script is triggered on frontend, and "mage-messages" cookie is cleared
  7. XHR response is received (from step 6.), and due to backend logic in vendor/magento/module-theme/Controller/Result/MessagePlugin.php:82, "mage-messages" cookie value is returned as received which causes "mage-messages" cookie to revert back to the state where it contains a message.
  8. Upon refresh or navigating to a new page, same message is displayed again

So basically we have a race condition where cookie is set (with message), then it is removed via messages.js script, and then again set by returning XHR response that was sent before cookie was removed.

Workaround:

copy vendor/magento/module-theme/view/frontend/web/js/view/messages.js to your theme file, and at the end of initialize function add:

$(window).on('beforeunload', function(){
    $.cookieStorage.set('mage-messages', '');
});
Catalog Cannot Reproduce Clear Description Format is valid bug report

All 8 comments

Same behavior here (message won't go away), also with success messages on default M2 2.1.7 with sample data, tested on Chrome and Firefox.

You click "Add to Cart" on product page, submit (not ajax on default 2.1.7), you end up back on product page + success message. When you navigate away from product page, or refresh product page, you'll get same message again. That is, if messages.js is executed before review ajax comes back as @epson121 explained, which happens most of the time.

For the same reason, sometimes if you click Add to Cart multiple times, success messages are stacking :)

screenshot at 2017-06-30 12-29-03

HI @epson121. I cannot reproduce this issue. Please add more details to your description of the steps you followed when identifying this issue. Screenshots, video or logs would be helpful, too.
My video to reproduce https://youtu.be/OH35yqneD50

Hi all, I am also facing the same issue in Magento 2.1.7. Here is the video link for reproducing this issue. https://www.useloom.com/share/0c7a9812101c44a3b61f259fb0a3f39c

Hi, @sabarivenkatesankrish. Unfortunately, I could not reproduce the issue as you described it.

Hi, @1408sheva. I just installed Magento 2.1.7 package from repo.magento.com through the composer. I have also installed sample data. Apart from this, I did nothing. I have disabled the cache for ensuring the cache issue. I changed the session storage to "db" instead "files". But Still this exist. As of my knowledge it might not be a cache related issue. It could be a session related issue. I am not sure that the problem with the package. It may be from my end. But I can't able find. Any suggestion would be appreciated.

@epson121, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.

I have resolve isssue from below referance link:
https://community.magento.com/t5/Technical-Issues/Error-Success-message-not-showing-on-one-page-in-custom-module/td-p/72823

I was using

<script type="text/x-magento-init">

for the javascript validation.
I changed it to

`<script type="text/javascript">`

Need to flush and enable cache.

Was this page helpful?
0 / 5 - 0 ratings