Skip to content
Open
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
15 changes: 10 additions & 5 deletions src/Stache/Indexes/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class Index
protected $name;
protected $items = [];
protected $loaded = false;
private static ?string $currentlyLoading = null;
private static array $loadingStack = [];

public function __construct($store, $name)
{
Expand Down Expand Up @@ -66,9 +66,9 @@ public function load()
}

$loadingKey = $this->store->key().'/'.$this->name;
$currentlyLoadingThis = static::$currentlyLoading === $loadingKey;
$currentlyLoadingThis = in_array($loadingKey, static::$loadingStack);

static::$currentlyLoading = $loadingKey;
static::$loadingStack[] = $loadingKey;

$this->loaded = true;

Expand All @@ -86,7 +86,7 @@ public function load()

$this->store->cacheIndexUsage($this);

static::$currentlyLoading = null;
array_pop(static::$loadingStack);

return $this;
}
Expand Down Expand Up @@ -163,6 +163,11 @@ public function clear()

public static function currentlyLoading()
{
return static::$currentlyLoading;
return end(static::$loadingStack) ?: null;
}

public static function isLoading(): bool
{
return ! empty(static::$loadingStack);
}
}
4 changes: 1 addition & 3 deletions src/Stache/Stores/CollectionEntriesStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ protected function getCachedItem($key)
return null;
}

$isLoadingIds = Index::currentlyLoading() === $this->key().'/id';

if (! $isLoadingIds && $this->shouldBlinkEntryUris && ($uri = $this->resolveIndex('uri')->load()->get($entry->id()))) {
if (! Index::isLoading() && $this->shouldBlinkEntryUris && ($uri = $this->resolveIndex('uri')->load()->get($entry->id()))) {
Blink::store('entry-uris')->put($entry->id(), $uri);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Stache/Stores/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ public function paths()
return $isDuplicate ?? false;
});

$items->each(fn ($item) => $this->cacheItem($item['item']));

$paths = $items->pluck('path', 'key');

$this->cachePaths($paths);
Expand Down
78 changes: 78 additions & 0 deletions tests/Stache/ColdStacheUriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Tests\Stache;

use Facades\Tests\Factories\EntryFactory;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Facades\Stache;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class ColdStacheUriTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

private function simulateColdStache(): void
{
Stache::clear();
Blink::flush();
}

#[Test]
public function entries_have_uris_on_cold_stache_with_single_site_structured_collection()
{
$collection = Collection::make('pages')
->routes('{parent_uri}/{slug}')
->structureContents(['root' => true]);
$collection->save();

EntryFactory::id('alfa-id')->collection('pages')->slug('alfa')->data(['title' => 'Alfa'])->create();
EntryFactory::id('bravo-id')->collection('pages')->slug('bravo')->data(['title' => 'Bravo'])->create();

$this->simulateColdStache();

// Loading a non-URI index first triggers re-entrant URI index loading via getCachedItem().
Stache::store('entries')->store('pages')->index('site')->load();

$entries = Entry::query()
->where('collection', 'pages')
->whereNotNull('uri')
->whereStatus('published')
->get();

$this->assertGreaterThanOrEqual(2, $entries->count(), 'Expected at least 2 entries with URI on cold stache');
}

#[Test]
public function entries_have_uris_on_cold_stache_with_multisite_structured_collection()
{
$this->setSites([
'en' => ['url' => 'http://localhost/', 'locale' => 'en_US'],
'fr' => ['url' => 'http://localhost/fr/', 'locale' => 'fr_FR'],
]);

$collection = Collection::make('pages')
->routes('{parent_uri}/{slug}')
->structureContents(['root' => true])
->sites(['en', 'fr']);
$collection->save();

EntryFactory::id('alfa-id')->locale('en')->collection('pages')->slug('alfa')->data(['title' => 'Alfa'])->create();
EntryFactory::id('bravo-id')->locale('fr')->collection('pages')->slug('bravo')->origin('alfa-id')->data(['title' => 'Bravo'])->create();

$this->simulateColdStache();

Stache::store('entries')->store('pages')->index('site')->load();

$entries = Entry::query()
->where('collection', 'pages')
->whereNotNull('uri')
->whereStatus('published')
->get();

$this->assertGreaterThanOrEqual(1, $entries->count(), 'Expected at least 1 entry with URI on cold stache');
}
}