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
72 changes: 71 additions & 1 deletion src/StaticCaching/DefaultInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

namespace Statamic\StaticCaching;

use Illuminate\Support\Carbon;
use Illuminate\Support\Collection as IlluminateCollection;
use Statamic\Contracts\Assets\Asset;
use Statamic\Contracts\Entries\Collection;
use Statamic\Contracts\Entries\Entry;
use Statamic\Contracts\Forms\Form;
use Statamic\Contracts\Globals\Variables;
use Statamic\Contracts\Routing\UrlBuilder;
use Statamic\Contracts\Structures\Nav;
use Statamic\Contracts\Structures\NavTree;
use Statamic\Facades;
use Statamic\Facades\Antlers;
use Statamic\Facades\Site;
use Statamic\Facades\URL;
use Statamic\Statamic;
use Statamic\Structures\CollectionTree;
use Statamic\Support\Arr;
use Statamic\Support\Str;
Expand All @@ -33,6 +36,11 @@ public function __construct(Cacher $cacher, $rules = [])

public function invalidate($item)
{
// Old URLs no longer resolve so they cannot be recached, only invalidated.
if ($this->refreshing && ($oldUrls = $this->getItemOldUrls($item))) {
$this->cacher->invalidateUrls($oldUrls);
}

if ($this->rules === 'all') {
$this->refreshing
? $this->cacher->refreshUrls($this->cacher->getUrls()->all())
Expand All @@ -45,7 +53,7 @@ public function invalidate($item)

$this->refreshing
? $this->cacher->refreshUrls($urls)
: $this->cacher->invalidateUrls($urls);
: $this->cacher->invalidateUrls([...$urls, ...$this->getItemOldUrls($item)]);
}

public function refresh($item)
Expand Down Expand Up @@ -93,6 +101,68 @@ protected function getItemUrls($item)
return $urls;
}

protected function getItemOldUrls($item)
{
return $item instanceof Entry ? $this->getOldEntryUrls($item) : [];
}

protected function getOldEntryUrls($entry)
{
if (! ($route = $entry->route())) {
return [];
}

// The route can reference any field, e.g. {year}/{month}/{day}/{slug}.
$original = collect(Antlers::identifiers($this->convertToAntlers($route)))
->filter(fn ($identifier) => $this->routeIdentifierIsDirty($entry, $identifier))
->mapWithKeys(fn ($identifier) => [$identifier => $this->originalRouteValue($entry, $identifier)])
->filter(fn ($value) => ! is_null($value));

if ($original->isEmpty()) {
return [];
}

$uri = app(UrlBuilder::class)->content($entry)->merge([
'parent_uri' => $entry->parent()?->uri(),
...$original->all(),
])->build($route);

$oldUrl = URL::tidy($entry->site()->absoluteUrl().'/'.$uri);

if ($oldUrl === $entry->absoluteUrl()) {
return [];
}

// Anything cached under the old URL (descendants, mounted collections) is stale too.
return [$oldUrl, Str::finish($oldUrl, '/').'*'];
}

private function routeIdentifierIsDirty($entry, $identifier)
{
return $entry->isDirty(in_array($identifier, ['year', 'month', 'day']) ? 'date' : $identifier);
}

private function originalRouteValue($entry, $identifier)
{
if (! in_array($identifier, ['year', 'month', 'day', 'date'])) {
return $entry->getOriginal($identifier);
}

if (is_null($original = $entry->getOriginal('date'))) {
return null;
}

$date = Carbon::createFromFormat('Y-m-d-Hi', $original, $entry->date()?->timezone)
->setTimezone(Statamic::displayTimezone());

return match ($identifier) {
'year' => $date->format('Y'),
'month' => $date->format('m'),
'day' => $date->format('d'),
'date' => $date,
};
}

protected function getFormUrls($form)
{
$rules = collect(Arr::get($this->rules, "forms.{$form->handle()}.urls"));
Expand Down
Loading
Loading