Skip to content

Commit

Permalink
Merge pull request #270 from Krinkle/runslist-cc
Browse files Browse the repository at this point in the history
Set Cache-Control headers for runs list and runs view
  • Loading branch information
markstory committed Jun 10, 2019
2 parents f9cc197 + 8e4044f commit 1e8b3d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Xhgui/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ public function templateVars()

public function render()
{
$this->app->render($this->_template, $this->_templateVars);
// We want to render the specified Twig template to the output buffer.
// The simplest way to do that is Slim::render, but that is not allowed
// in middleware, because it uses Slim\View::display which prints
// directly to the native PHP output buffer.
// Doing that is problematic, because the HTTP readers set via $app->response()
// must be output first, which won't happen until after the middleware
// is completed. Output of headers and body is done by the Slim::run entry point.

// The below is copied from Slim::render (slim/slim@2.6.3).
// Modified to use View::fetch + Response::write, instead of View::display.
$this->app->view->appendData($this->_templateVars);
$body = $this->app->view->fetch($this->_template);
$this->app->response->write($body);
}

}
17 changes: 17 additions & 0 deletions src/Xhgui/Controller/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public function __construct(Slim $app, Xhgui_Searcher_Interface $searcher)

public function index()
{
$response = $this->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');

$request = $this->app->request();

$search = array();
Expand Down Expand Up @@ -73,6 +80,16 @@ public function index()

public function view()
{
$response = $this->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');

$request = $this->app->request();
$detailCount = $this->app->config('detail.count');
$result = $this->searcher->get($request->get('id'));
Expand Down

0 comments on commit 1e8b3d4

Please sign in to comment.