Skip to content

Commit 6a1b5d4

Browse files
committed
feat: [LAR-85] update sitemap
1 parent eacf7b9 commit 6a1b5d4

File tree

9 files changed

+117
-55
lines changed

9 files changed

+117
-55
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ composer.phar
2020
/public/hot
2121
/public/storage
2222
/public/media
23-
/public/sitemap.xml
23+
/public/**/*.xml
2424
/storage/*.key
2525
/storage/framework/cache
2626
.env

app/Console/Commands/GenerateSitemap.php

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,45 @@
44

55
namespace App\Console\Commands;
66

7-
use App\Models\Article;
87
use Illuminate\Console\Command;
9-
use Illuminate\Support\Str;
10-
use Psr\Http\Message\UriInterface;
118
use Spatie\Sitemap\Sitemap;
12-
use Spatie\Sitemap\SitemapGenerator;
9+
use Spatie\Sitemap\SitemapIndex;
1310
use Spatie\Sitemap\Tags\Url;
1411

1512
final class GenerateSitemap extends Command
1613
{
1714
protected $signature = 'sitemap:generate';
1815

19-
protected $description = 'Crawl the site to generate a sitemap.xml file';
20-
21-
/**
22-
* @var array|string[]
23-
*/
24-
private array $noIndexPaths = [
25-
'',
26-
'/forum/*',
27-
'/user/*',
28-
];
16+
protected $description = 'Generate the sitemap';
2917

3018
public function handle(): void
3119
{
32-
SitemapGenerator::create(config('app.url'))
33-
->shouldCrawl(fn (UriInterface $url) => $this->shouldIndex($url->getPath()))
34-
->hasCrawled(function (Url $url) {
35-
if ($this->shouldNotIndex($url->path())) {
36-
return;
37-
}
38-
39-
return $url;
40-
})
41-
->writeToFile(public_path('sitemap.xml'));
42-
4320
Sitemap::create()
44-
->add(Article::query()->whereNotNull('approved_at')->get())
45-
->writeToFile(public_path('sitemap.xml'));
46-
}
47-
48-
private function shouldNotIndex(string $path): bool
49-
{
50-
return Str::is($this->noIndexPaths, $path);
51-
}
52-
53-
private function shouldIndex(string $path): bool
54-
{
55-
return ! $this->shouldNotIndex($path);
21+
->add(
22+
Url::create(route('home'))
23+
->setLastModificationDate(now()->subMinutes(10))
24+
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
25+
->setPriority(0.5)
26+
)
27+
->add(
28+
Url::create(route('sponsors'))
29+
->setLastModificationDate(now()->subMinutes(10))
30+
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
31+
->setPriority(0.5)
32+
)
33+
->add(
34+
Url::create(route('about'))
35+
->setLastModificationDate(now()->subMinutes(10))
36+
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
37+
->setPriority(0.5)
38+
)
39+
->writeToFile(public_path('sitemaps/base_sitemap.xml'));
40+
41+
$sitemap = SitemapIndex::create()
42+
->add('/sitemaps/base_sitemap.xml')
43+
->add('/sitemaps/discussion_sitemap.xml')
44+
->add('/sitemaps/blog_sitemap.xml');
45+
46+
$sitemap->writeToFile(public_path('sitemap.xml'));
5647
}
5748
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Console\Commands\Sitemap;
6+
7+
use App\Models\Article;
8+
use Illuminate\Console\Command;
9+
use Spatie\Sitemap\Sitemap;
10+
11+
final class GenerateArticlesSitemapCommand extends Command
12+
{
13+
protected $signature = 'sitemap:blog-generate';
14+
15+
protected $description = 'Generate the articles sitemaps.';
16+
17+
public function handle(): void
18+
{
19+
$sitemap = Sitemap::create();
20+
21+
Article::query()->whereNotNull('approved_at')->each(function ($article) use ($sitemap): void {
22+
$sitemap->add($article); // @phpstan-ignore-line
23+
});
24+
25+
$sitemap->writeToFile(public_path('sitemaps/blog_sitemap.xml'));
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Console\Commands\Sitemap;
6+
7+
use App\Models\Discussion;
8+
use Illuminate\Console\Command;
9+
use Spatie\Sitemap\Sitemap;
10+
11+
final class GenerateDiscussionsSitemapCommand extends Command
12+
{
13+
protected $signature = 'sitemap:discussion-generate';
14+
15+
protected $description = 'Generate the discussions sitemaps.';
16+
17+
public function handle(): void
18+
{
19+
$sitemap = Sitemap::create();
20+
21+
Discussion::query()->whereHas('replies')->each(function ($discussion) use ($sitemap): void {
22+
$sitemap->add($discussion); // @phpstan-ignore-line
23+
});
24+
25+
$sitemap->writeToFile(public_path('sitemaps/discussion_sitemap.xml'));
26+
}
27+
}

app/Livewire/Components/Discussion/Comments.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function comments(): Collection
7474
{
7575
$replies = collect();
7676

77+
// @phpstan-ignore-next-line
7778
foreach ($this->discussion->replies->load(['allChildReplies', 'user']) as $reply) {
7879
/** @var Reply $reply */
7980
if ($reply->allChildReplies->isNotEmpty()) {

app/Models/Article.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
* @property int $user_id
3737
* @property string | null $locale
3838
* @property-read User $user
39-
* @property \Carbon\Carbon | null $published_at
40-
* @property \Carbon\Carbon | null $submitted_at
41-
* @property \Carbon\Carbon | null $approved_at
42-
* @property \Carbon\Carbon | null $shared_at
43-
* @property \Carbon\Carbon | null $declined_at
44-
* @property \Carbon\Carbon | null $sponsored_at
45-
* @property \Carbon\Carbon $created_at
46-
* @property \Carbon\Carbon $updated_at
39+
* @property Carbon | null $published_at
40+
* @property Carbon | null $submitted_at
41+
* @property Carbon | null $approved_at
42+
* @property Carbon | null $shared_at
43+
* @property Carbon | null $declined_at
44+
* @property Carbon | null $sponsored_at
45+
* @property Carbon $created_at
46+
* @property Carbon $updated_at
4747
* @property \Illuminate\Database\Eloquent\Collection | Tag[] $tags
4848
*/
4949
final class Article extends Model implements HasMedia, ReactableInterface, Sitemapable, Viewable
@@ -99,6 +99,14 @@ public function newEloquentBuilder($query): ArticleQueryBuilder
9999
return new ArticleQueryBuilder($query);
100100
}
101101

102+
public function toSitemapTag(): Url
103+
{
104+
return Url::create(route('articles.show', $this))
105+
->setLastModificationDate(Carbon::create($this->updated_at)) // @phpstan-ignore-line
106+
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
107+
->setPriority(0.5);
108+
}
109+
102110
public function excerpt(int $limit = 110): string
103111
{
104112
return Str::limit(strip_tags((string) md_to_html($this->body)), $limit);
@@ -240,12 +248,4 @@ public function delete(): ?bool
240248

241249
return parent::delete();
242250
}
243-
244-
public function toSitemapTag(): Url|string|array
245-
{
246-
return Url::create(route('articles.show', $this))
247-
->setLastModificationDate(Carbon::create($this->updated_at))
248-
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
249-
->setPriority(0.1);
250-
}
251251
}

app/Models/Discussion.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use Illuminate\Database\Eloquent\Model;
2727
use Illuminate\Support\Collection;
2828
use Illuminate\Support\Str;
29+
use Spatie\Sitemap\Contracts\Sitemapable;
30+
use Spatie\Sitemap\Tags\Url;
2931

3032
/**
3133
* @property-read int $id
@@ -41,8 +43,9 @@
4143
* @property Carbon $updated_at
4244
* @property User $user
4345
* @property Collection | SpamReport[] $spamReports
46+
* @property Collection | Reply[] $replies
4447
*/
45-
final class Discussion extends Model implements ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable
48+
final class Discussion extends Model implements ReactableInterface, ReplyInterface, Sitemapable, SpamReportableContract, SubscribeInterface, Viewable
4649
{
4750
use HasAuthor;
4851
use HasFactory;
@@ -107,6 +110,14 @@ public function excerpt(int $limit = 110): string
107110
return Str::limit(strip_tags((string) md_to_html($this->body)), $limit);
108111
}
109112

113+
public function toSitemapTag(): Url
114+
{
115+
return Url::create(route('discussions.show', $this))
116+
->setLastModificationDate(Carbon::create($this->updated_at)) // @phpstan-ignore-line
117+
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
118+
->setPriority(0.5);
119+
}
120+
110121
public function isPinned(): bool
111122
{
112123
return $this->is_pinned;

public/sitemaps/.gitkeep

Whitespace-only changes.

routes/console.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
use Illuminate\Foundation\Inspiring;
66
use Illuminate\Support\Facades\Artisan;
7+
use Illuminate\Support\Facades\Schedule;
78

89
Artisan::command('inspire', function (): void {
910
$this->comment(Inspiring::quote());
1011
})->purpose('Display an inspiring quote')->hourly();
12+
13+
Schedule::command('sitemap:blog-generate')->dailyAt('01:00');
14+
Schedule::command('sitemap:discussion-generate')->dailyAt('01:10');
15+
Schedule::command('sitemap:generate')->dailyAt('02:00');

0 commit comments

Comments
 (0)