Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/Http/Responses/DataResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ public function toResponse($request)
->make($this->contents())
->withHeaders($this->headers);

if ($content = $response->getContent()) {
$response
->setEtag(md5($content))
->isNotModified($request);
}

ResponseCreated::dispatch($response, $this->data);

return $response;
Expand Down
26 changes: 2 additions & 24 deletions src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ public function __construct(Cacher $cacher, Session $nocache)
public function handle($request, Closure $next)
{
if ($response = $this->attemptToServeCachedResponse($request)) {
return $this->addEtagToResponse($request, $response);
return $response;
}

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

try {
return $lock->block($this->lockFor,
fn () => $this->addEtagToResponse($request, $this->handleRequest($request, $next))
);
return $lock->block($this->lockFor, fn () => $this->handleRequest($request, $next));
} catch (LockTimeoutException $e) {
return $this->outputRefreshResponse($request);
}
Expand Down Expand Up @@ -231,24 +229,4 @@ private function outputRefreshResponse($request)

return response($html, 503, ['Retry-After' => 1]);
}

private function addEtagToResponse($request, $response)
{
if (! $response->isRedirect() && $content = $response->getContent()) {
// Clear any potentially stale cache-related headers that might interfere
$response->headers->remove('ETag');
$response->headers->remove('Last-Modified');

// Set fresh ETag based on current content
$response->setEtag(md5($content));

// Only call isNotModified() if request has cache validation headers
// This prevents 304 responses to clients that haven't sent If-None-Match or If-Modified-Since
if ($request->headers->has('If-None-Match') || $request->headers->has('If-Modified-Since')) {
$response->isNotModified($request);
}
}

return $response;
}
}
28 changes: 0 additions & 28 deletions tests/FrontendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1064,32 +1064,4 @@ public function it_protects_404_pages()
->get('/does-not-exist')
->assertStatus(404);
}

#[Test]
public function it_sets_etag_header_and_returns_304_when_content_matches()
{
$this->withStandardBlueprints();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('default', '<h1>Test Page</h1>');

$this->createPage('about');

$response = $this->get('/about');
$response->assertStatus(200);

$content = trim($response->content());
$this->assertEquals('<h1>Test Page</h1>', $content);

$etag = $response->headers->get('ETag');
$this->assertEquals('"'.md5($content).'"', $etag); // Per spec, the quotes need to be in the string.

$response = $this->get('/about', ['If-None-Match' => $etag]);
$response->assertStatus(304);
$this->assertEmpty($response->content());

$response = $this->get('/about', ['If-None-Match' => '"wrong-etag"']);
$response->assertStatus(200);
$this->assertEquals('<h1>Test Page</h1>', trim($response->content()));
}
}