- How do I generate a sitemap by crawling my Laravel site automatically?
- Use `SitemapGenerator::create('https://your-site.com')->writeToFile(public_path('sitemap.xml'))`. The crawler respects Laravel routes and middleware, but limit concurrency/depth to avoid server overload. For large sites, split into multiple sitemaps with `->setMaxItemsPerSitemap()`.
- Can I manually define URLs in the sitemap instead of crawling?
- Yes. Use `Sitemap::create()->add(Url::create('/path')->setLastModificationDate(Carbon::now()))->writeToFile()`. This is ideal for static pages or when you need precise control over metadata like priority or change frequency.
- Does this package support Laravel 10 and newer versions?
- Yes, `spatie/laravel-sitemap` is actively maintained and supports Laravel 9+ (including 10.x). Check the [GitHub releases](https://github.com/spatie/laravel-sitemap/releases) for version-specific notes. Always test in a staging environment first.
- How do I add sitemap URLs from Eloquent models (e.g., BlogPost)?
- Implement the `Sitemapable` interface in your model: `class BlogPost implements Sitemapable { public function toSitemapTag(): Url { return Url::create(route('posts.show', $this))->setLastModificationDate($this->updated_at); } }`. The crawler will auto-discover these during generation.
- Will this package work for JavaScript-rendered pages (SPAs or dynamic content)?
- Yes, but you’ll need `spatie/browsershot` for headless Chrome rendering. Install it separately (`composer require spatie/browsershot`) and configure the crawler with `->setCrawlerOptions(['execute_javascript' => true])`. Note the ~50MB binary dependency may require Docker or Chrome setup.
- How can I split a large sitemap into multiple files (index sitemap)?
- Use `SitemapGenerator::create()->setMaxItemsPerSitemap(5000)->writeToDisk()`. This auto-generates an index file (`sitemap-index.xml`) linking to sub-sitemaps. For async processing, dispatch the command via Laravel queues: `dispatch(new GenerateSitemap)->delay(now()->addHours(1));`.
- Does this package support multilingual sitemaps (hreflang tags)?
- Yes. After generating a sitemap, add alternate links with `->addAlternate('https://fr.example.com/page', 'fr')`. For news articles, use `->addNews()` with publication dates. Example: `$url->addAlternate('https://es.example.com/post', 'es')->addNews(Carbon::now(), 'EN', 'Tech News');`.
- How do I test sitemap generation in CI/CD or unit tests?
- Mock the generator in PHPUnit with `Sitemap::fake()` (if available) or assert file output: `$this->assertFileExists(public_path('sitemap.xml'));`. For crawling tests, use `SitemapGenerator::create('http://test-site.test')->setCrawlerOptions(['max_depth' => 1])->writeToFile()`. Test in a fresh Laravel environment with `php artisan sitemap:generate --env=testing`.
- What are the performance implications of crawling in production?
- Crawling can strain server resources. Mitigate this by: 1) Running during off-peak hours (use Laravel scheduling), 2) Limiting depth/concurrency (`->depth(2)->setConcurrency(5)`), 3) Offloading to queues, or 4) generating sitemaps in staging and deploying pre-built files. Monitor CPU/memory usage during pilot tests.
- Are there alternatives to spatie/laravel-sitemap for Laravel sitemaps?
- Yes. For simpler needs, consider `spatie/laravel-sitemap-generator` (older version) or `nuwave/lighthouse` (GraphQL-focused). For more control, use `kylekatarnls/laravel-sitemap` (supports ImageSitemap tags) or build a custom solution with `SimpleXMLElement`. However, `spatie/laravel-sitemap` stands out for its balance of automation, extensibility, and Laravel integration.