[TikiWiki-commits] [Git][tikiwiki/tiki][master] [ENH] Improve Caching and HTTP Headers for Assets Served via Tiki and Apache
"SoftStart Code \(@softstartcode\) via TikiWiki-cvs" <[email protected]>
| Newsgroups | gmane.comp.cms.tiki.cvs |
|---|---|
| Message-ID | <68a26aab92acd_2cd6b3c94736@gitlab-sidekiq-low-urgency-cpu-bound-v2-7b465d79f4-bskmm.mail> |
SoftStart Code pushed to branch master at Tiki Wiki CMS Groupware / Tiki
Commits:
7822f499 by Sandeep D at 2025-08-17T23:42:21+00:00
[ENH] Improve Caching and HTTP Headers for Assets Served via Tiki and Apache
---
* [UPD] tiki-download_file.php: Remove redundant multiplication of max age for cache control
* [UPD] Update caching settings: change default max age to seconds and revert the htaccess
* [ENH] Improve Caching and HTTP Headers for Assets Served via Tiki and Apache
This merge request enhances Tiki's performance by optimizing caching and HTTP headers for both dynamically served assets (via PHP) and static files (served directly by Apache). The goal is to ensure assets are cached efficiently while respecting permission settings and security requirements.
**Changes Implemented:**
**1. HTTP Headers for Assets Served via Tiki (PHP)**
- **Publicly accessible assets** now include `Cache-Control: public, max-age=<user controlable>`.
- **Restricted assets** are explicitly marked as non-cacheable (`Cache-Control: private, no-store, no-cache`).
- **New Admin Preference**: Added a setting to allow admins to configure cache expiration (TTL).
**2. Improved Caching for Static Assets Served by Apache**
- **JS, CSS, images, and fonts** now include optimal cache headers:
- `Cache-Control: public, max-age=30days`
- `ETag` and `Last-Modified` for efficient cache validation.
**3. Web Page Caching Review**
- Reviewed caching behavior for web pages served by Tiki.
- Ensured proper handling of pages accessed by anonymous vs. authenticated users.
See merge request tikiwiki/tiki!7088
- - - - -
3 changed files:
- lib/prefs/tiki.php
- templates/admin/include_performance.tpl
- tiki-download_file.php
Changes:
=====================================
lib/prefs/tiki.php
=====================================
@@ -101,6 +101,13 @@ function prefs_tiki_list()
'dependencies' => [ 'session_silent' ],
'default' => '',
],
+ 'tiki_cachecontrol_maxage' => [
+ 'name' => tra('Cache-Control Max Age'),
+ 'description' => tra('Defines the maximum age (in seconds) for caching assets via the Cache-Control HTTP header.'),
+ 'type' => 'text',
+ 'filter' => 'digits',
+ 'default' => '86400',
+ ],
'tiki_cdn' => [
'name' => tra('Content delivery networks'),
'description' => tra('Use alternate domains to serve static files from this Tiki site to avoid sending cookies, improve local caching and generally improve user-experience performance.'),
=====================================
templates/admin/include_performance.tpl
=====================================
@@ -36,6 +36,7 @@
{/if}
</div>
{preference name=tiki_cachecontrol_session}
+ {preference name=tiki_cachecontrol_maxage}
{preference name=smarty_compilation}
{preference name=users_serve_avatar_static}
{preference name=allowImageLazyLoad }
=====================================
tiki-download_file.php
=====================================
@@ -251,10 +251,6 @@ if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $last_modified == strtotime(cur
unset($tmp);
}
-header("Pragma: ");
-header('Expires: ');
-header('Cache-Control: ' . ( ! empty($user) ? 'private' : 'public' ) . ',must-revalidate,post-check=0,pre-check=0');
-
if ($use_client_cache) {
header('Status: 304 Not Modified', true, 304);
exit;
@@ -264,6 +260,18 @@ if ($use_client_cache) {
}
}
+$maxAge = isset($prefs['tiki_cachecontrol_maxage']) ? (int) $prefs['tiki_cachecontrol_maxage'] : 86400;
+$downloadableByAnonymous = $userlib->user_has_perm_on_object(null, $info['fileId'], 'file', 'tiki_p_download_files');
+
+if ($downloadableByAnonymous) {
+ header('Cache-Control: public, max-age=' . $maxAge . ', must-revalidate');
+ header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $maxAge) . ' GMT');
+} else {
+ header('Cache-Control: private, no-cache, no-store, must-revalidate');
+ header('Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
+ header('Pragma: no-cache');
+}
+
// Indicates if a 'office' document should be converted to pdf for download or display in browser.
$convertToPdf = (isset($_GET['display']) || isset($_GET['pdf'])) && PDFHelper::canConvertToPDF($info['filetype']) && $prefs['fgal_convert_documents_pdf'] == 'y';
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/7822f499448386f498cb933b0635887723840cb8
--
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/7822f499448386f498cb933b0635887723840cb8
You're receiving this email because of your account on gitlab.com.
_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs