Skip to content

Commit

Permalink
fix: fix sitemap again (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Feb 18, 2024
1 parent 5976791 commit dbd58c3
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 35 deletions.
20 changes: 20 additions & 0 deletions app/Console/Commands/CustomCrawlProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Console\Commands;

use Illuminate\Support\Str;
use Psr\Http\Message\UriInterface;
use Spatie\Crawler\CrawlProfiles\CrawlProfile;

class CustomCrawlProfile extends CrawlProfile
{
public function shouldCrawl(UriInterface $url): bool
{
if ($url->getQuery() !== ''
|| Str::isMatch('/\/prenoms\/\d+\/\w+/', $url->getPath())) {
return false;
}

return true;
}
}
41 changes: 22 additions & 19 deletions app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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'));
}
}
23 changes: 15 additions & 8 deletions app/Console/Commands/SetupApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions app/Console/Commands/SubmitSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
29 changes: 29 additions & 0 deletions app/Http/Middleware/TrackPageview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Pirsch\Facades\Pirsch;

class TrackPageview
{
public function handle(Request $request, Closure $next): mixed
{
$response = $next($request);

if ($response instanceof RedirectResponse) {
return $response;
}

// If app is down, don't track pageviews.
if (app()->isDownForMaintenance()) {
return $response;
}

Pirsch::track();

return $response;
}
}
11 changes: 7 additions & 4 deletions app/Models/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions config/sitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

use App\Console\Commands\CustomCrawlProfile;
use GuzzleHttp\RequestOptions;

return [

/*
* These options will be passed to GuzzleHttp\Client when it is created.
* For in-depth information on all options see the Guzzle docs:
*
* http://docs.guzzlephp.org/en/stable/request-options.html
*/
'guzzle_options' => [

/*
* 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,

];
2 changes: 1 addition & 1 deletion public/robots.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
User-agent: *
Disallow:
Sitemap: /sitemap.xml
Sitemap: /sitemap/sitemap.xml
File renamed without changes.

0 comments on commit dbd58c3

Please sign in to comment.