Skip to content

Commit e55c8b9

Browse files
authored
[5.x] Fix multi-site URL invalidation in ApplicationCacher (#13793)
1 parent 9395cc8 commit e55c8b9

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/StaticCaching/Cachers/ApplicationCacher.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,18 @@ public function flush()
117117
*/
118118
public function invalidateUrl($url, $domain = null)
119119
{
120+
// For CLI contexts where Site::current()->url() may return the wrong
121+
// domain causing getUrls() to look under the wrong cache key.
122+
if ($domain === null) {
123+
[$url, $domain] = $this->getPathAndDomain($url);
124+
}
125+
120126
$this
121127
->getUrls($domain)
122128
->filter(fn ($value) => $value === $url || str_starts_with($value, $url.'?'))
123-
->each(function ($value, $key) {
129+
->each(function ($value, $key) use ($domain) {
124130
$this->cache->forget($this->normalizeKey('responses:'.$key));
125-
$this->forgetUrl($key);
131+
$this->forgetUrl($key, $domain);
126132
});
127133

128134
UrlInvalidated::dispatch($url, $domain);

tests/StaticCaching/ApplicationCacherTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,60 @@ public function invalidating_a_url_will_invalidate_all_query_string_versions_too
120120
$this->assertNotNull($cache->get('static-cache:responses:two'));
121121
}
122122

123+
#[Test]
124+
public function invalidating_a_url_with_explicit_domain_updates_correct_url_index()
125+
{
126+
$cache = app(Repository::class);
127+
$cacher = new ApplicationCacher($cache, ['base_url' => 'http://example.com']);
128+
129+
// Different domain than base_url
130+
$domainHash = md5('http://differentexample.com');
131+
$cache->forever("static-cache:{$domainHash}.urls", [
132+
'one' => '/one',
133+
'two' => '/two',
134+
]);
135+
$cache->forever('static-cache:responses:one', 'html content');
136+
$cache->forever('static-cache:responses:two', 'two html content');
137+
138+
// Invalidate explicit domain
139+
$cacher->invalidateUrl('/one', 'http://differentexample.com');
140+
141+
// URL index under http://differentexample.com should be updated and not http://example.com
142+
$this->assertEquals(
143+
['two' => '/two'],
144+
$cacher->getUrls('http://differentexample.com')->all()
145+
);
146+
$this->assertNull($cache->get('static-cache:responses:one'));
147+
$this->assertNotNull($cache->get('static-cache:responses:two'));
148+
}
149+
150+
#[Test]
151+
public function invalidating_a_full_url_without_domain_extracts_domain_correctly()
152+
{
153+
$cache = app(Repository::class);
154+
$cacher = new ApplicationCacher($cache, ['base_url' => 'http://example.com']);
155+
156+
// Different domain than base_url
157+
$domainHash = md5('http://differentexample.com');
158+
$cache->forever("static-cache:{$domainHash}.urls", [
159+
'one' => '/one',
160+
'two' => '/two',
161+
]);
162+
$cache->forever('static-cache:responses:one', 'html content');
163+
$cache->forever('static-cache:responses:two', 'two html content');
164+
165+
// Invalidate full URL with no explicit domain to simulate CLI
166+
$cacher->invalidateUrl('http://differentexample.com/one');
167+
168+
// Should extract domain from URL and update correct URL index
169+
$this->assertEquals(
170+
['two' => '/two'],
171+
$cacher->getUrls('http://differentexample.com')->all()
172+
);
173+
$this->assertNull($cache->get('static-cache:responses:one'));
174+
$this->assertNotNull($cache->get('static-cache:responses:two'));
175+
}
176+
123177
#[Test]
124178
#[DataProvider('invalidateEventProvider')]
125179
public function invalidating_a_url_dispatches_event($domain, $expectedUrl)

0 commit comments

Comments
 (0)