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:

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:

schedule command

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.

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here