From 25a7066317b5ba76e888b8fdefabbbcd7f85b3a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 26 Dec 2020 12:25:50 +0200 Subject: [PATCH] Move Cache-Control header setup to routes In slim v3: This is due to the immutable response object, headers need to be set before calling the controller. --- src/Controller/RunController.php | 15 --------------- src/ServiceProvider/RouteProvider.php | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Controller/RunController.php b/src/Controller/RunController.php index de0a81953..1a7803a71 100644 --- a/src/Controller/RunController.php +++ b/src/Controller/RunController.php @@ -30,12 +30,6 @@ public function __construct(App $app, SearcherInterface $searcher) public function index(Request $request, Response $response): void { - // The list changes whenever new profiles are recorded. - // Generally avoid caching, but allow re-use in browser's bfcache - // and by cache proxies for concurrent requests. - // https://github.com/perftools/xhgui/issues/261 - $response->headers->set('Cache-Control', 'public, max-age=0'); - $search = []; $keys = ['date_start', 'date_end', 'server_name', 'url']; foreach ($keys as $key) { @@ -83,15 +77,6 @@ public function index(Request $request, Response $response): void public function view(Request $request, Response $response): void { - // Permalink views to a specific run are meant to be public and immutable. - // But limit the cache to only a short period of time (enough to allow - // handling of abuse or other stampedes). This way we don't have to - // deal with any kind of purging system for when profiles are deleted, - // or for after XHGui itself is upgraded and static assets may be - // incompatible etc. - // https://github.com/perftools/xhgui/issues/261 - $response->headers->set('Cache-Control', 'public, max-age=60, must-revalidate'); - $detailCount = $this->config('detail.count'); $result = $this->searcher->get($request->get('id')); diff --git a/src/ServiceProvider/RouteProvider.php b/src/ServiceProvider/RouteProvider.php index bb0a9d0c4..42ca34c72 100644 --- a/src/ServiceProvider/RouteProvider.php +++ b/src/ServiceProvider/RouteProvider.php @@ -42,15 +42,30 @@ private function registerRoutes(Container $di, App $app): void $request = $app->request(); $response = $app->response(); + // The list changes whenever new profiles are recorded. + // Generally avoid caching, but allow re-use in browser's bfcache + // and by cache proxies for concurrent requests. + // https://github.com/perftools/xhgui/issues/261 + $response->headers->set('Cache-Control', 'public, max-age=0'); + $controller->index($request, $response); })->setName('home'); $app->get('/run/view', static function () use ($di, $app): void { - /** @var Controller\RunController $controller */ - $controller = $di[Controller\RunController::class]; $request = $app->request(); $response = $app->response(); + // Permalink views to a specific run are meant to be public and immutable. + // But limit the cache to only a short period of time (enough to allow + // handling of abuse or other stampedes). This way we don't have to + // deal with any kind of purging system for when profiles are deleted, + // or for after XHGui itself is upgraded and static assets may be + // incompatible etc. + // https://github.com/perftools/xhgui/issues/261 + $response->headers->set('Cache-Control', 'public, max-age=60, must-revalidate'); + + /** @var Controller\RunController $controller */ + $controller = $di[Controller\RunController::class]; $controller->view($request, $response); })->setName('run.view');