Laravel Sitemap v8 is here: automatic splitting, XSL stylesheets, and crawler v9 original
We just released v8 of Laravel Sitemap. This package that can generate sitemaps by crawling your entire site or by manually adding URLs. This version upgrades the underlying crawler to v9, adds a couple of nice new features, and cleans up the internals.
Let me walk you through what the package can do and what's new in v8.
Generating a sitemap by crawling
The simplest way to use the package is to point it at your site and let it crawl every page.
use Spatie\Sitemap\SitemapGenerator; SitemapGenerator::create('https://example.com')->writeToFile($path);
That's it. The generator will follow all internal links and produce a complete sitemap.xml. You can filter which URLs end up in the sitemap using the shouldCrawl callback.
SitemapGenerator::create('https://example.com') ->shouldCrawl(function (string $url) { return ! str_contains(parse_url($url, PHP_URL_PATH) ?? '', '/admin'); }) ->writeToFile($path);
Creating a sitemap manually
If you'd rather have full control, you can build the sitemap yourself.
use Carbon\Carbon; use Spatie\Sitemap\Sitemap; use Spatie\Sitemap\Tags\Url; Sitemap::create() ->add(Url::create('/home') ->setLastModificationDate(Carbon::yesterday()) ->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY) ->setPriority(0.1)) ->add(Url::create('/contact')) ->writeToFile($path);
You can also combine both approaches: let the crawler do the heavy lifting, then add extra URLs on top.
SitemapGenerator::create('https://example.com') ->getSitemap() ->add(Url::create('/extra-page')) ->writeToFile($path);
Adding models directly
If your models implement the Sitemapable interface, you can add them to the sitemap directly.
use Spatie\Sitemap\Contracts\Sitemapable; use Spatie\Sitemap\Tags\Url; class Post extends Model implements Sitemapable { public function toSitemapTag(): Url | string | array { return route('blog.post.show', $this); } }
Now you can pass a single model or an entire collection.
Sitemap::create() ->add($post) ->add(Post::all()) ->writeToFile($path);
Automatic sitemap splitting
Large sites can easily exceed the 50,000 URL limit that the sitemap protocol allows per file. New in v8, you can call maxTagsPerSitemap() on your sitemap, and the package will automatically split it into multiple files with a sitemap index.
Sitemap::create() ->maxTagsPerSitemap(10000) ->add($allUrls) ->writeToFile(public_path('sitemap.xml'));
If your sitemap contains more than 10,000 URLs, this will write sitemap_1.xml, sitemap_2.xml, etc., and a sitemap.xml index file that references them all. If your sitemap stays under the limit, it just writes a single file as usual.
XSL stylesheet support
Sitemaps are XML files, and they look pretty rough when opened in a browser. Also new in v8, you can attach an XSL stylesheet to make them human-readable.
Sitemap::create() ->setStylesheet('/sitemap.xsl') ->add(Post::all()) ->writeToFile(public_path('sitemap.xml'));
This works on both Sitemap and SitemapIndex. When combined with maxTagsPerSitemap(), the stylesheet is automatically applied to all split files and the index.
In closing
Under the hood, we've upgraded the package to use spatie/crawler v9.
You'll find the complete documentation on our docs site. The package is available on GitHub.
This is one of the many packages we've created at Spatie. If you want to support our open source work, consider picking up one of our paid products.