Problem with Cloudflare and Autoptimize

Hi everyone,

I’m currently working with Cloudafre, Autoptimize, and WP Rocket. I’m having some trouble on an actual page that is not displaying properly the page (https://lesdeuxpiedsdehors.com/asie/). A week or two ago, it was an other page.

If I examine the page, i do see i have a 404 on a Autoptimize script and i guess this is related. I can’t figure out where the problem is. I already know that if I decide to purge the CF cache of this page, it will be working fine again (I’ve tested it last time I had this trouble).

I’m not geeky enough to understand this and i’m looking for help or people who can have a look at the page. If someone have an idea, that would be great. I don’t know if the problem is from Cloudfare, Autoptimize or my set-up ?

Attached the 404,
Thanks,
Sylvain

That’s the danger of layered caching. Especially when you’re using Cache Everything in a Page Rule, as it appears you’re doing.

The caches have gone out of sync. Your Page HTML, which you’ve cached on Cloudflare has that autoptimize js hardcoded into it. But behind the scenes, Autoptimize has regenerated that js and given is a new serial number, then deleted the old one.

Now your cached main page has an outdated reference. You’ll have to turn off that Cache Everything Page Rule to stop that from happening.

Hi,

Yes that’s exactly what i found too. Basically, the html page is cache on CF with the old autoptimize (AO) js and AO is behind the scene regenerating a new js so when the html is calling this file, it isn’t here.

What i don’t understand is it was working (i guess so) before. I’m not sure what changed ?
Is there a possibility that i can force the CF cache to be purged every time AO is regenerating a new css/js ?

I find it strange that it’s happening to you, but not me. In my case, I use Astra theme, which generates CSS in a similar way, and I had the same caching issue (Astra kept regenerating css and js), so I started using Autoptimize and that fixed the problem. I wish I new why Astra (and now AO) regenerates CSS and JS if nothing on the site has changed. I also wish they would let me turn off their cache-busting scheme.

Yes i don’t understand why. Maybe this is theme related i don’t know. The plugin developer is aware of this, i found this : https://wordpress.org/support/topic/autoptimize-2-7-coming-any-testers-here/

I have open a new ticket also here : https://wordpress.org/support/topic/404-with-autoptimze-and-cloudfare/
Maybe i’ll get some answers…

1 Like

tried setting css files cache everything rule to have a longer edge cache ttl than your wordpress dynamic php HTML pages edge cache ttl ?

Thanks for your answer. I’m not sure to understand, sorry. Could you elaborate please ?

For my Wordpress blog with Autoptimize with cache everything rules i have dynamic php pages with edge cache ttl = 1 day and css/js with edge cache ttl = 30 days so dynamic php generated HTML pages will always expire cf edge cache before css/js files will

Of course this depends on what and why your Autoptimize is regenerating the css/js concatenated files. It could be other wordpress caching plugins hooked into Autoptimize which when you purge those wordpress cache plugins, it also purges Autoptimize plugin cache too - forcing a regeneration of Autoptimize plugin’s cache and generated css/js files. I know Cache Enabler wordpress plugin does this if Autoptimize plugin is installed and detected too.

wp rocket https://docs.wp-rocket.me/article/983-can-i-use-autoptimize-with-wp-rocket

1 Like

from https://en-au.wordpress.org/plugins/autoptimize/

Can I disable AO listening to page cache purges?

As from AO 2.4 AO “listens” to page cache purges to clear its own cache. You can disable this behaviour with this filter;

add_filter('autoptimize_filter_main_hookpagecachepurge','__return_false');
1 Like

Thanks. I have no idea and why autoptimize is regenerating this file. I’m not sure why the AO cache is actually purging and how/where I can check this. Right now, I’m using WP rocket for page caching as well as Autoptimize.

I mainly use Autoptimize to optimize JS and CSS. The problem is, from time to time, I have tome 404 on some random page because my Html page on CF is calling a file that isn’t here anymore because it was regenerated by the purge of Autoptimize cache (if I understand correctly).

The question are:

  • Is this normal that autoptimize plugin is purging itself?
  • Would WP Rocket be responsible for this?

I’m not sure what to do anymore as this is random of course. I can’t always be sure, there’s a 404 from autoptimize somewhere and it’s only on 1/2 pages at the time

Knowing my luck, the HTML edge cache will expire and refresh five minutes before Autoptimize regenerates. Thanks for the Autoptimize filter suggestion. I’m looking forward to 2.7 that don’t break when it updates.

When does WP Rocket clear the cache?

The most common triggers for full or partial cache deletion are:

  • Cache Lifespan:
    On the Cache settings tab you can specify a Cache Lifespan. Only cache files older than the specified lifespan will be deleted. A cron job will run every hour to check which files should be deleted.
  • New content:
    When you publish new content or update/edit existing content, the cache for that post/page as well as related content (e.g. taxonomy archives) will be cleared and (if your settings allow) preloaded.

So you want to purge CF cache when new post/updates are made and/or set WP Rocket cache lifetime longer than CF cache everything edge cache ttl

I just checked and right now it’s set-up to 10h. Every 10h, the WP rocket cache is purged.

I’m sorry, but i don’t know how and where to add this filter for now ?

A filter like that would go at the end of your theme’s functions.php file. You just have to remember to re-add it whenever you update your theme.

1 Like

Thanks, i will see how it goes then.

yes you add it to your functions.php file or you can create your own wordpress plugin for it. I did that to enable Autoptimize’s Gzip support which precompresses optimised css/js files with gzip and/or brotli compression for >3x performance https://github.com/centminmod/autoptimize-gzip

you can see the code I used at https://github.com/centminmod/autoptimize-gzip/blob/master/autoptimize-gzip.php. It would be similar for autoptimize_filter_main_hookpagecachepurge filter I suspect.

But best to ask Autoptimize questions on their own support forums like you did for https://wordpress.org/support/topic/404-with-autoptimze-and-cloudfare/

1 Like

Thanks for the follow-up. I have no idea why but it seems that Autoptimize is listening to WP Rocket flushing the cache and clears its own in the same time. I tested it and it’s seems to be true as when i purges WP Rocket Cache, Autoptimize was cleared too. No idea though why.

Anyway, this was of course causing the problem in my humble opinion. So for now, i’ve installed WP Rocket no-cache add-on. This was, i think Autoptimize who purged itself. Maybe that’s a solution.

My only concern is about the “Autoptimize cache”. Won’t it grow bigger and bigger at time ? And if i understand, if i decided to manually purge the Autoptimze cache, i will also have to purge the couldfare cache. Otherwise, i will have the same problem again and again ?

PS : Yes i have asked the question directly at their support. Hope to get an answer :slight_smile:

You should perhaps consider installing Cloudflare’s own plugin, and enable Automatic Cache Management, which will purge any page cache from its datacenters any time you edit or make changes to a page, and purge all pages when there’s a theme update or some other changes that affect all pages. It will also purge any Autoptimize cache related to those pages.

By calling the following hooks, the plugin will purge specific cache URLs:

  • deleted_post
  • edit_post
  • delete_attachment
  • autoptimize_action_cachepurged // for compatibility with the Autoptimize WordPress plugin
2 Likes

Hi there,

Thanks for your informations.Fo the follow up for everyone :

  • I’m curently testing the filter from the Autoptimize doc to force AO to not listen the WP Rocket purge cache, and it’s seems to be working pretty well.
  • About the possibility to enable automatic cache management, i did’t know it was possible. I will have a look at it then. I’m also thinking about testing this plugin which is doing the same thing (and more i guess) : https://wordpress.org/plugins/wp-cloudflare-page-cache/
  • I’m not sure what are the main difference of these two options though?

For now, i have to figure out the interest of keeping WP Rocket with Autoptimize as all my optimisation are done in AO and i’m only using WP rocket for page caching as back-up when the CF cache is purged. but if i found a solution that i will be able to only purge specific URL’s automatically (WP cloudfare page cache or enable automatic cache management), i don’t see any interest of keeping WP Rocket, don’t you ?

Thanks,
Sylvain