- How do I generate a sitemap for my Laravel app without manually listing every URL?
- Use the `SitemapGenerator` class to crawl your site automatically. Call `SitemapGenerator::create('https://yourdomain.com')->writeToFile($path)` to discover all URLs dynamically. Configure crawl depth, concurrency, and filters via the generator’s methods to optimize performance.
- Can I mix auto-crawled URLs with manually added ones in the same sitemap?
- Yes. Start with `SitemapGenerator::create()` to crawl your site, then chain `->getSitemap()->add(Url::create(...))` to append custom URLs. This hybrid approach is ideal for combining static routes with dynamic content like blog posts or CMS pages.
- What Laravel versions does spatie/laravel-sitemap support?
- The package officially supports Laravel 8.x, 9.x, and 10.x. Check the [GitHub releases](https://github.com/spatie/laravel-sitemap/releases) for version-specific compatibility notes. It leverages Laravel’s service container and filesystem, so updates align with Laravel’s LTS cycles.
- How do I add lastmod, changefreq, or priority tags to URLs in a manual sitemap?
- Use the `Url` class methods: `setLastModificationDate()`, `setChangeFrequency()`, and `setPriority()`. For example: `Url::create('/page')->setLastModificationDate(Carbon::now())->setChangeFrequency('daily')`. These tags are critical for SEO and can be set per URL or via bulk operations.
- Will the crawler work for large sites with thousands of pages? What are the performance risks?
- The crawler uses configurable concurrency (default: 10 threads) and depth limits to balance speed and resource usage. For large sites, monitor CPU/memory usage and adjust `->setCrawlDepth()` or `->setMaxCrawlCount()`. Consider running crawls during off-peak hours or via Laravel queues to avoid timeouts.
- How do I generate a sitemap index for multiple sitemaps (e.g., paginated or language-specific)?
- Use `SitemapGenerator::create()->writeToDisk()` with the `SitemapIndex` class. The package automatically splits sitemaps into chunks (e.g., 50,000 URLs per file) and generates an index.xml. For language-specific sitemaps, create separate generators and merge them into the index.
- Can I integrate this with Eloquent models to auto-generate sitemaps for database records?
- Yes. Implement the `Sitemapable` interface on your model and define a `getSitemapUrl()` method. The package will automatically include these URLs in the sitemap. For example: `class Post implements Sitemapable { public function getSitemapUrl(): string { return route('posts.show', $this); } }`.
- How do I handle JavaScript-rendered content (SPAs, React, Vue) in the sitemap?
- Install `spatie/browsershot` to render JS before crawling. Add it to your crawler config: `->useBrowsershot()` and set `execute_javascript = true`. Note this requires Chrome installed on your server and may increase crawl time. For CI/CD, use Docker with Chrome pre-installed.
- Is there a way to exclude specific routes or pages from the auto-crawler?
- Yes. Use the `shouldCrawl()` method to filter URLs. For example: `->shouldCrawl(function (CrawlRequest $request) { return !Str::startsWith($request->url, '/admin'); })`. You can also exclude URLs via `->ignore()` or `->hasCrawled()` callbacks for dynamic logic.
- How do I schedule sitemap regeneration in Laravel (e.g., daily updates)?
- Create a custom Artisan command extending `SitemapGenerator` and schedule it in `app/Console/Kernel.php`. Example: `Schedule::command('sitemap:generate')->daily()`. For large sites, use Laravel queues to run the command asynchronously. Test under production load to avoid timeouts during peak traffic.