- How do I generate a sitemap by crawling my Laravel site automatically?
- Use `SitemapGenerator::create('https://your-site.com')->writeToFile($path)`. The package crawls your site recursively, respecting Laravel routes. Configure concurrency with `setConcurrency()` to avoid overloading shared hosting. Exclude URLs via `shouldCrawl()` callbacks.
- Can I manually add URLs to a sitemap instead of crawling?
- Yes. Use `Sitemap::create()->add(Url::create('/path')->setLastModificationDate(...))` to build sitemaps programmatically. This is ideal for dynamic routes or non-HTML content like APIs. Combine with crawling for hybrid sitemaps.
- What Laravel versions does spatie/laravel-sitemap support?
- The package officially supports Laravel 8+. For Laravel 7, check the CHANGELOG for compatibility notes, as some features may require adjustments. PHP 8.0+ is required due to underlying dependencies like spatie/crawler.
- How do I add model entries (e.g., Blog posts) to a sitemap?
- Implement the `Sitemapable` interface on your Eloquent model. Define `getSitemapUrl()` and `getLastModificationDate()` methods. The package will auto-detect and include these models when generating sitemaps via `SitemapGenerator`.
- Is there a way to handle JavaScript-rendered content (SPAs) in sitemaps?
- Yes, but it requires `spatie/browsershot` for headless Chrome rendering. Configure the crawler with `setCrawlerOptions(['browser' => true])` and ensure a Chrome binary is available. Note this adds complexity and may fail in CI/CD environments.
- How do I optimize sitemap generation for large sites or shared hosting?
- Reduce concurrency with `setConcurrency(1)` to avoid timeouts. Limit crawl depth with `setMaximumCrawlCount()`. For shared hosting, disable JS rendering and use manual URL additions for critical pages. Schedule generation via Artisan commands to avoid runtime conflicts.
- Can I validate or test sitemaps before deploying to production?
- Test manually generated sitemaps first with `Sitemap::create()->writeToFile()` to ensure correct URLs and metadata. For crawled sitemaps, mock `SitemapGenerator` in unit tests or validate against Google Search Console. Use `hasCrawled()` callbacks to debug excluded URLs.
- What are the alternatives to spatie/laravel-sitemap for Laravel?
- Alternatives include `kolapunch/laravel-sitemap` (simpler, less flexible) and `nuwave/lighthouse` (for GraphQL-driven sitemaps). However, spatie/laravel-sitemap stands out for its hybrid crawling/manual approach, model integration, and Laravel-native design.
- How do I store sitemaps in cloud storage like S3?
- Use Laravel’s filesystem disks. Pass a disk path to `writeToFile()`, e.g., `storage_path('app/sitemap.xml')` or `public_path('sitemap.xml')`. For S3, configure `filesystem_disks` in `config/filesystems.php` and use `storage_disk('s3')->path('sitemaps/sitemap.xml')`.
- Why is my sitemap generation failing with 'Maximum execution time exceeded'?
- This typically happens due to high concurrency or deep crawls. Reduce concurrency with `setConcurrency(2)` or limit crawl depth. For shared hosting, increase PHP’s `max_execution_time` in `php.ini` or split sitemaps into smaller chunks using `SitemapGenerator::create()->chunk(1000)`.