]> source.dussan.org Git - nextcloud-server.git/commitdiff
Improve caching policy 31141/head
authorCarl Schwan <carl@carlschwan.eu>
Fri, 11 Feb 2022 21:14:04 +0000 (22:14 +0100)
committerCarl Schwan <carl@carlschwan.eu>
Wed, 16 Feb 2022 10:35:57 +0000 (11:35 +0100)
* 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>
.htaccess
apps/theming/lib/Controller/IconController.php
apps/theming/tests/Controller/IconControllerTest.php
core/Controller/PreviewController.php
lib/public/AppFramework/Http/Response.php

index ce84f0698c76e1388318d6daec8681597b401948..60908984185f75f67aabb11381b96fc9b31d355c 100644 (file)
--- a/.htaccess
+++ b/.htaccess
     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"
index 45d3ee14e47010bd9252590ed5bb5f8f0733dad0..173b521039473776b72c9d5ba8053261f829b486 100644 (file)
@@ -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;
        }
 
index 049547821683ff1fa1e591cf5d528458701cd273..470709a3fab379aeb7bf5402793a399d57ed1c6d 100644 (file)
@@ -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'));
        }
 
index 1224ea39877b3f52dc1339a9792b667bfafd017e..30b0a45c13dd559342e9d9fb930de5771b6a7000 100644 (file)
@@ -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);
index ad2280ab9daf985f94a5f36749a78e31fce78201..6febef838cf8b0070248506a32296a45adf29b6d 100644 (file)
@@ -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