Skip to content

Commit 2d80fa5

Browse files
authored
[5.x] Generate etag after nocache replacements (#13433)
1 parent 0a970e4 commit 2d80fa5

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/StaticCaching/Middleware/Cache.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ public function __construct(Cacher $cacher, Session $nocache)
5050
public function handle($request, Closure $next)
5151
{
5252
if ($response = $this->attemptToServeCachedResponse($request)) {
53-
return $response;
53+
return $this->addEtagToResponse($request, $response);
5454
}
5555

5656
$lock = $this->createLock($request);
5757

5858
try {
59-
return $lock->block($this->lockFor, fn () => $this->handleRequest($request, $next));
59+
return $lock->block($this->lockFor,
60+
fn () => $this->addEtagToResponse($request, $this->handleRequest($request, $next))
61+
);
6062
} catch (LockTimeoutException $e) {
6163
return $this->outputRefreshResponse($request);
6264
}
@@ -229,4 +231,15 @@ private function outputRefreshResponse($request)
229231

230232
return response($html, 503, ['Retry-After' => 1]);
231233
}
234+
235+
private function addEtagToResponse($request, $response)
236+
{
237+
if (! $response->isRedirect() && $content = $response->getContent()) {
238+
$response
239+
->setEtag(md5($content))
240+
->isNotModified($request);
241+
}
242+
243+
return $response;
244+
}
232245
}

0 commit comments

Comments
 (0)