Score:1

Cloudflare cache everything

ch flag
STF

we have a Drupal 8 with Cloudflare cache rule. By default we have always GBP currency, then we developed a module that allow users change their currency with a footer dropdown and saves it on the user session.

Without Cloudflare cache rule, the webpage works correctly. The problem is if the rule is active, the first visit to the web is cached with the default currency, then if someone try to change the currency to EUR, the web page is reloaded and it shows the cached page with default currency and not EUR.

Cloudflare cache is by URL and we always keep the same URL for all currencies.




Start Clodflare screeshots

Cloudflare Cache rule Cloudflare Cache level

End Clodflare screeshots




A solution that we thought is to add/force the currency as a parameter of the URL.

https://www.amazon.com/ 
https://www.amazon.com/?currency=EUR&language=es_US 
https://www.amazon.com/?currency=EUR&language=en_US 
https://www.amazon.com/?currency=USD&language=es_US

Is possible to force a request to have always a param? Is there any way to do this? Like an EventSubscriber?

Thank u all.

Score:1
cn flag

Yes, you can force the request to have always query parameters by redirecting in an event subscriber. The problem is, how do you know what currency the user is requesting if the parameter is missing? So what you probably want is to process outbound paths to have already the correct parameters.

BTW your question is a bit confusing. The first part is talking about logged-in users. Then the solution is not query parameters but to configure the CDN to bypass cache on SESS.* cookies (which you need anyway to keep the user logged in). See https://support.cloudflare.com/hc/en-us/articles/115002911927-Caching-HTML-with-Drupal

STF avatar
ch flag
STF
Hi @4k4, thx for u quick response. There is always a session cookie and this is needed for guest and logged users. We tried to configure CDN with session cookie (and also custom cookies), but we can't use this solution. That's the question, if a request doesn't has the currency parameter, could I force to add it if it exists in session?
STF avatar
ch flag
STF
If I try to reach the site www.example.com and if I have EUR in session but the parameter don't exist, could I force to change this request to www.example.com?currency=EUR
4uk4 avatar
cn flag
You can get the session in an event subscriber `$event->getRequest()->getSession()` and redirect if the query parameter is missing. See https://www.drupal.org/node/2013014. But I'm not sure this works when you have configured the CDN to cache traffic with session cookies. And you still might want to add the query parameters to outbound links, especially those of the currency dropdown.
Score:0
ch flag
STF

Cloudflare has an option that is Custom Cache Key. Here I can configure cache by cookie name and/or cookie name and value.

We save the currency in a cookie and Cloudflare is able to detect that cookie name with the specified currency.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.