Skip to content

Commit

Permalink
Sitemaps (laravel#255)
Browse files Browse the repository at this point in the history
* Add sitemaps

* Remove published vendor files

* Remove published sitemap config

* Remove unused imports
  • Loading branch information
jbrooksuk authored Aug 23, 2022
1 parent 385f28c commit 7a4c162
Show file tree
Hide file tree
Showing 5 changed files with 972 additions and 331 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/node_modules
/public/hot
/public/storage
/public/sitemap*.xml
/storage/*.key
/vendor
.DS_Store
Expand Down
62 changes: 62 additions & 0 deletions app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Console\Commands;

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

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

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate the sitemap.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
SitemapGenerator::create(config('app.url'))
->shouldCrawl(function (UriInterface $url) {
// Crawl everything without "docs" in the path, as we'll crawl the docs separately...
return ! Str::contains($url->getPath(), 'docs');
})
->hasCrawled(function (Url $url) {
if ($url->segment(1) === 'team') {
$url->setPriority(0.5);
}

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

SitemapGenerator::create(config('app.url').'/docs/'.DEFAULT_VERSION)
->shouldCrawl(function (UriInterface $url) {
return Str::contains($url->getPath(), 'docs');
})
->writeToFile(public_path('sitemap_docs.xml'));

SitemapIndex::create()
->add('sitemap_pages.xml')
->add('sitemap_docs.xml')
->writeToFile(public_path('sitemap.xml'));

return 0;
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();

$schedule->command('sitemap:generate')->daily();
}

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"fideloper/proxy": "^4.2",
"laravel/framework": "^8.0",
"laravel/tinker": "^2.2",
"spatie/laravel-sitemap": "^5.9",
"symfony/browser-kit": "^5.0",
"torchlight/torchlight-commonmark": "^0.5.2",
"torchlight/torchlight-laravel": "^0.5.7"
Expand Down
Loading

0 comments on commit 7a4c162

Please sign in to comment.