1、原來的寫法
<link href="/static/css/m.min.css" rel="stylesheet">
2、改進後的寫法
在配置檔案中增加一個配置,如果需要更新就修改版本号
<link href="/static/css/m.min.css?t={{version.app_version}}" rel="stylesheet">
例如
<link href="/static/css/m.min.css?t=1623291687929" rel="stylesheet">
改進處理可以使用檔案的md5值作為版本号,隻要發生修改就會更新版本号
關于版本号的處理
1、每次部署的時候自動生成一個版本号檔案
echo $(date "+%Y%m%d%H%M%S") > version.txt
2、配置檔案中讀取版本号
config/version.php
<?php
// +----------------------------------------------------------------------
// | 應用版本号設定
// +----------------------------------------------------------------------
// 生成version
// echo $(date "+%Y%m%d%H%M%S") > version.txt
$version_file = '../version.txt';
if(file_exists($version_file)){
$version = trim(file_get_contents($version_file));
} else{
$version = time() . '';
}
return [
// 應用版本号,用于更新靜态檔案
'app_version' => $version,
];
3、每次渲染模闆的時候在中間件中讀取該配置
<?php
declare (strict_types=1);
namespace app\index\middleware;
use \Think\response\View as ViewResponse;
class CommonDataMiddleware
{
public function handle($request, \Closure $next)
{
$response = $next($request);
if ($response instanceOf ViewResponse) {
if (!$response->getVars('version')) {
$response->assign('version', config('version'));
}
}
return $response;
}
}
4、模闆檔案中使用
<link href="/static/css/m.min.css?t={{version.app_version}}" rel="stylesheet">
現在就實作了重新部署就更新版本号,穩定運作就不更新