cache_javascript_headers()
云策文档标注
概述
cache_javascript_headers() 函数用于设置 JavaScript 文件的 HTTP 缓存头,包括内容类型和 10 天过期时间。
关键要点
- 设置 Content-Type 为 text/javascript,并使用 get_bloginfo('charset') 获取字符集
- 添加 Vary: Accept-Encoding 头以处理代理服务器
- 设置 Expires 头,基于当前时间加上 10 天(使用 DAY_IN_SECONDS 常量)
- 该函数自 WordPress 2.1.0 版本引入
代码示例
function cache_javascript_headers() {
$expires_offset = 10 * DAY_IN_SECONDS;
header( 'Content-Type: text/javascript; charset=' . get_bloginfo( 'charset' ) );
header( 'Vary: Accept-Encoding' ); // Handle proxies.
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expires_offset ) . ' GMT' );
}
原文内容
Sets the HTTP headers for caching for 10 days with JavaScript content type.
Source
function cache_javascript_headers() {
$expires_offset = 10 * DAY_IN_SECONDS;
header( 'Content-Type: text/javascript; charset=' . get_bloginfo( 'charset' ) );
header( 'Vary: Accept-Encoding' ); // Handle proxies.
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expires_offset ) . ' GMT' );
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |