From 71ee30014357a2b59fe9848fae7f824aee8f07dd Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Mon, 28 Jan 2013 09:35:34 +0100 Subject: [PATCH] Purge browser cache using a timestamp variable when switching templates. This should hopefully fix Ian and my problems. --- docs/NEWS | 6 +++++- include/admin/templates.inc.php | 1 + include/functions_smarty.inc.php | 8 ++++++++ serendipity_admin.php | 17 ++++++++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docs/NEWS b/docs/NEWS index 9aab78de..2b46d345 100644 --- a/docs/NEWS +++ b/docs/NEWS @@ -3,8 +3,12 @@ Version 2.0 () ------------------------------------------------------------------------ + * When switching templates, both the backend and the frontend + will remember the timestamp of the last template change, + to make sure that the browser will not cache a mismatching CSS. + * Use Smarty for backend display output - + Version 1.7 () ------------------------------------------------------------------------ diff --git a/include/admin/templates.inc.php b/include/admin/templates.inc.php index e72d27f9..74f567fb 100644 --- a/include/admin/templates.inc.php +++ b/include/admin/templates.inc.php @@ -65,6 +65,7 @@ if ($serendipity['GET']['adminAction'] == 'install' ) { serendipity_set_config_var('template', htmlspecialchars($serendipity['GET']['theme'])); serendipity_set_config_var('template_engine', isset($themeInfo['engine']) ? $themeInfo['engine'] : 'default'); + serendipity_set_config_var('last_template_change', time()); $data["adminAction"] = "install"; $data["install_template"] = htmlspecialchars($serendipity['GET']['theme']); diff --git a/include/functions_smarty.inc.php b/include/functions_smarty.inc.php index b984d152..a34ddcbe 100644 --- a/include/functions_smarty.inc.php +++ b/include/functions_smarty.inc.php @@ -967,6 +967,14 @@ function serendipity_smarty_init($vars = array()) { if (!isset($serendipity['smarty_vars']['head_link_stylesheet'])) { $serendipity['smarty_vars']['head_link_stylesheet'] = serendipity_rewriteURL('serendipity.css'); + + // When templates are switched, append a specific version string to make sure the browser does not cache the CSS + if (strstr($serendipity['smarty_vars']['head_link_stylesheet'], '?')) { + $serendipity['smarty_vars']['head_link_stylesheet'] .= '&v=' . $serendipity['last_template_change']; + } else { + $serendipity['smarty_vars']['head_link_stylesheet'] .= '?v=' . $serendipity['last_template_change']; + } + } $serendipity['smarty']->assign( diff --git a/serendipity_admin.php b/serendipity_admin.php index b8bf4e99..c6bbb106 100644 --- a/serendipity_admin.php +++ b/serendipity_admin.php @@ -14,9 +14,24 @@ if (IS_installed === false) { require(S9Y_INCLUDE_PATH . 'include/functions_permalinks.inc.php'); require(S9Y_INCLUDE_PATH . 'include/functions_installer.inc.php'); require(S9Y_INCLUDE_PATH . 'include/functions_config.inc.php'); - $css_file = 'serendipity.css.php?serendipity[css_mode]=serendipity_admin.css'; + $css_file = 'serendipity.css.php?serendipity[css_mode]=serendipity_admin.css&v=' . time(); } else { $css_file = serendipity_rewriteURL('serendipity_admin.css'); + + // This is a bit of an ugly hack, but when switching templates, the HTML head is already emitted, + // so we need a way to actually enforce switching/updating the CSS. So we need to do it at + // this place. + if ($serendipity['GET']['adminAction'] == 'install' && $serendipity['GET']['adminModule'] == 'templates') { + $serendipity['last_template_change'] = time(); + } + + // When templates are switched, append a specific version string to make sure the browser does not cache the CSS + if (strstr($css_file, '?')) { + $css_file .= '&v=' . $serendipity['last_template_change']; + } else { + $css_file .= '?v=' . $serendipity['last_template_change']; + } + if (defined('IS_up2date') && IS_up2date === true) { serendipity_plugin_api::hook_event('backend_configure', $serendipity); }