Cache for video files
-
Hi,
If I understand correctly, this plugin takes advantage of the Cache Everything page rule. I have a “tube” site with self hosted videos.
Will this plugin cache all my .mp4 files and stream from Cloudfare CDN’s? If so, can this be disabled? I think caching mp4’s is against Cloudfare TOS under the free plan.
Additional info:
-All videos have the preload attribute set to metadata.
-Under network > Response, I get a 206 status code and cf-cache-status: HITPlease help. Thanks.
-
Will this plugin cache all my .mp4 files and stream from Cloudfare CDN’s?
– yes if you have proper cache control header or if you have enabled the option for adding cache control header for static files inside the plugin: https://i.imgur.com/Wz0EbnT.jpeg
If so, can this be disabled? I think caching mp4’s is against Cloudfare TOS under the free plan.
– Yes you are absolutely right. Caching MP4 is not against Cloudflare TOS, it’s caching videos for tube site is. So, lets say if you have a small MP4 video that you use as the page header background, or something like that, it’s fine. As it’s just a background video and won’t be more than 1-2MB. But your use case is against their TOS. That’s why Cloudflare has Cloudflare Stream for your use case: https://www.cloudflare.com/products/cloudflare-stream/
If you want to make sure that the MP4s are not gets cached by Cloudflare CDN then you have to make sure that your MP4 files has a
cache-controlheader set with the value ofno-cache.Under network > Response, I get a 206 status code and cf-cache-status: HIT
– Which means they are being served via the CDN. For videos 206 is normal as it is loaded in chuncks.
Thank you for your answer Saumya.
I have Add browsing caching rules for static assets enabled.
If you want to make sure that the MP4s are not gets cached by Cloudflare CDN then you have to make sure that your MP4 files has a cache-control header set with the value of no-cache.
How do I do this? I’m lost.
Check the browser cache rule added in your server, from the default browser cache rule, remove the mp4 extension and then for mp4 extension, add a new rule to set
cache-controlheader value asno-cache.I don’t really know what I’m doing. I’d like to check with you if I understood correctly.
Based on ur answer, I revised the cache rules that were created by the plugin on .htaccess.
There, under
<FilesMatch "\.(jpg|jpeg|png|gif|ico|eot|swf|svg|webp|avif|ttf|otf|woff|woff2|ogg|mp4|mpeg|avi|mkv|webm|mp3)$"> <IfModule mod_headers.c> Header set Cache-Control "public, must-revalidate, proxy-revalidate, immutable, max-age=31536000, stale-while-revalidate=86400, stale-if-error=604800" </IfModule>deleted the mp4 extension.
Then, above
# END WP Cloudflare Super Page CacheI added the following:<FilesMatch "\.(mp4)$"> <IfModule mod_headers.c> Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0" </IfModule> </FilesMatch>Did I get it right?
Also, in the code theres this message:
# The directives (lines) between "BEGIN WP Cloudflare Super Page Cache" and "END WP Cloudflare Super Page Cache" are # dynamically generated, and should only be modified via WordPress filters. # Any changes to the directives between these markers will be overwritten.I’m still lost.
Hi,
yes what you did should been perfect. But as you have not shared your website URL I couldn’t check it on my end. So, check the network tab in the browser developer tools and check if the mp4 files are not being served via Cloudflare CDN or not.I can confirm now the mp4s files give cf-cache-status: BYPASS.
Im Stil confused about the disclaimer in the code:
# The directives (lines) between "BEGIN WP Cloudflare Super Page Cache" and "END WP Cloudflare Super Page Cache" are # dynamically generated, and should only be modified via WordPress filters. # Any changes to the directives between these markers will be overwritten.Does this mean the changes will be overwritten eventually? Would I have to keep changing the cache rules periodically?
Also, since caching mp4 files is not forbidden by cloudfare (under the conditions you mentioned), could I add some code to just cache 1 mp4 file (1-2MB) and keep the rest on bypass?
<FilesMatch "\video.(mp4)$"> <IfModule mod_headers.c> Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0" </IfModule> </FilesMatch>This is most likely to be wrong but added for explanation purposes
I can confirm now the mp4s files give cf-cache-status: BYPASS.
– That’s good which means things are working as expected
Im Stil confused about the disclaimer in the code:
– Ignore that for now. Actually as these rules are dynamically generated by the plugin if you make any changes to the plugin settings the plugin might replace your modified htaccess code with the default code in which case you have to do the same thing you did now.
Also, since caching mp4 files is not forbidden by cloudfare (under the conditions you mentioned), could I add some code to just cache 1 mp4 file (1-2MB) and keep the rest on bypass?
– technically you can do that but for that you need to write a bit complicated htaccess rule to have one
cache-controlvalue for one specific mp4 file while others have a different value.Understood Saumya.
– technically you can do that but for that you need to write a bit complicated htaccess rule to have one cache-control value for one specific mp4 file while others have a different value.
Can you please help me with this rule?
Saumya,
I added the following code above
# END WP Cloudflare Super Page Cache# Added to bypass mp4 files from Cloudfare's Cache. <FilesMatch "\.(mp4)$"> <IfModule mod_headers.c> Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0" </IfModule> </FilesMatch> # Added to cache only header mp4 file <FilesMatch "header\.(mp4)$"> <IfModule mod_headers.c> Header set Cache-Control "public, must-revalidate, proxy-revalidate, immutable, max-age=31536000, stale-while-revalidate=86400, stale-if-error=604800" </IfModule> </FilesMatch>I can confirm all mp4 files give
cf-cache-status: BYPASSexcept the header video “header.mp4” which givescf-cache-status: HITDo you a see a problem/conflict with this solution?
Hi,
No. I don’t see any problem if it is working, feel free to use it.
The topic ‘Cache for video files’ is closed to new replies.