5

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.

2 Answers 2

18

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']);

7
  • Is there an option to reload customer data in php? do you know? Commented Sep 24, 2018 at 15:34
  • @JulianoVargas Did you find any way to do that on the server side?
    – Pini
    Commented Oct 7, 2018 at 8:35
  • @Pini I haven't investigated it any further! Commented Oct 8, 2018 at 8:37
  • 1
    Yes! This - Thank you so much for providing a very exact example on how to run it from the console. Don't ever stop doing that :)
    – Fizk
    Commented Nov 12, 2018 at 12:44
  • @JulianoVargas if you need to invalidate the cache from backend you need to send private_content_version cookie with some random value. However, this way works only on POST requests initiated by the consumer. There is no way to invalidate private cache for many customers from admin panel. Commented Nov 13, 2018 at 13:09
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.