How to force reloading of a customer data section?
-
25-12-2020 - |
Question
I'm using CustomerData library to display private content (Magento docs).
I see that in etc/frontend/sections.xml
I can configure which requests would result in requesting the data from the server and storing in browser's local storage.
Is there a way to specifically ask to invalidate the data (the particular section)? For example, I want to request it if a data property is absent or if a data property had been received more than 1 hour ago.
Do I make a dedicated dummy action that does nothing just so I could add it to etc/frontend/sections.xml
and a request by AJAX when I need to invalidate the section cache, or is there some better way?
If this is the solution, then why should I bother with the customer data at all? I could just make that AJAX call return the data I need and store it the local storage myself.
Solution
There is no easy way to invalidate data from backend.
On frontend you can use
app/code/Magento/Customer/view/frontend/web/js/customer-data.js
library to invalidate the local storage. Just call customerData.reload(['section_name']);
In crhome console it will look like: require('Magento_Customer/js/customer-data').reload(['customer']);
OTHER TIPS
As far as I know, You can do this by reloading on the front end side by calling the customerData from:
Magento_Customer/js/customer-data.js
and do something like this:
let sections = ['name_section'];
customerData.invalidate(sections);
customerData.reload(sections, true);