Generating Large XML Sitemaps in Django
It was really easy to generate xml sitemaps using the Django web framework, however If your model data is large it will take a long time building the sitemaps on the fly. This can result in HTTP 502 Bad Gateway error or proxy timeouts depending in your deployment setup and platform. In our case its an H12 Request Timeout since our django application is deployed in Heroku. Heroku has a hard limit of 30 seconds for a response to be sent upon receiving the web request.
So how to generate sitemaps for a large dataset? One way to solve this problem is to break the large sitemap into a group of smaller sitemaps.
For instance I have built a separate sitemap for Articles by the first letter of the title. So there will be a sitemap file for every letter in the alpahabet.
If you want to use alpahnumeric characters that will also work but in our case alphabet letters works fine. The Python built-in method ascii_lowercase
will return an array of alphabet characters that we can loop to build each sitemaps.
Below I have also added caching of the sitemaps which is really nice. Then add the sitemaps in the urls.py
like so: