Skip to content

Commit

Permalink
Add sitemap generation command and update related files; modify robot…
Browse files Browse the repository at this point in the history
…s.txt and site manifest for SEO improvements
  • Loading branch information
abdessamadbettal committed Dec 14, 2024
1 parent c7431be commit 5e2bf9e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Homestead.yaml
auth.json
npm-debug.log
yarn-error.log
/public/sitemap.xml
/.fleet
/.idea
/.vscode
Expand Down
66 changes: 66 additions & 0 deletions app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Sitemap\SitemapGenerator;
use Spatie\Sitemap\Tags\Url;
use Psr\Http\Message\UriInterface;
use Illuminate\Support\Str;


class GenerateSitemap extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate-sitemap';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Crawl the site to generate a sitemap.xml file';


private array $noIndexPaths = [
'',
'/forum/*',
'/login/github',
'/user/*',
];

/**
* Execute the console command.
*/
public function handle(): void
{
SitemapGenerator::create(config('app.url'))
->shouldCrawl(function (UriInterface $url) {
return $this->shouldIndex($url->getPath());
})
->hasCrawled(function (Url $url) {
if ($this->shouldNotIndex($url->path())) {
return;
}

return $url;
})
->writeToFile(public_path('sitemap.xml'));

$this->info('Sitemap generated');
}

private function shouldNotIndex(string $path): bool
{
return Str::is($this->noIndexPaths, $path);
}

private function shouldIndex(string $path): bool
{
return ! $this->shouldNotIndex($path);
}
}
9 changes: 9 additions & 0 deletions public/browserconfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square150x150logo src="/web-app-manifest-192x192.png"/>
<TileColor>#2b5797</TileColor>
</tile>
</msapplication>
</browserconfig>
3 changes: 1 addition & 2 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
User-agent: *
Allow: /
Sitemap: https://laravel-starter.com/sitemap.xml
Disallow:
6 changes: 3 additions & 3 deletions public/site.webmanifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "MyWebSite",
"short_name": "MySite",
"name": "Laravel Starter",
"short_name": "Laravel Starter",
"icons": [
{
"src": "/web-app-manifest-192x192.png",
Expand All @@ -18,4 +18,4 @@
"theme_color": "#ffffff",
"background_color": "#ffffff",
"display": "standalone"
}
}
11 changes: 0 additions & 11 deletions public/sitemap.xml

This file was deleted.

2 changes: 2 additions & 0 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote')->hourly();

Schedule::command('generate-sitemap')->daily();

0 comments on commit 5e2bf9e

Please sign in to comment.