Skip to content

Commit ac4978e

Browse files
authored
Merge pull request #31141 from nextcloud/fix/better-cache-policy
Improve caching policy use immutable when loading versionned assets
2 parents 098dfe1 + 7dddbd0 commit ac4978e

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
@@ -96,7 +96,7 @@ public function getThemedIcon(string $app, string $image): Response {
9696
$iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon);
9797
}
9898
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
99-
$response->cacheFor(86400);
99+
$response->cacheFor(86400, false, true);
100100
return $response;
101101
}
102102

apps/theming/tests/Controller/IconControllerTest.php

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

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
@@ -110,10 +110,10 @@ public function __construct() {
110110
* @return $this
111111
* @since 6.0.0 - return value was added in 7.0.0
112112
*/
113-
public function cacheFor(int $cacheSeconds, bool $public = false) {
113+
public function cacheFor(int $cacheSeconds, bool $public = false, bool $immutable = false) {
114114
if ($cacheSeconds > 0) {
115115
$pragma = $public ? 'public' : 'private';
116-
$this->addHeader('Cache-Control', $pragma . ', max-age=' . $cacheSeconds . ', must-revalidate');
116+
$this->addHeader('Cache-Control', sprintf('%s, max-age=%s, %s', $pragma, $cacheSeconds, ($immutable ? 'immutable' : 'must-revalidate')));
117117
$this->addHeader('Pragma', $pragma);
118118

119119
// Set expires header

0 commit comments

Comments
 (0)