Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Fix static caching invalidation for multi-sites #10669

Open
wants to merge 22 commits into
base: 5.x
Choose a base branch
from
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
161 changes: 133 additions & 28 deletions src/StaticCaching/DefaultInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
use Statamic\Contracts\Entries\Collection;
use Statamic\Contracts\Entries\Entry;
use Statamic\Contracts\Forms\Form;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables;
use Statamic\Contracts\Structures\Nav;
use Statamic\Contracts\Taxonomies\Term;
use Statamic\Contracts\Structures\NavTree;
use Statamic\Facades\Site;
use Statamic\Structures\CollectionTree;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Taxonomies\LocalizedTerm;

class DefaultInvalidator implements Invalidator
{
Expand All @@ -22,58 +26,90 @@ public function __construct(Cacher $cacher, $rules = [])
$this->rules = $rules;
}

public function invalidate($item)
public function invalidate($item): void
{
if ($this->rules === 'all') {
return $this->cacher->flush();
$this->cacher->flush();

return;
}

if ($item instanceof Entry) {
$this->invalidateEntryUrls($item);
} elseif ($item instanceof Term) {
} elseif ($item instanceof LocalizedTerm) {
$this->invalidateTermUrls($item);
} elseif ($item instanceof Nav) {
$this->invalidateNavUrls($item);
} elseif ($item instanceof GlobalSet) {
} elseif ($item instanceof NavTree) {
$this->invalidateNavTreeUrls($item);
} elseif ($item instanceof Variables) {
$this->invalidateGlobalUrls($item);
} elseif ($item instanceof Collection) {
$this->invalidateCollectionUrls($item);
} elseif ($item instanceof CollectionTree) {
$this->invalidateCollectionTreeUrls($item);
} elseif ($item instanceof Asset) {
$this->invalidateAssetUrls($item);
} elseif ($item instanceof Form) {
$this->invalidateFormUrls($item);
}
}

protected function invalidateFormUrls($form)
protected function invalidateFormUrls($form): void
{
$this->cacher->invalidateUrls(
Arr::get($this->rules, "forms.{$form->handle()}.urls")
);
$rules = collect(Arr::get($this->rules, "forms.{$form->handle()}.urls"));

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

Site::all()->each(function ($site) use ($rules) {
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl(Str::removeRight($site->url(), '/').Str::ensureLeft($rule, '/')));
});
}

protected function invalidateAssetUrls($asset)
protected function invalidateAssetUrls($asset): void
{
$this->cacher->invalidateUrls(
Arr::get($this->rules, "assets.{$asset->container()->handle()}.urls")
);
$rules = collect(Arr::get($this->rules, "assets.{$asset->container()->handle()}.urls"));

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

Site::all()->each(function ($site) use ($rules) {
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl(Str::removeRight($site->url(), '/').Str::ensureLeft($rule, '/')));
});
}

protected function invalidateEntryUrls($entry)
protected function invalidateEntryUrls($entry): void
{
$rules = collect(Arr::get($this->rules, "collections.{$entry->collectionHandle()}.urls"));

$entry->descendants()->merge([$entry])->each(function ($entry) {
if (! $entry->isRedirect() && $url = $entry->absoluteUrl()) {
$this->cacher->invalidateUrl(...$this->splitUrlAndDomain($url));
}
});

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

$this->cacher->invalidateUrls(
Arr::get($this->rules, "collections.{$entry->collectionHandle()}.urls")
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->map(fn (string $rule) => Str::removeRight($entry->site()->url(), '/').Str::ensureLeft($rule, '/'))->values()->all()
);
}

protected function invalidateTermUrls($term)
protected function invalidateTermUrls($term): void
{
$rules = collect(Arr::get($this->rules, "taxonomies.{$term->taxonomyHandle()}.urls"));

if ($url = $term->absoluteUrl()) {
$this->cacher->invalidateUrl(...$this->splitUrlAndDomain($url));

Expand All @@ -84,37 +120,106 @@ protected function invalidateTermUrls($term)
});
}

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

$this->cacher->invalidateUrls(
Arr::get($this->rules, "taxonomies.{$term->taxonomyHandle()}.urls")
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->map(fn (string $rule) => Str::removeRight($term->site()->url(), '/').Str::ensureLeft($rule, '/'))->values()->all()
);
}

protected function invalidateNavUrls($nav)
protected function invalidateNavUrls($nav): void
{
$rules = collect(Arr::get($this->rules, "navigation.{$nav->handle()}.urls"));

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

$nav->sites()->each(function (string $site) use ($rules) {
$this->cacher->invalidateUrls(
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->map(fn (string $rule) => Str::removeRight(Site::get($site)->url(), '/').Str::ensureLeft($rule, '/'))->values()->all()
);
});
}

protected function invalidateNavTreeUrls($tree): void
{
$rules = collect(Arr::get($this->rules, "navigation.{$tree->structure()->handle()}.urls"));

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

$this->cacher->invalidateUrls(
Arr::get($this->rules, "navigation.{$nav->handle()}.urls")
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->map(fn (string $rule) => Str::removeRight($tree->site()->url(), '/').Str::ensureLeft($rule, '/'))->values()->all()
);
}

protected function invalidateGlobalUrls($set)
protected function invalidateGlobalUrls($variables): void
{
$rules = collect(Arr::get($this->rules, "globals.{$variables->globalSet()->handle()}.urls"));

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

$this->cacher->invalidateUrls(
Arr::get($this->rules, "globals.{$set->handle()}.urls")
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->map(fn (string $rule) => Str::removeRight($variables->site()->url(), '/').Str::ensureLeft($rule, '/'))->values()->all()
);
}

protected function invalidateCollectionUrls($collection)
protected function invalidateCollectionUrls($collection): void
{
if ($url = $collection->absoluteUrl()) {
$this->cacher->invalidateUrl(...$this->splitUrlAndDomain($url));
}
$rules = collect(Arr::get($this->rules, "collections.{$collection->handle()}.urls"));

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

$collection->sites()->each(function (string $site) use (&$collection, $rules) {
if ($url = $collection->absoluteUrl($site)) {
$this->cacher->invalidateUrl(...$this->splitUrlAndDomain($url));
}

$this->cacher->invalidateUrls(
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->map(fn (string $rule) => Str::removeRight(Site::get($site)->url(), '/').Str::ensureLeft($rule, '/'))->values()->all()
);
});
}

protected function invalidateCollectionTreeUrls($tree): void
{
$rules = collect(Arr::get($this->rules, "collections.{$tree->collection()->handle()}.urls"));

$rules
->filter(fn (string $rule) => $this->isAbsoluteUrl($rule))
->each(fn (string $rule) => $this->cacher->invalidateUrl($rule));

$this->cacher->invalidateUrls(
Arr::get($this->rules, "collections.{$collection->handle()}.urls")
$rules
->reject(fn (string $rule) => $this->isAbsoluteUrl($rule))
->map(fn (string $rule) => Str::removeRight($tree->site()->url(), '/').Str::ensureLeft($rule, '/'))->values()->all()
);
}

private function splitUrlAndDomain(string $url)
private function isAbsoluteUrl(string $url): bool
{
return isset(parse_url($url)['scheme']);
}

private function splitUrlAndDomain(string $url): array
{
$parsed = parse_url($url);

Expand Down
22 changes: 11 additions & 11 deletions src/StaticCaching/Invalidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
use Statamic\Events\EntrySaved;
use Statamic\Events\FormDeleted;
use Statamic\Events\FormSaved;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\LocalizedTermDeleted;
use Statamic\Events\LocalizedTermSaved;
use Statamic\Events\NavDeleted;
use Statamic\Events\NavSaved;
use Statamic\Events\NavTreeDeleted;
use Statamic\Events\NavTreeSaved;
use Statamic\Events\TermDeleted;
use Statamic\Events\TermSaved;
use Statamic\Facades\Form;

class Invalidate implements ShouldQueue
Expand All @@ -32,10 +32,10 @@ class Invalidate implements ShouldQueue
AssetDeleted::class => 'invalidateAsset',
EntrySaved::class => 'invalidateEntry',
EntryDeleting::class => 'invalidateEntry',
TermSaved::class => 'invalidateTerm',
TermDeleted::class => 'invalidateTerm',
GlobalSetSaved::class => 'invalidateGlobalSet',
GlobalSetDeleted::class => 'invalidateGlobalSet',
LocalizedTermSaved::class => 'invalidateTerm',
LocalizedTermDeleted::class => 'invalidateTerm',
GlobalVariablesSaved::class => 'invalidateGlobalSet',
GlobalVariablesDeleted::class => 'invalidateGlobalSet',
NavSaved::class => 'invalidateNav',
NavDeleted::class => 'invalidateNav',
FormSaved::class => 'invalidateForm',
Expand Down Expand Up @@ -77,7 +77,7 @@ public function invalidateTerm($event)

public function invalidateGlobalSet($event)
{
$this->invalidator->invalidate($event->globals);
$this->invalidator->invalidate($event->variables);
}

public function invalidateNav($event)
Expand All @@ -92,12 +92,12 @@ public function invalidateForm($event)

public function invalidateCollectionByTree($event)
{
$this->invalidator->invalidate($event->tree->collection());
$this->invalidator->invalidate($event->tree);
}

public function invalidateNavByTree($event)
{
$this->invalidator->invalidate($event->tree->structure());
$this->invalidator->invalidate($event->tree);
}

public function invalidateByBlueprint($event)
Expand Down
Loading
Loading