Skip to content

Commit 7b7bddf

Browse files
authored
[5.x] Fix: Prevent 304 responses without client cache headers (#13654)
1 parent 2c4c770 commit 7b7bddf

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/StaticCaching/Middleware/Cache.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,18 @@ private function outputRefreshResponse($request)
235235
private function addEtagToResponse($request, $response)
236236
{
237237
if (! $response->isRedirect() && $content = $response->getContent()) {
238-
$response
239-
->setEtag(md5($content))
240-
->isNotModified($request);
238+
// Clear any potentially stale cache-related headers that might interfere
239+
$response->headers->remove('ETag');
240+
$response->headers->remove('Last-Modified');
241+
242+
// Set fresh ETag based on current content
243+
$response->setEtag(md5($content));
244+
245+
// Only call isNotModified() if request has cache validation headers
246+
// This prevents 304 responses to clients that haven't sent If-None-Match or If-Modified-Since
247+
if ($request->headers->has('If-None-Match') || $request->headers->has('If-Modified-Since')) {
248+
$response->isNotModified($request);
249+
}
241250
}
242251

243252
return $response;

0 commit comments

Comments
 (0)