Skip to content

Commit 1da8fe5

Browse files
committed
Improve caching policy
* Cache css with version in url. This makes most js and css requests to be cached by the browser * Force caching previews, the etag is in the url so that if the propfind gives a new etag, we will refresh it otherwise it's no use to try to fetch the new etag and do tons of DB queries Tested with firefox and 'debug' => false (important so that the js/css urls are generated with ?v= parameter) Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent ef67f01 commit 1da8fe5

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

.htaccess

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
Header set Cache-Control "max-age=15778463"
4848
</FilesMatch>
4949

50+
<FilesMatch "\.(css|js|svg|gif|png|jpg|ico|wasm|tflite)(\?v=.*)?$">
51+
Header set Cache-Control "max-age=15778463, immutable"
52+
</FilesMatch>
53+
5054
# Let browsers cache WOFF files for a week
5155
<FilesMatch "\.woff2?$">
5256
Header set Cache-Control "max-age=604800"

apps/theming/lib/Controller/IconController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function getThemedIcon(string $app, string $image): Response {
9797
}
9898
if ($iconFile !== false) {
9999
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
100-
$response->cacheFor(86400);
100+
$response->cacheFor(86400, false, true);
101101
return $response;
102102
}
103103

apps/theming/tests/Controller/IconControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function testGetThemedIcon() {
102102
->with('icon-core-filetypes_folder.svg')
103103
->willReturn($file);
104104
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
105-
$expected->cacheFor(86400);
105+
$expected->cacheFor(86400, false, true);
106106
$this->assertEquals($expected, $this->iconController->getThemedIcon('core', 'filetypes/folder.svg'));
107107
}
108108

core/Controller/PreviewController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ private function fetchPreview(
167167

168168
try {
169169
$f = $this->preview->getPreview($node, $x, $y, !$a, $mode);
170-
$response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
171-
$response->cacheFor(3600 * 24);
170+
$response = new FileDisplayResponse($f, Http::STATUS_OK, [
171+
'Content-Type' => $f->getMimeType(),
172+
]);
173+
$response->cacheFor(3600 * 24, false, true);
172174
return $response;
173175
} catch (NotFoundException $e) {
174176
return new DataResponse([], Http::STATUS_NOT_FOUND);

lib/public/AppFramework/Http/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public function __construct() {
103103
* @return $this
104104
* @since 6.0.0 - return value was added in 7.0.0
105105
*/
106-
public function cacheFor(int $cacheSeconds, bool $public = false) {
106+
public function cacheFor(int $cacheSeconds, bool $public = false, bool $immutable = false) {
107107
if ($cacheSeconds > 0) {
108108
$pragma = $public ? 'public' : 'private';
109-
$this->addHeader('Cache-Control', $pragma . ', max-age=' . $cacheSeconds . ', must-revalidate');
109+
$this->addHeader('Cache-Control', sprintf('%s, max-age=%s, %s', $pragma, $cacheSeconds, ($immutable ? 'immutable' : 'must-revalidate')));
110110
$this->addHeader('Pragma', $pragma);
111111

112112
// Set expires header

0 commit comments

Comments
 (0)