From fd60bed3a71cf7b9cc8b34f8a2cfa046db4d539b Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione <18699708+giuscris@users.noreply.github.com> Date: Fri, 1 Nov 2024 19:30:25 +0100 Subject: [PATCH] Add cache headers to assets --- formwork/src/Controllers/AssetController.php | 2 +- formwork/src/Controllers/PageController.php | 3 ++- formwork/src/Panel/Controllers/UsersController.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/formwork/src/Controllers/AssetController.php b/formwork/src/Controllers/AssetController.php index 7f6530f2..31300945 100644 --- a/formwork/src/Controllers/AssetController.php +++ b/formwork/src/Controllers/AssetController.php @@ -14,7 +14,7 @@ class AssetController extends AbstractController $path = FileSystem::joinPaths($this->config->get('system.images.processPath'), $routeParams->get('id'), $routeParams->get('name')); if (FileSystem::isFile($path)) { - return new FileResponse($path); + return new FileResponse($path, headers: ['Cache-Control' => 'private, max-age=31536000, immutable']); } throw new FileNotFoundException('Cannot find asset'); diff --git a/formwork/src/Controllers/PageController.php b/formwork/src/Controllers/PageController.php index 63d897f4..5b7a98d2 100644 --- a/formwork/src/Controllers/PageController.php +++ b/formwork/src/Controllers/PageController.php @@ -85,7 +85,8 @@ class PageController extends AbstractController } if ((($parent = $this->site->findPage($upperLevel)) !== null) && $parent->files()->has($filename)) { - return new FileResponse($parent->files()->get($filename)->path()); + $file = $parent->files()->get($filename); + return new FileResponse($file->path(), headers: ['ETag' => $file->hash(), 'Last-Modified' => gmdate('D, d M Y H:i:s T', $file->lastModifiedTime())]); } } diff --git a/formwork/src/Panel/Controllers/UsersController.php b/formwork/src/Panel/Controllers/UsersController.php index e672426c..886b1b86 100644 --- a/formwork/src/Panel/Controllers/UsersController.php +++ b/formwork/src/Panel/Controllers/UsersController.php @@ -218,7 +218,7 @@ class UsersController extends AbstractController $path = FileSystem::joinPaths($this->config->get('system.users.paths.images'), $routeParams->get('image')); if (FileSystem::isFile($path)) { - return new FileResponse($path, headers: ['Cache-Control' => 'max-age=31536000, private']); + return new FileResponse($path, headers: ['Cache-Control' => 'private, max-age=31536000, immutable']); } throw new FileNotFoundException('Cannot find asset');