Does filemtime(custom.css) in functions.php return timestamp if OPcache is ON ?
-
Question 1: Is there a mechanism which reliably invalidates the OPcache if the corresponding PHP file gets updated?
- On my webhost I have PHP OPcache ON with a timeout of 300 seconds.
- When I perform changes in the
functions.php
of my block theme (a modified TT3 theme) and save the file (via SSH or SFTP), these do not reflect. - For now my workaround is that during performing changes in functions.php I destroy the current OPcache and disable it henceforth. Then my changes in
functions.php
always are reflected when reloading. After that session I re-enable it for performance gains.
Question 2: If you have OPcache ON and in
wp_enqueue_styles()
you want to set the version tag to be the return value offilemtime(/path/to/custom.css)
are CSS file changes noticed/refreshed for all new requests thereafter if the PHP file performing those filesystem calls itself is OPcached? Or isfilemtime()
run once and then the result of this is kept for the next 300secs? And at earliestfilemtime()
runs on the next request after that 300secs timeout?- I use W3 Total Cache for Page Caching (HTML) and also have set up cache busting for my asset files (CSS, JS, fonts). Meaning for them max-age is set to half a year without any re-validation. So only when the version tag changes the browser re-fetches them.
- I use
wp_enqueue_styles()
to enqueue my own global.css file and I see basically 3 possibilities:
<?php function mytheme_enqueue_styles(){ // 1) No verstion tag at all wp_enqueue_style( 'extra-style', get_stylesheet_directory_uri() . '/assets/css/global.css'); // 2) Manually: Set version tag myself to a version number or date whenever I have done meaningful changes to global.css which all users shall get. wp_enqueue_style( 'extra-style', get_stylesheet_directory_uri() . '/assets/css/global.css', NULL, "2023-06-08--204400"); // 3) Automated: Set the version tag from global.css the file modification date wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/assets/css/global.css', date( DATE_ATOM, filemtime(get_theme_file_path() . '/assets/css/global.css'))); } add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_styles' ); >
During development I use variant °1 and in the web browser simply perform a hard-reload to enforce getting the up to date assets.
Now for production: Can I rely upon °3 automated tag from CSS timestamp or must I set °2 a manual version tag in functions.php each time something significant changes in my CSS file?
- The topic ‘Does filemtime(custom.css) in functions.php return timestamp if OPcache is ON ?’ is closed to new replies.