我正在使用一些 php 代码_init.php将版本号添加到 css 文件名。
如今,大多数浏览器都会将具有不同查询字符串的 URL 视为不同的文件并下载新副本。大多数 CDN 甚至都支持并推荐这一点。更多信息:css-tricks.com
这是PHP:
// Set path and url
$assets_path = $config->paths->templates . "your-folder/";
$assets_url = $config->urls->templates . "your-folder/";
$file_css = "app.css";
// Generate css version addon
// Check if file exists and get date of last change
if (file_exists($assets_path . $file_css)) {
$css_version_addon = "?v" . date("His", filemtime($assets_path .
$file_css)); // Time of last change 23h 59m 59s in format 235959
} else {
$css_version_addon = "?somethingWentWrong";
}
// Full url with css file and version addon
$css_with_version = $assets_url . $file_css . $css_version_addon;
您可以在模板中输出它
// In your template file
<link rel="stylesheet" href="<?php echo $css_with_version; ?>" />
// This will output to something like this
<link rel="stylesheet" href="/site/templates/your-folder/app.css?v101525" />
我在我所有的 ProcessWire 模板中都使用了它。正如@jmartsch 提到的,有很多策略,但这个最适合我。如何生成文件并不重要(我使用带有 gulp 的 scss 来生成 css)。脚本只检查文件是否被修改并输出更改时间。