Skip to content

Commit

Permalink
Do not send cache headers with known incorrect path
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Apr 27, 2020
1 parent cf727c5 commit dde3b40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ public function handleRequest(Request $request): Response
$failedHash = false;
}

if((!$relativeHash || $failedHash) && $fileHash === $manager->getFileHash($fullPath))
$contentHashMatch = $fileHash === $manager->getFileHash($fullPath);
if((!$relativeHash || $failedHash) && $contentHashMatch)
{
$failedHash = false;
}
Expand Down Expand Up @@ -281,7 +282,7 @@ public function handleRequest(Request $request): Response
$resource->setOptions($this->config()->getSection('ext.' . $ext)->getItems());
}
}
return ResourceFactory::create($resource);
return ResourceFactory::create($resource, $contentHashMatch);
}

public function config()
Expand Down
32 changes: 19 additions & 13 deletions src/Resources/ResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ public static function getExtensionResource($extension): DispatchResource
/**
* @param DispatchResource $resource
*
* @param bool $cache
*
* @return Response
* @throws \Exception
*/
public static function create(DispatchResource $resource)
public static function create(DispatchResource $resource, $cache = true)
{
$response = new Response();

Expand All @@ -127,20 +128,25 @@ public static function create(DispatchResource $resource)
//Domain specific content will vary on the uri itself
$response->headers->set("Vary", "Accept-Encoding");

//Set the etag to the hash of the request uri, as it is in itself a hash
$response->setEtag($resource->getHash());
$response->setPublic();
if($cache)
{
//Set the etag to the hash of the request uri, as it is in itself a hash
$response->setEtag($resource->getHash());
$response->setPublic();

//This resource should last for 1 year in cache
$response->setMaxAge(31536000);
$response->setSharedMaxAge(31536000);
$response->setExpires((new \DateTime())->add(new \DateInterval('P365D')));
//This resource should last for 1 year in cache
$response->setMaxAge(31536000);
$response->setSharedMaxAge(31536000);
$response->setExpires((new \DateTime())->add(new \DateInterval('P365D')));

//Set the last modified date to now
$date = new \DateTime();
$date->setTimezone(new \DateTimeZone('UTC'));
$response->headers->set('Last-Modified', $date->format('D, d M Y H:i:s') . ' GMT');
}

//Set the last modified date to now
$date = new \DateTime();
$date->setTimezone(new \DateTimeZone('UTC'));
$response->headers->set('Last-Modified', $date->format('D, d M Y H:i:s') . ' GMT');
$response->setContent($resource->getContent());

return $response;
}
}

0 comments on commit dde3b40

Please sign in to comment.