summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2022-02-17 16:58:35 +0100
committerGitHub <noreply@github.com>2022-02-17 16:58:35 +0100
commitac4978e715862ec9e86150b1171296629eebeced (patch)
treedfb1705f6687f501c2c6e1cd69680410e94db81e
parent098dfe15c1e932a2acc8bb4853979040339a3ad7 (diff)
parent7dddbd0c355d1b4761466f9f86b30aed8b112ba1 (diff)
downloadnextcloud-server-ac4978e715862ec9e86150b1171296629eebeced.tar.gz
nextcloud-server-ac4978e715862ec9e86150b1171296629eebeced.zip
Merge pull request #31141 from nextcloud/fix/better-cache-policy
Improve caching policy use immutable when loading versionned assets
-rw-r--r--.htaccess4
-rw-r--r--apps/theming/lib/Controller/IconController.php2
-rw-r--r--apps/theming/tests/Controller/IconControllerTest.php2
-rw-r--r--core/Controller/PreviewController.php6
-rw-r--r--lib/public/AppFramework/Http/Response.php4
5 files changed, 12 insertions, 6 deletions
diff --git a/.htaccess b/.htaccess
index ce84f0698c7..60908984185 100644
--- a/.htaccess
+++ b/.htaccess
@@ -47,6 +47,10 @@
Header set Cache-Control "max-age=15778463"
</FilesMatch>
+ <FilesMatch "\.(css|js|svg|gif|png|jpg|ico|wasm|tflite)(\?v=.*)?$">
+ Header set Cache-Control "max-age=15778463, immutable"
+ </FilesMatch>
+
# Let browsers cache WOFF files for a week
<FilesMatch "\.woff2?$">
Header set Cache-Control "max-age=604800"
diff --git a/apps/theming/lib/Controller/IconController.php b/apps/theming/lib/Controller/IconController.php
index 45d3ee14e47..173b5210394 100644
--- a/apps/theming/lib/Controller/IconController.php
+++ b/apps/theming/lib/Controller/IconController.php
@@ -96,7 +96,7 @@ class IconController extends Controller {
$iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon);
}
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
- $response->cacheFor(86400);
+ $response->cacheFor(86400, false, true);
return $response;
}
diff --git a/apps/theming/tests/Controller/IconControllerTest.php b/apps/theming/tests/Controller/IconControllerTest.php
index 04954782168..470709a3fab 100644
--- a/apps/theming/tests/Controller/IconControllerTest.php
+++ b/apps/theming/tests/Controller/IconControllerTest.php
@@ -104,7 +104,7 @@ class IconControllerTest extends TestCase {
->with('icon-core-filetypes_folder.svg')
->willReturn($file);
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
- $expected->cacheFor(86400);
+ $expected->cacheFor(86400, false, true);
$this->assertEquals($expected, $this->iconController->getThemedIcon('core', 'filetypes/folder.svg'));
}
diff --git a/core/Controller/PreviewController.php b/core/Controller/PreviewController.php
index 1224ea39877..30b0a45c13d 100644
--- a/core/Controller/PreviewController.php
+++ b/core/Controller/PreviewController.php
@@ -167,8 +167,10 @@ class PreviewController extends Controller {
try {
$f = $this->preview->getPreview($node, $x, $y, !$a, $mode);
- $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
- $response->cacheFor(3600 * 24);
+ $response = new FileDisplayResponse($f, Http::STATUS_OK, [
+ 'Content-Type' => $f->getMimeType(),
+ ]);
+ $response->cacheFor(3600 * 24, false, true);
return $response;
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
diff --git a/lib/public/AppFramework/Http/Response.php b/lib/public/AppFramework/Http/Response.php
index ad2280ab9da..6febef838cf 100644
--- a/lib/public/AppFramework/Http/Response.php
+++ b/lib/public/AppFramework/Http/Response.php
@@ -110,10 +110,10 @@ class Response {
* @return $this
* @since 6.0.0 - return value was added in 7.0.0
*/
- public function cacheFor(int $cacheSeconds, bool $public = false) {
+ public function cacheFor(int $cacheSeconds, bool $public = false, bool $immutable = false) {
if ($cacheSeconds > 0) {
$pragma = $public ? 'public' : 'private';
- $this->addHeader('Cache-Control', $pragma . ', max-age=' . $cacheSeconds . ', must-revalidate');
+ $this->addHeader('Cache-Control', sprintf('%s, max-age=%s, %s', $pragma, $cacheSeconds, ($immutable ? 'immutable' : 'must-revalidate')));
$this->addHeader('Pragma', $pragma);
// Set expires header