內容簡介
此外掛會自動將使用wp_enqueue_script 及 wp_enqueue_style 加入的所有腳本和樣式表合併和縮小為邏輯組。例如,頁面底部的腳本會被分組,同樣媒體(例如print)的樣式表會一起縮小。縮小是通過WP-Cron完成的,以避免競態條件並確保縮小過程不會減慢頁面響應速度。
這是Optimize Scripts插件的重新設計和重寫,現在取代了該插件。
特點:
縮小來源存儲在WP選項表中,因此不需要特殊的文件系統訪問。
縮小請求的端點為/_minified,可以進行配置。
管理頁面用於盤點縮小的腳本和樣式表,提供過期或清除緩存數據的方法。
必須排除縮小的依賴關係可能會通過dependency_minification_excluded過濾器排除。
預設情況下,托管在其他域上的相依關係會被排除,但可以通過過濾器dependency_minification_options過濾default_exclude_remote_dependencies選項,或通過前面提到的過濾器根據情況進行更改。
預設情況下,外部腳本不會被合併和縮小,但可以通過dependency_minification_excluded過濾器設定。
縮小源的緩存時間默認為1個月,但可以通過cache_control_max_age_cache選項進行配置。
如果縮小源尚不可用,頁面源將註明等待依賴關係縮小過程。
任何在縮小過程中發生的錯誤都會在前端以注釋的形式顯示,如果已啟用show_error_messages選項,則此類錯誤默認情況下已啟用WP_DEBUG。
如果縮小過程出錯,原始未縮小的來源將被提供並且該錯誤將被緩存1小時(默認情況下,通過cache_control_max_age_error進行配置),以防止連續的cron的回溯不斷地嘗試進行縮小。
緩存的縮小源使用Last-Modified和ETag響應標頭提供,請求將憑藉If-None-Match和If-Modified-Since來返回304 Not Modified的響應(可通過選項allow_not_modified_responses進行配置)。
附加到腳本的數據(例如通過wp_localize_script附加到腳本)也會一起合併並附加到新縮小的腳本上。
使用WP-Cron來啟動縮小過程,以避免競態條件,並確保頁面響應速度不會減慢。
過時的縮小腳本和樣式表一直保留到刷新,這可以確保引用過時縮小源的完整頁面緩存不會導致任何404。
可以使用gzip或deflate服務壓縮響應。
轉換樣式表中的相對路徑(例如背景圖像)為絕對路徑,以便它們不會404。
此插件的開發在GitHub上完成。歡迎提交拉取請求。在前往插件論壇之前,請參閱在此報告的問題。如果使用Nginx與此外掛一起運行,請閱讀附加文檔以了解更多資訊。
外掛標籤
開發者團隊
原文外掛簡介
This plugin takes all scripts and stylesheets that have been added via wp_enqueue_script and wp_enqueue_style
and automatically concatenates and minifies them into logical groups. For example, scripts in the footer get grouped
together and styles with the same media (e.g. print) get minified together. Minification is done via WP-Cron in order
to prevent race conditions and to ensure that the minification process does not slow down page responses.
This is a reincarnation and rewrite of the Optimize Scripts plugin,
which this plugin now supersedes.
Features:
Minified sources are stored in the WP Options table so no special filesystem access is required.
Endpoint for minified requests is at /_minified, which can be configured.
Admin page for taking inventory of minified scripts and stylesheets, with methods for expiring or purging the cached data.
Dependencies which must not be minified may be excluded via the dependency_minification_excluded filter.
Dependencies hosted on other domains are by default excluded, but this behavior can be changed by filtering the default_exclude_remote_dependencies option via the dependency_minification_options filter, or on a case-by-case basis via the filter previously mentioned.
By default excludes external scripts from being concatenated and minified, but they can be opted in via the dependency_minification_excluded filter.
The length of time that a minified source is cached defaults to 1 month, but can be configured via the cache_control_max_age_cache option.
If a minified source is not available yet, the page source will note that the dependency minification process is pending.
Any errors that occur during minification will be shown on the frontend in comments if the show_error_messages option is enabled; such errors are enabled by default if WP_DEBUG.
If the minification process errors out, the original unminified sources are served and the error is cached for 1 hour (by default, configured via cache_control_max_age_error) to prevent back-to-back crons from continually attempting to minify in perpetuity.
Cached minified sources are served with Last-Modified and ETag responses headers and requests will honor If-None-Match and If-Modified-Since to return 304 Not Modified responses (configurable via the allow_not_modified_responses option).
Data attached to scripts (e.g. via wp_localize_script) is also concatenated together and attached to the newly-minified script.
WP-Cron is utilized to initiate the minification process in order to prevent race conditions, and to ensure that page responses aren’t slowed down.
Stale minified scripts and stylesheets remain until replaced by refreshed ones; this ensures that full-page caches which reference stale minified sources won’t result in any 404s.
Can serve compressed responses with gzip or deflate.
Transforms relatives paths in stylesheets (e.g. background-images) to absolute ones, so that they don’t 404.
Development of this plugin is done on GitHub. Pull requests welcome. Please see issues reported there before going to the plugin forum.
If you are using Nginx with the default Varying Vagrant Vagrants config, you’ll want to remove css|js from this rule in nginx-wp-common.conf (or remove the rule altogether):
# Handle all static assets by serving the file directly. Add directives
# to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
