Ad

How To Submit Sitemap-index And Child Sitemaps In Express?

- 1 answer

So far I used this option how to generate a sitemap in expressjs

But now that my website has over 50k URLs I need to switch to sitemap index - https://developers.google.com/search/docs/advanced/sitemaps/large-sitemaps

So in express I can't just do:

router.get('/sitemap.xml', function(req, res) {
res.sendFile('YOUR_PATH/sitemap.xml');
});

because now I have multiple files:

/public/sitemaps:
sitemap-index.xml.gz
sitemap-0.xml.gz
sitemap-1.xml.gz
sitemap-2.xml.gz
...

And I need to give google access to all of them, so how can I make it work on express?

Ad

Answer

I found a solution using express routing and error handling for 404.

app.get('/sitemap-:int.xml.gz', (req, res, next) => {
  try {
    res.set('Cache-control', 'public, max-age=0');
    res.sendFile(path.join(__dirname, 'public', 'sitemaps', `sitemap-${req.params.int}.xml.gz`));
  } catch (err) {
    next(err);
    // or res.status(404).render('error');
  }
});

That will cover all sitemaps including the index one. Submit to google the www.yourwebsite.com/sitemap-index.xml.gz and they will fetch the rest themselves.

Ad
source: stackoverflow.com
Ad