Uncategorized

Handling Large Sitemaps in Laravel with Spatie’s Sitemap Package

Sitemaps are vital to your site’s SEO as they guide search engines to the most essential pages of your website. If your website contains a vast amount of data (say, over 50,000 URLs), creating and managing a sitemap can be challenging. This is where Spatie’s Laravel Sitemap package comes in handy. In this article, we’ll explore how to use this package to generate and manage large sitemaps efficiently.

Installation

Firstly, we need to install the package. You can do so via composer:

Problem Statement

If your website has more than 50,000 URLs, generating a sitemap can be a challenge. The Sitemaps protocol limits a sitemap file to 50,000 URLs and 50MB uncompressed. If you need to list more than that, you have to create multiple Sitemap files.

To tackle this, we’ll break our sitemap into smaller parts, each containing no more than 20,000 URLs. For instance, if we have a model called Post with 1 million records, we’ll create a series of sitemap files, each containing 20,000 URLs.

Solution

We’ll create a custom Artisan command that will generate multiple sitemaps and a sitemap index. The index will include links to all of our sitemaps.

Here’s how you can accomplish this:

In this script, the loop starts from 0, and the sitemap files are named sitemap-0.xml, sitemap-1.xml, etc. The sitemap-index.xml file will reference all the individual sitemap files.

Remember to register the new command in your Kernel.php file:

Related Post

To generate the sitemaps, run the following Artisan command:

This will create a new sitemap file for every 20,000 Post records, up to a total of 1 million records, and then it will write an index file that references all the individual sitemap files.

Keeping Sitemaps Updated

You should run this command regularly to keep your sitemaps updated. You can use Laravel’s task scheduling to run the command once a day:

This will ensure that your sitemap remains current with your website’s content.

Wrapping Up

Managing large sitemaps can be a daunting task, but with Laravel and the Spatie Sitemap package, it becomes a breeze. By breaking down the sitemap into smaller chunks, you can efficiently handle a larger number of URLs without violating the sitemap protocol. As a result, your website becomes more accessible and SEO-friendly.

This post was last modified on June 17, 2023 2:28 pm

Dharmesh Goyal

Dharmesh is Co-Founder of TechnoFizi and a passionate blogger. He loves new Gadgets and Tools. He generally covers Tech Tricks, Gadget Reviews etc in his posts. Beside this, He also work as a SEO Analyst at TechnoFizi Solutions.

Share
Published by
Tags: laravel

Recent Posts

How to Automate Instagram DMs and Comments in 2026 (Without Getting Your Account Flagged

If you've grown even a modest Instagram following, you already know the problem: DMs and…

3 days ago

How Autonomous Pallet Mover Robots Adapt to Frequent Warehouse Layout Changes in US Fulfillment Centers

USA-based fulfilment centres rarely operate with the same warehouse layout for long. Changing demand due…

2 weeks ago

5 Best VPNs with Free Trial No Credit Card Needed: Risk-Free Picks for 2026

You hit “Start free trial,” enter a card, forget to cancel, and a bill appears.…

3 weeks ago

Top 10 Best AI Avatar Generator Tools of 2026 I Actually Used to Grow My Instagram

AI avatar generator tools have completely transformed the way creators produce content for Instagram and…

1 month ago

How to Choose the Right Delivery Routing Program for Your Fleet Size and Delivery Goals

As delivery volumes surge and customer expectations evolve, choosing the right routing program is crucial…

2 months ago

Best AI Tools for Summarizing PDFs: The Ones I Use In 2026

Best AI Tools for Summarizing PDFs can help you understand lengthy documents in minutes instead…

2 months ago