From dbd58c33a644bc8f5394a755dbe44081aa69ca39 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sun, 18 Feb 2024 23:02:02 +0100 Subject: [PATCH] fix: fix sitemap again (#47) --- app/Console/Commands/CustomCrawlProfile.php | 20 ++++++++ app/Console/Commands/GenerateSitemap.php | 41 ++++++++------- app/Console/Commands/SetupApplication.php | 23 ++++++--- app/Console/Commands/SubmitSitemap.php | 8 ++- app/Http/Kernel.php | 2 +- app/Http/Middleware/TrackPageview.php | 29 +++++++++++ app/Models/Name.php | 11 ++-- config/sitemap.php | 57 +++++++++++++++++++++ public/robots.txt | 2 +- public/{ => sitemap}/sitemap.xml | 0 10 files changed, 158 insertions(+), 35 deletions(-) create mode 100644 app/Console/Commands/CustomCrawlProfile.php create mode 100644 app/Http/Middleware/TrackPageview.php create mode 100644 config/sitemap.php rename public/{ => sitemap}/sitemap.xml (100%) diff --git a/app/Console/Commands/CustomCrawlProfile.php b/app/Console/Commands/CustomCrawlProfile.php new file mode 100644 index 0000000..c6a5d1e --- /dev/null +++ b/app/Console/Commands/CustomCrawlProfile.php @@ -0,0 +1,20 @@ +getQuery() !== '' + || Str::isMatch('/\/prenoms\/\d+\/\w+/', $url->getPath())) { + return false; + } + + return true; + } +} diff --git a/app/Console/Commands/GenerateSitemap.php b/app/Console/Commands/GenerateSitemap.php index 6e3da18..71cb7ea 100644 --- a/app/Console/Commands/GenerateSitemap.php +++ b/app/Console/Commands/GenerateSitemap.php @@ -7,12 +7,14 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; use Spatie\Sitemap\Sitemap; +use Spatie\Sitemap\SitemapGenerator; use Spatie\Sitemap\SitemapIndex; use Spatie\Sitemap\Tags\Sitemap as SitemapTag; -use Spatie\Sitemap\Tags\Url; class GenerateSitemap extends Command { + public const PREFIX_PATH = 'sitemap'; + /** * The name and signature of the console command. * @@ -34,30 +36,31 @@ public function handle(): void { $sitemapIndex = SitemapIndex::create(); + SitemapGenerator::create(config('app.url')) + ->writeToFile(public_path(static::PREFIX_PATH . '/sitemap_00.xml')); + + $sitemapIndex->add(SitemapTag::create(url(static::PREFIX_PATH . '/sitemap_00.xml'))); + + $this->sitemap_names($sitemapIndex); + + $sitemapIndex->writeToFile(public_path(static::PREFIX_PATH . '/sitemap.xml')); + } + + /** + * Get names sitemap. + */ + private function sitemap_names(SitemapIndex $sitemapIndex): void + { Name::where('name', '!=', '_PRENOMS_RARES') ->chunkById(2000, function (Collection $names, int $key) use ($sitemapIndex) { - $file = 'sitemap_' . Str::padLeft($key, 2, '0') . '.xml'; - $sitemap = Sitemap::create(); + $file = static::PREFIX_PATH . '/sitemap_' . Str::padLeft("$key", 2, '0') . '.xml'; - $names->each(function (Name $name) use ($sitemap) { - $sitemap->add( - Url::create(route('name.show', [ - 'id' => $name->id, - 'name' => Str::lower($name->name), - ])) - ->setPriority(0.9) - ->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY) - ->setLastModificationDate($name->updated_at) - ); - }); - - $sitemap->writeToFile(public_path($file)); - $sitemap = null; + Sitemap::create() + ->add($names) + ->writeToFile(public_path($file)); $sitemapIndex->add(SitemapTag::create(url($file))); }, 'id'); - - $sitemapIndex->writeToFile(public_path('sitemap.xml')); } } diff --git a/app/Console/Commands/SetupApplication.php b/app/Console/Commands/SetupApplication.php index 6d52112..d0a47de 100644 --- a/app/Console/Commands/SetupApplication.php +++ b/app/Console/Commands/SetupApplication.php @@ -34,14 +34,21 @@ class SetupApplication extends Command public function handle(): void { if ($this->confirmToProceed()) { - $this->resetCache(); - $this->clearConfig(); - $this->symlink(); - $this->migrate(); - $this->cacheConfig(); - $this->scout(); - $this->sitemap(); - $this->cloudflare(); + try { + $this->artisan('✓ Maintenance mode: on', 'down', [ + '--retry' => '10', + ]); + $this->resetCache(); + $this->clearConfig(); + $this->symlink(); + $this->migrate(); + $this->cacheConfig(); + $this->scout(); + $this->sitemap(); + $this->cloudflare(); + } finally { + $this->artisan('✓ Maintenance mode: off', 'up'); + } } } diff --git a/app/Console/Commands/SubmitSitemap.php b/app/Console/Commands/SubmitSitemap.php index 79c6f4f..a1960b5 100644 --- a/app/Console/Commands/SubmitSitemap.php +++ b/app/Console/Commands/SubmitSitemap.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; class SubmitSitemap extends Command @@ -29,8 +30,11 @@ class SubmitSitemap extends Command */ public function handle() { - $sitemapUrl = Str::finish(config('app.url'), '/') . 'sitemap.xml'; + $sitemapUrl = Str::finish(config('app.url'), '/') . 'sitemap/sitemap.xml'; $url = "https://www.google.com/webmasters/sitemaps/ping?sitemap=$sitemapUrl"; - Http::get($url); + + Log::debug("Submitting sitemap to Google: $url"); + Http::get($url) + ->throw(); } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index ee981da..7a7adb9 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -35,7 +35,7 @@ class Kernel extends HttpKernel \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, - \Pirsch\Http\Middleware\TrackPageview::class, + \App\Http\Middleware\TrackPageview::class, ], 'api' => [ diff --git a/app/Http/Middleware/TrackPageview.php b/app/Http/Middleware/TrackPageview.php new file mode 100644 index 0000000..3af1e8f --- /dev/null +++ b/app/Http/Middleware/TrackPageview.php @@ -0,0 +1,29 @@ +isDownForMaintenance()) { + return $response; + } + + Pirsch::track(); + + return $response; + } +} diff --git a/app/Models/Name.php b/app/Models/Name.php index 6193ee8..2f1fb6a 100644 --- a/app/Models/Name.php +++ b/app/Models/Name.php @@ -2,11 +2,11 @@ namespace App\Models; -use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Support\Str; use Laravel\Scout\Searchable; use Spatie\Sitemap\Contracts\Sitemapable; use Spatie\Sitemap\Tags\Url; @@ -72,10 +72,13 @@ public function lists(): BelongsToMany public function toSitemapTag(): Url|string|array { - return Url::create(route('name.show', $this)) - ->setLastModificationDate(Carbon::create($this->updated_at)) + return Url::create(route('name.show', [ + 'id' => $this->id, + 'name' => Str::lower($this->name), + ])) + ->setPriority(0.9) ->setChangeFrequency(Url::CHANGE_FREQUENCY_MONTHLY) - ->setPriority(0.1); + ->setLastModificationDate($this->updated_at); } public function getNoteForUser(): ?string diff --git a/config/sitemap.php b/config/sitemap.php new file mode 100644 index 0000000..3914cb1 --- /dev/null +++ b/config/sitemap.php @@ -0,0 +1,57 @@ + [ + + /* + * Whether or not cookies are used in a request. + */ + RequestOptions::COOKIES => true, + + /* + * The number of seconds to wait while trying to connect to a server. + * Use 0 to wait indefinitely. + */ + RequestOptions::CONNECT_TIMEOUT => 10, + + /* + * The timeout of the request in seconds. Use 0 to wait indefinitely. + */ + RequestOptions::TIMEOUT => 10, + + /* + * Describes the redirect behavior of a request. + */ + RequestOptions::ALLOW_REDIRECTS => false, + ], + + /* + * The sitemap generator can execute JavaScript on each page so it will + * discover links that are generated by your JS scripts. This feature + * is powered by headless Chrome. + */ + 'execute_javascript' => false, + + /* + * The package will make an educated guess as to where Google Chrome is installed. + * You can also manually pass its location here. + */ + 'chrome_binary_path' => null, + + /* + * The sitemap generator uses a CrawlProfile implementation to determine + * which urls should be crawled for the sitemap. + */ + 'crawl_profile' => CustomCrawlProfile::class, + +]; diff --git a/public/robots.txt b/public/robots.txt index bfa8dd7..0ee2e08 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -1,3 +1,3 @@ User-agent: * Disallow: -Sitemap: /sitemap.xml +Sitemap: /sitemap/sitemap.xml diff --git a/public/sitemap.xml b/public/sitemap/sitemap.xml similarity index 100% rename from public/sitemap.xml rename to public/sitemap/sitemap.xml