Skip to content

Commit dde3b40

Browse files
committed
Do not send cache headers with known incorrect path
1 parent cf727c5 commit dde3b40

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/Dispatch.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ public function handleRequest(Request $request): Response
251251
$failedHash = false;
252252
}
253253

254-
if((!$relativeHash || $failedHash) && $fileHash === $manager->getFileHash($fullPath))
254+
$contentHashMatch = $fileHash === $manager->getFileHash($fullPath);
255+
if((!$relativeHash || $failedHash) && $contentHashMatch)
255256
{
256257
$failedHash = false;
257258
}
@@ -281,7 +282,7 @@ public function handleRequest(Request $request): Response
281282
$resource->setOptions($this->config()->getSection('ext.' . $ext)->getItems());
282283
}
283284
}
284-
return ResourceFactory::create($resource);
285+
return ResourceFactory::create($resource, $contentHashMatch);
285286
}
286287

287288
public function config()

src/Resources/ResourceFactory.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ public static function getExtensionResource($extension): DispatchResource
112112
/**
113113
* @param DispatchResource $resource
114114
*
115+
* @param bool $cache
116+
*
115117
* @return Response
116-
* @throws \Exception
117118
*/
118-
public static function create(DispatchResource $resource)
119+
public static function create(DispatchResource $resource, $cache = true)
119120
{
120121
$response = new Response();
121122

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

130-
//Set the etag to the hash of the request uri, as it is in itself a hash
131-
$response->setEtag($resource->getHash());
132-
$response->setPublic();
131+
if($cache)
132+
{
133+
//Set the etag to the hash of the request uri, as it is in itself a hash
134+
$response->setEtag($resource->getHash());
135+
$response->setPublic();
133136

134-
//This resource should last for 1 year in cache
135-
$response->setMaxAge(31536000);
136-
$response->setSharedMaxAge(31536000);
137-
$response->setExpires((new \DateTime())->add(new \DateInterval('P365D')));
137+
//This resource should last for 1 year in cache
138+
$response->setMaxAge(31536000);
139+
$response->setSharedMaxAge(31536000);
140+
$response->setExpires((new \DateTime())->add(new \DateInterval('P365D')));
141+
142+
//Set the last modified date to now
143+
$date = new \DateTime();
144+
$date->setTimezone(new \DateTimeZone('UTC'));
145+
$response->headers->set('Last-Modified', $date->format('D, d M Y H:i:s') . ' GMT');
146+
}
138147

139-
//Set the last modified date to now
140-
$date = new \DateTime();
141-
$date->setTimezone(new \DateTimeZone('UTC'));
142-
$response->headers->set('Last-Modified', $date->format('D, d M Y H:i:s') . ' GMT');
143148
$response->setContent($resource->getContent());
149+
144150
return $response;
145151
}
146152
}

0 commit comments

Comments
 (0)