I am looking at a Caching library that is trying to use the If-Modified-Since
header of a request object. The problem is this header never gets set, it is always blank which makes sense to me seeing how it is a REQUEST.
How can you force a request to have a If-Modified-Since
header? Or am I way off for what this does.
Here is the function I am referring to.
public function isNotModified(Request $request)
{
$lastModified = $request->headers->get('If-Modified-Since');
$notModified = false;
if ($etags = $request->getEtags()) {
$notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);
} elseif ($lastModified) {
$notModified = $lastModified == $this->headers->get('Last-Modified');
}
if ($notModified) {
$this->setNotModified();
}
return $notModified;
}
A request with
If-Modified-Since
only makes sense if the client already has a resource which is obtained along with a response that has aLast-Modified
header in combination with headers which allow browser caching like aCache-Control
and/orPragma
value containingpublic
.Also, I've noticed that some browsers does not include
If-Modified-Since
when the original response also contained anETag
header. The browser will instead useIf-None-Match
to test it.See also:
First you have to make sure the initial response is cached in the first place (I answered this in another, related question.
Try to set the following fields:
Last-Modified
is needed as a validator (do not sendETag
if you want to test forIf-Modified-Since
)Expires -1
tells that the resource is stale and must always be revalidatedCache-Control
must not include no-cache nor no-storeWhen you send these headers on the initial
HTTP/200
response, on subsequent requests, the browser should send conditional requests that include theIf-Modified-Since
header.