Try This
Method-1 Using Module
app/code/VendoreName/ModuleName/view/frontend
requirejs-config.js
var config = {
map: {
'*': {
'Magento_Theme/js/view/messages':'VendoreName_ModuleName/js/view/messages'
}
}
};
app/code/VendoreName/ModuleName/view/frontend/web/js
messages.js
define([
'jquery',
'uiComponent',
'Magento_Customer/js/customer-data',
'underscore',
'escaper',
'jquery/jquery-storageapi'
], function ($, Component, customerData, _, escaper) {
'use strict';
return Component.extend({
defaults: {
cookieMessages: [],
messages: [],
allowedTags: ['div', 'span', 'b', 'strong', 'i', 'em', 'u', 'a']
},
/**
* Extends Component object by storage observable messages.
*/
initialize: function () {
this._super();
this.cookieMessages = _.unique($.cookieStorage.get('mage-messages'), 'text');
this.messages = customerData.get('messages').extend({
disposableCustomerData: 'messages'
});
// Force to clean obsolete messages
if (!_.isEmpty(this.messages().messages)) {
customerData.set('messages', {});
}
$.cookieStorage.setConf({path: '/', expires: -1}).set('mage-messages', null);
},
/**
* Prepare the given message to be rendered as HTML
*
* @param {String} message
* @return {String}
*/
prepareMessageForHtml: function (message) {
return escaper.escapeHtml(message, this.allowedTags);
}
});
});
Method-2 Using Theme
app/design/frontend/your-vendor-name/your-theme-name/Magento_Theme/web/js/view
messages.js
define([
'jquery',
'uiComponent',
'Magento_Customer/js/customer-data',
'underscore',
'escaper',
'jquery/jquery-storageapi'
], function ($, Component, customerData, _, escaper) {
'use strict';
return Component.extend({
defaults: {
cookieMessages: [],
messages: [],
allowedTags: ['div', 'span', 'b', 'strong', 'i', 'em', 'u', 'a']
},
/**
* Extends Component object by storage observable messages.
*/
initialize: function () {
this._super();
this.cookieMessages = _.unique($.cookieStorage.get('mage-messages'), 'text');
this.messages = customerData.get('messages').extend({
disposableCustomerData: 'messages'
});
// Force to clean obsolete messages
if (!_.isEmpty(this.messages().messages)) {
customerData.set('messages', {});
}
$.cookieStorage.setConf({path: '/', expires: -1}).set('mage-messages', null);
},
/**
* Prepare the given message to be rendered as HTML
*
* @param {String} message
* @return {String}
*/
prepareMessageForHtml: function (message) {
return escaper.escapeHtml(message, this.allowedTags);
}
});
});
Must be run Magento 2 commands after adding the above files
php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento s:d:c
php bin/magento c:c