• Resolved erikdemarco

    (@erikdemarco)


    Try this on woocommerce using CF worker version:
    -) Purge all cache
    -) Open any page as logged out user (after couple try the cache status will become ‘HIT’)
    -) Now add any product to cart
    -) Open any page (none of them will have cache status ‘HIT’)

    I tested above using clean woocommerce install without any plugin.

    This works flawlessly using default version (cache everything). Isn’t worker version should be more compatible with other plugin?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor iSaumya

    (@isaumya)

    I don’t see any issues there. When you add any product to a cart, based on how your shop has been set WooCommerce will add a cookie to your browser to track the cart. As soon as the Cookie is present Cloudflare will not provide you a cached response as now you have items in cart which can get updated/modified any time so cached version won’t work.

    Thread Starter erikdemarco

    (@erikdemarco)

    @isaumya Next time. Please please please try before you reply. ??????

    I will explain why you are wrong.

    CF will not will not provide you a cached response ‘IF’ there is a cookie in the ‘RESPONSE’ (IF your server response send ‘set-cookie’ in the header). ELI5: So it means When cookie exist in request, cloudflare will still respond you with cache version. Thats how all cache on earth works including nginx (CF use nginx as the backend)
    source: https://support.cloudflare.com/hc/en-us/articles/200172516-Understanding-Cloudflare-s-CDN

    “Cloudflare does not cache the resource if the Cache-Control header is set to private, no-store, no-cache, or max-age=0 or if there is a cookie in the response” (ELI5: CF will still cache if request header contain cookie, do you know the difference between request and response header?)
    source: https://developers.cloudflare.com/cache/about/default-cache-behavior

    Thats explain why using ‘default’ version work. But using ‘worker’ version doesnt work.

    Have you try add to cart (when logged out) using default method instead using ‘worker’? It still send cached version. Please please please try it before reply.

    After that you will understand this question. “So why it works on default version but not works on ‘worker’ version?”

    Plugin Contributor iSaumya

    (@isaumya)

    I did read your message properly and tried before replying. I’ve written the worker code myself so I know what it does and how it does it. Take a look at the loom video I recorded for you: https://www.loom.com/share/1cb30d5386bc46a0836b1f3a5a63c759
    This will give you better understanding.

    You are basically comparing a shortcoming of cache everything page rule with the benefit of worker as the worker code doesn’t have that shortcoming.

    Thread Starter erikdemarco

    (@erikdemarco)

    @isaumya Thank you so so much for making the video to explain. So from what I get you mean, Worker version cant work when user add to cart? But default version will work even with add to cart?

    Please also see my video explaination:

    with default setting:
    https://loom.com/share/402301b563834dba8e6665ecaaf80d04

    with worker:
    https://loom.com/share/dde2d68c35e243738604cbc637afba98

    Plugin Contributor iSaumya

    (@isaumya)

    Hi @erikdemarco,
    I think you still did understand the nuances clearly. Let me explain in more detail.

    Page Rule Mode
    In the page rule mode the way CF works is that it evaluates each URL separately and doesn’t understand the full picture of the website. So, if you visit https://example.com/shop/ and at that time if the page is not cached in CF CDN, CF will check if the page response headers has any saet-cookie in it. If there is any cookie on that request, CF won’t cache that page.

    Now lets say for the first request it was DYNAMIC and at that time CF didn’t saw any cookies in the response header. So, CF will go ahead and cache that request. Now when you click on Add to Cart to any product present in the shop page, it will be added to cart and then when you refresh the page will again be served from CF cache.

    This is because when you clicked on Add to Cart and on that request a few cookies are set, CF won’t distinguish it. Instead it will see that this request is already cached in my server to serve it from cache.

    Now in case of the example you are running you might not see an problem. But lets imagine that on your top nav you had a Cart Button showing the total number of items in the cart r the items added in the cart.

    In this case you will see that no matter how many times you add products to cart, that cart button will stay exactly as it was in the beginning as if no items has been added. But when you click on the Cart button and go to the cart page (which is bypassed from CF cache) you will see the items perfectly there.

    This is because despite you adding the items to the cart, as you are using the page rule mode and the initial request is cached in CF, it will keep providing you the cached page instead of bypassing the cache as now you have items added in cart so some cookies are set and PHP needs to be executed to dynamically show the items you add/remove frm the cart.

    When a page is served from CF CDN, no PHP gets executed, instead CF will provide the cached HTML it received the first time when it cached the page.

    In Worker Mode
    Now when you are using the worker mode, as soon as some cookies are set for which pages needs to be bypassed from caching (e.g. WooCommerce Cookies), pages will be bypassed and won’t served from CF cache.

    This is because when you are using the worker mode, when CF receive a request, it first check the worker logic to see if it needs to provide cached page or bypass that. Whereas when you are using page rule mode, there is no such check. If a request is cached by CF, it will be served by CF CDN.

    So, which mode is best?
    Now the question that might come to your mind is which mode to choose then? Well, as a thumb rule the page rule mode will work on 99% of the sites. Where you do not have complicated shop and cookie logics.

    But if you have a eCommerce shop with header cart icon and things like that where if a user add something to the cart and then go to some other page, the cart button will keep showing the items added, in situations like these the worker mode will work best.

    I hope this answers your question and clarifies your doubts.

    Thread Starter erikdemarco

    (@erikdemarco)

    Now in case of the example you are running you might not see an problem.

    This is the answer I was looking for. I just asking “Why add-to-cart will still cache page in page rule mode. But not in worker mode?” but you reply with “I don’t see any issues there.”

    In this case you will see that no matter how many times you add products to cart, that cart button will stay exactly as it was in the beginning as if no items has been added.

    Again? Please please next time try it before you make any statement. I never see any problem with cart count on the header. Woocommerce already smart enough. It will show ‘0’ for cart count on first page load. The real count will be updated using ajax (ajax result will not gets cached)
    See this line in woocommerce source code:
    https://github.com/woocommerce/woocommerce/blob/b19500728b4b292562afb65eb3a0c0f50d5859de/includes/widgets/class-wc-widget-cart.php#L71

    I tested this with storefront, astra, oceanwp, shoptimizer. All dont have this problem.

    Cart count problem may exist if the theme developer doesnt use woocommerce’s own widget code. Instead they make their own code and didnt think about cache.

    Anyhow I just gave 5 star review for your plugin. Thank you so much

    Plugin Contributor iSaumya

    (@isaumya)

    Cart count problem may exist if the theme developer doesnt use woocommerce’s mini cart code. Instead they make their own code and didnt think about cache.

    – Yes you are right and true me most of the theme devs out there don’t follow any guidelines.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Cache seems not working perfectly on CF Worker’ is closed to new replies.