Skip to content

Commit 01a3af6

Browse files
[6.x] Fix cache invalidation when using custom fields in URI route (#14564)
1 parent 340b330 commit 01a3af6

5 files changed

Lines changed: 36 additions & 4 deletions

File tree

src/Stache/Stores/CollectionEntriesStore.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,11 @@ protected function writeItemToDisk($item)
215215
break;
216216
}
217217

218-
$itemFromDisk = $this->makeItemFromFile($path, $contents);
218+
// Only parse the ID from the file rather than using makeItemFromFile,
219+
// which would put a stale entry into the structure-entries blink store.
220+
$id = Arr::get(YAML::file($path)->parse($contents), 'id');
219221

220-
if ($item->id() == $itemFromDisk->id()) {
222+
if ($item->id() == $id) {
221223
break;
222224
}
223225

@@ -269,7 +271,7 @@ public function updateParents($ids = null)
269271

270272
private function updateEntriesWithinIndex($index, $ids)
271273
{
272-
if (empty($ids)) {
274+
if (collect($ids)->isEmpty()) {
273275
return $index->update();
274276
}
275277

@@ -281,7 +283,7 @@ private function updateEntriesWithinIndex($index, $ids)
281283

282284
private function updateEntriesWithinStore($ids)
283285
{
284-
if (empty($ids)) {
286+
if (collect($ids)->isEmpty()) {
285287
$ids = $this->paths()->keys();
286288
}
287289

src/Structures/CollectionStructure.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function entryUri($entry)
4444
->keyBy->reference()
4545
->get($entry->id());
4646

47+
$page?->setEntry($entry);
48+
4749
return optional($page)->uri();
4850
}
4951

src/Structures/Page.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ public function setEntry($reference): self
139139
if (is_object($reference)) {
140140
throw_unless($id = $reference->id(), new \Exception('Cannot set an entry without an ID'));
141141
Blink::store('structure-entries')->put($id, $reference);
142+
Blink::store('structure-uris')->forget($id);
142143
$reference = $id;
143144
}
144145

145146
$this->reference = $reference;
147+
$this->routeData = null;
146148

147149
return $this;
148150
}

tests/Data/Entries/EntryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,31 @@ public static function firstChildRedirectProvider()
702702
];
703703
}
704704

705+
#[Test]
706+
public function structured_entry_uri_updates_when_custom_field_in_route_changes()
707+
{
708+
$this->setSites([
709+
'en' => ['url' => 'http://domain.com/', 'locale' => 'en_US'],
710+
]);
711+
712+
$collection = tap((new Collection)->handle('pages')->routes('{slug}/{test}'))->save();
713+
714+
$entry = tap((new Entry)->id('1')->locale('en')->collection($collection)->slug('page')->set('test', 'foo'))->save();
715+
716+
$collection->structureContents(['expects_root' => false])->save();
717+
$collection->structure()->in('en')->tree([['entry' => '1']])->save();
718+
719+
// Warm the structure caches like they would be in production.
720+
Blink::store('entry-uris')->flush();
721+
$this->assertEquals('/page/foo', $entry->uri());
722+
723+
$entry->set('test', 'bar');
724+
$entry->save();
725+
726+
Blink::store('entry-uris')->flush();
727+
$this->assertEquals('/page/bar', $entry->uri());
728+
}
729+
705730
#[Test]
706731
public function it_gets_and_sets_supplemental_data()
707732
{

tests/Data/Structures/CollectionStructureTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public function it_gets_an_entry_uri()
175175

176176
$page = $this->mock(Page::class);
177177
$page->shouldReceive('reference')->andReturn('the-entry-id');
178+
$page->shouldReceive('setEntry')->once()->andReturnSelf();
178179
$page->shouldReceive('uri')->andReturn('/the-uri-from-the-page');
179180

180181
$tree = $this->mock(Tree::class);

0 commit comments

Comments
 (0)