
內容簡介
這是一個基於檔案名稱的快取清除外掛程式,使用最後修改日期來管理 WordPress scripts/styles。
不要再讓客戶或使用者硬重整或者按下 shift + 重整了。只要檔案有修改,你網站上所有 css/js 檔案的 URL 就會自動更改。你可以設定適當的長期快取存活時間,以協助你在 Google Pagespeed 上獲得最高分數,同時避免快取後的 css/javascript 問題。
基於 Dominik Schilling 的這項 gist https://gist.github.com/ocean90/1966227 ,我對它進行了增強,即可自動以檔案修改時間來替換資產版號,並自動編輯 htaccess - 安裝和忘記。
如果有更多情況,也可以選擇使用查詢字串選項作為後備。
Web 伺服器
Apache
這個外掛會自動把所需的添加到你的 .htaccess 檔案,就像 WordPress 本身一樣。但如果由於任何原因它無法運作,以下是它添加的內容:
# FBACB
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.([0-9\.]+)\.(js|css)$ $1.$3 [L]
</IfModule>
# still fbacb
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
# END FBACB
NGINX
location ~* ^(.+)\.(?:\d+)\.(min.js|min.css|js|css)($|\?.*$) {
try_files $uri $1.$2;
}
特定主機注意事項
某些網站主機可能需要進行一些客製化設定,才能使基於檔案名稱的 URL 運作正常。
WP Engine
要在 WP-Engine 上運行,您需要透過 my.wpengine -> installname -> 重新導向規則,新增以下導向:
Source: ^(.+)\.([0-9\.]+)\.(js|css)$
Dest: $1.$3
Type: break (this is under advanced)
CloudWays
可以直接使用。
Flywheel
也可以直接使用。
PHP Fallback
如果您的網頁伺服器無法使用 URL 重新導向提供資產,該外掛程式會嘗試使用 PHP 程式來提供它們(例如:透過 PHP 本身)。顯然,這樣在實際生產情況下會很慢/不理想(速度會慢很多),因此您應該將伺服器設定為適當地提供檔案。如果在您的資產中看到此 HTTP 標頭“FBACB-Php-Fallback:yes”,這樣的情況就適用於您。
請注意,網站主機越來越多地被設定為從磁碟直接提供 css/js,並且不會回到 PHP + WordPress 錯誤處理 – 因此此後備將無法運作。
外掛標籤
開發者團隊
② 後台搜尋「Filename based asset cache busting」→ 直接安裝(推薦)
📦 歷史版本下載
原文外掛簡介
Filename based cache busting for WordPress scripts/styles using last modified date.
Stop telling clients or users to hard refresh / shift refresh. The url to all of the css/js files on your site will change automatically whenever the files are modified. You can set proper long cache lifetimes to help get top scores on Google Pagespeed without running into cached css/javascript problems.
Based on this gist https://gist.github.com/ocean90/1966227 from Dominik Schilling, I’ve enhanced it by automatically replacing the asset version with the files modification time and automatically editing htaccess – making it install + forget.
Includes querystring option as a fallback – which works in more scenarios.
Webservers
Apache
The plugin should add what it needs to your .htaccess file jsut like WP itself. But if for any reason that doesn’t work for you – here’s what it adds:
# FBACB
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.([0-9\.]+)\.(js|css)$ $1.$3 [L]
# still fbacb
ExpiresActive on
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
# END FBACB
NGINX
location ~* ^(.+)\.(?:\d+)\.(min.js|min.css|js|css)($|\?.*$) {
try_files $uri $1.$2;
}
Host specific notes
Some webhosts need a bit of custom config to get the filename based urls working.
WP Engine
To work on WP-Engine, you’ll need the following redirect added via my.wpengine -> installname -> Redirect rules
Source: ^(.+)\.([0-9\.]+)\.(js|css)$
Dest: $1.$3
Type: break (this is under advanced)
CloudWays
Just works.
Flywheel
Just works.
PHP Fallback
If your webserver is failing to serve assets using url rewriting, the plugin attempts to serve them itsef (ie: via PHP). Obviously this is slow/less than ideal in production (it’ll be much slower) – so you should configure your web server correctly to serve the files. If you see this HTTP header “FBACB-Php-Fallback: yes” on your assets, this applies to you.
Note that hosts are increasingly configured to serve css/js directly from disk and won’t fall back to PHP + WordPress error handling – this fallback then won’t work.
