Skip to content

Commit

Permalink
Smart sitemap cache-ing
Browse files Browse the repository at this point in the history
  • Loading branch information
martink635 committed Nov 10, 2023
1 parent 95eaf0e commit 57adb8d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 28 deletions.
7 changes: 7 additions & 0 deletions src/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ protected function formBlueprint()
'display' => __('seotamic::general.settings_preview_domain_title'),
'instructions' => __('seotamic::general.settings_preview_domain_instructions'),
],
'headless_mode' => [
'type' => 'text',
'character_limit' => '50',
'prepend' => 'https://',
'display' => "Headless Mode",
'instructions' => "If set this domain will replace the domain for canonical and social url tags. Image links will still be linked to the CMS domain.",
],
'robots_none' => [
'type' => 'toggle',
'display' => __('seotamic::general.settings_robots_title'),
Expand Down
54 changes: 27 additions & 27 deletions src/Http/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,34 @@ public function __invoke()
$addon = Addon::get('cnj/seotamic');
abort_unless(config('seotamic.sitemap') && $addon->edition() === 'pro', 404);

$entries = Collection::all()
->flatMap(function ($collection) {
return $collection->cascade('seo') !== false && $collection->cascade('social') !== false
? $collection->queryEntries()->get()
: collect();
})
->filter(function (Entry $entry) {
return self::shouldBeIndexed($entry);
})
->map(function (Entry $entry) {
return [
'loc' => $entry->absoluteUrl(),
'lastmod' => $entry->lastModified()->toAtomString(),
'alternates' => $entry->sites()
->map(fn ($site) => $entry->in($site))
->filter(function (?Entry $entry) {
return $entry && self::shouldBeIndexed($entry);
})
->map(function (Entry $entry) {
return array(
'lang' => $entry->locale,
'href' => $entry->absoluteUrl()
);
})
];
});
$content = Cache::rememberForever('seotamic_sitemap', function () {
$entries = Collection::all()
->flatMap(function ($collection) {
return $collection->cascade('seo') !== false && $collection->cascade('social') !== false
? $collection->queryEntries()->get()
: collect();
})
->filter(function (Entry $entry) {
return self::shouldBeIndexed($entry);
})
->map(function (Entry $entry) {
return [
'loc' => $entry->absoluteUrl(),
'lastmod' => $entry->lastModified()->toAtomString(),
'alternates' => $entry->sites()
->map(fn ($site) => $entry->in($site))
->filter(function (?Entry $entry) {
return $entry && self::shouldBeIndexed($entry);
})
->map(function (Entry $entry) {
return array(
'lang' => $entry->locale,
'href' => $entry->absoluteUrl()
);
})
];
});

$content = Cache::remember('seotamic_sitemap', 1, function () use ($entries) {
return view('seotamic::sitemap', [
'header' => '<?xml version="1.0" encoding="UTF-8"?>',
'entries' => $entries,
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class ServiceProvider extends AddonServiceProvider
{
protected $subscribe = [Subscriber::class];
protected $subscribe = [Subscriber::class, SitemapSubscriber::class];

protected $commands = [MigrateCommand::class];

Expand Down
35 changes: 35 additions & 0 deletions src/SitemapSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Cnj\Seotamic;

use Statamic\Events\EntrySaved;
use Statamic\Events\EntryCreated;
use Statamic\Events\EntryDeleted;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Facades\Cache;

class SitemapSubscriber
{
/**
* Register the listeners for the subscriber.
*
* @return array<string, string>
*/
public function subscribe(Dispatcher $events): array
{
return [
EntrySaved::class => 'flushSitemapCache',
EntryCreated::class => 'flushSitemapCache',
EntryDeleted::class => 'flushSitemapCache',
];
}

/**
* Flush the sitemap cache
*
*/
public function flushSitemapCache()
{
Cache::forget('seotamic_sitemap');
}
}

0 comments on commit 57adb8d

Please sign in to comment.