Sometimes the notification messages don't get disappear on page reload or when I navigate to the different page.
Is there some kind of timeout
for the notification message or some session cookies?
I followed this link
Magento 2 : Error/Success message won't remove from page after refresh once display
But where should I put this code so that _deleteMessage
call every time when the page reloads or user navigates to a different page.
This issue is more prominent in iPhone devices safari browser
.
This is my code in messages.js
this.cookieMessages = _.unique($.cookieStorage.get('mage-messages'), 'text')[_.unique($.cookieStorage.get('mage-messages'), 'text').length-1];
this.messages = customerData.get('messages').extend({
disposableCustomerData: 'messages'
});
if (!_.isEmpty(this.messages().messages)) {
customerData.set('messages', {});
}
customerData.set('messages', {messages: ""});
$.cookieStorage.set('mage-messages', '');
On further debugging, My code randomly breaks anywhere due to WebAPI socket
error on console js debugging.
So this WebAPI socket
error doesn't let this cookie
clear.
This issue is intermittent.
Thanks in advance
Best Answer
For anyone coming across this problem where notification messages persist across refreshed pages here are some tips that might help you resolve the problem.
If you look at
magento-theme/view/frontend/web/js/view/messages.js
you will see where these notification messages are read from themage-messages
cookie and passed to the backend for display.You will notice that the function cleans up the
mage-messages
cookieIf you add a console info log here, you can see that this executes with every page refresh. However, if you monitor the
mage-messages
cookie in the console you can sometimes see the cookie being set, deleted, and set again. This leads to the cookie persisting to the next page.Whether this has to do with delays in the backend often seen on slower servers i.e. development / development mode or it's a new 2.4 bug I am not sure.
One way to ensure that the cookie is reset after being processed is to add another cleanup function to the
prepareMessageForHtml
function. Note this function is only present in 2.4.xIn summary if you are seeing this message during development on 2.4.x try going to production mode with all caches enabled first. Debug further by adding console logs to
messages.js
.