Add nocache param on JS/CSS URL to avoid cache problems

This commit is contained in:
Benjamin Renard 2018-05-22 18:47:08 +02:00
parent 18278bcc23
commit 35b9b9ad94
2 changed files with 39 additions and 13 deletions

View file

@ -1338,7 +1338,8 @@ class LSsession {
// JS
$JSscript_txt='';
foreach ($GLOBALS['defaultJSscipts'] as $script) {
$JSscript_txt.="<script src='".LS_JS_DIR.$script."' type='text/javascript'></script>\n";
$nocache = LStemplate :: getNoCacheFileValue(LS_JS_DIR.$script);
$JSscript_txt.="<script src='".LS_JS_DIR.$script."?nocache=$nocache' type='text/javascript'></script>\n";
}
foreach (self :: $JSscripts as $script) {
@ -1348,7 +1349,8 @@ class LSsession {
else {
$script['path'].='/';
}
$JSscript_txt.="<script src='".$script['path'].$script['file']."' type='text/javascript'></script>\n";
$nocache = LStemplate :: getNoCacheFileValue($script['path'].$script['file']);
$JSscript_txt.="<script src='".$script['path'].$script['file']."?nocache=$nocache' type='text/javascript'></script>\n";
}
$KAconf = LSconfig :: get('keepLSsessionActive');
@ -1384,7 +1386,8 @@ class LSsession {
}
$Css_txt='';
foreach (self :: $CssFiles as $file) {
$Css_txt.="<link rel='stylesheet' type='text/css' href='".$file."' />\n";
$nocache = LStemplate :: getNoCacheFileValue($file);
$Css_txt.="<link rel='stylesheet' type='text/css' href='".$file."?nocache=$nocache' />\n";
}
LStemplate :: assign('LSsession_css',$Css_txt);

View file

@ -138,32 +138,39 @@ class LStemplate {
* @param[in] string $name The file name (eg: mail.png)
* @param[in] string $root_dir The root directory (eg: images)
* @param[in] string $default_dir The default directory (eg: default)
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
*
* @retval string The path of the file
**/
public static function getFilePath($file,$root_dir,$default_dir='default') {
public static function getFilePath($file, $root_dir, $default_dir=null, $with_nocache=false) {
if ($default_dir === null)
$default_dir = 'default';
foreach(self :: $directories as $dir) {
if (file_exists($root_dir.'/'.$dir.'/'.$file)) {
return $root_dir.'/'.$dir.'/'.$file;
$path = $root_dir.'/'.$dir.'/'.$file;
}
}
if (!$default_dir) {
return;
}
return $root_dir.'/'.$default_dir.'/'.$file;
$path = $root_dir.'/'.$default_dir.'/'.$file;
if ($with_nocache)
$path .= "?nocache=".self::getNoCacheFileValue($path);
return $path;
}
/**
* Return the path of the image file to use
*
* @param[in] string $image The image name (eg: mail)
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
*
* @retval string The path of the image file
**/
public static function getImagePath($image) {
public static function getImagePath($image, $with_nocache=false) {
$exts=array('png','gif','jpg');
foreach($exts as $ext) {
$path=self :: getFilePath("$image.$ext",self :: $config['image_dir'],False);
$path = self :: getFilePath("$image.$ext", self :: $config['image_dir'], False, $with_nocache);
if ($path) return $path;
}
return self :: $config['image_dir']."/default/$image.png";
@ -173,22 +180,38 @@ class LStemplate {
* Return the path of the CSS file to use
*
* @param[in] string $css The CSS name (eg: main.css)
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
*
* @retval string The path of the CSS file
**/
public static function getCSSPath($css) {
return self :: getFilePath($css,self :: $config['css_dir']);
public static function getCSSPath($css, $with_nocache=false) {
return self :: getFilePath($css, self :: $config['css_dir'], Null, $with_nocache);
}
/**
* Return the path of the Smarty template file to use
*
* @param[in] string $template The template name (eg: top.tpl)
* @param[in] bool $with_nocache If true, include nocache URL param (default: false)
*
* @retval string The path of the Smarty template file
**/
public static function getTemplatePath($template) {
return self :: getFilePath($template,self :: $config['template_dir']);
public static function getTemplatePath($template, $with_nocache=false) {
return self :: getFilePath($template, self :: $config['template_dir'], null, $with_nocache);
}
/**
* Return the nocache value of the specify file
*
* @param[in] string $file The file path
*
* @retval string The specified file's nocache value
**/
public static function getNoCacheFileValue($file) {
$stat = @stat($file);
if (is_array($stat) && isset($stat['mtime']))
return md5($stat['mtime']);
return md5(time());
}
/**
@ -295,7 +318,7 @@ function LStemplate_smarty_img($params) {
function LStemplate_smarty_css($params) {
extract($params);
echo LStemplate :: getCSSPath($name);
echo LStemplate :: getCSSPath($name, true);
}
function LStemplate_smarty_uniqid($params, &$smarty) {