Skip to content

Commit c3b10a5

Browse files
committed
Refactor and support older caching http headers
1 parent 5569a16 commit c3b10a5

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/Charcoal/Cache/Middleware/CacheMiddleware.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,26 +192,26 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
192192
$response = $next($request, $response);
193193

194194
if (!$this->isResponseStatusValid($response)) {
195-
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
195+
return $this->noCacheResponse($response);
196196
}
197197

198198
if (!$this->isPathIncluded($path)) {
199-
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
199+
return $this->noCacheResponse($response);
200200
}
201201

202202
if ($this->isPathExcluded($path)) {
203-
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
203+
return $this->noCacheResponse($response);
204204
}
205205

206206
if (!$this->isQueryIncluded($query)) {
207207
$queryArr = $this->parseIgnoredParams($query);
208208
if (!empty($queryArr)) {
209-
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
209+
return $this->noCacheResponse($response);
210210
}
211211
}
212212

213213
if ($this->isQueryExcluded($query)) {
214-
return $response->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
214+
return $this->noCacheResponse($response);
215215
}
216216

217217
// Nothing has excluded the cache so far: add it to the pool.
@@ -401,4 +401,22 @@ private function parseIgnoredParams(array $queryParams)
401401

402402
return array_diff_key($queryParams, array_flip((array)$this->ignoredQuery));
403403
}
404+
405+
/**
406+
* Disable the HTTP cache headers
407+
*
408+
* - Cache-Control is the proper HTTP
409+
* - Pragma is for HTTP 1.0 support
410+
* - Expires is an alternative that also is supported by 1.0 proxies
411+
*
412+
* @param ResponseInterface $response
413+
* @return ResponseInterface
414+
*/
415+
private function noCacheResponse(ResponseInterface $response)
416+
{
417+
return $response
418+
->withHeader('Cache-Control', 'no-cache, no-store, must-revalidate')
419+
->withHeader('Pragma', 'no-cache')
420+
->withHeader('Expires', '0');
421+
}
404422
}

0 commit comments

Comments
 (0)