Skip to content

Commit

Permalink
Stop generating sitemap.xml.gz (#6561)
Browse files Browse the repository at this point in the history
We generate sitemap-index.xml which is also put correctly into robots.txt. However we still provide the old sitemap.xml.gz.

But, while this serves no purpose in addition to the index, it can cause problems if Google somehow parses it (for example it's submitted to the Google Search console). For this reason, we  stop providing sitemap.xml.gz.

- also serve the batched sitemap under the old name sitemap.xml.gz
- update comment in robots.txt
  • Loading branch information
reebalazs committed Feb 11, 2025
1 parent 97f34bf commit 4212296
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/volto/news/6561.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop generating sitemap.xml.gz @reebalazs
15 changes: 14 additions & 1 deletion packages/volto/src/express-middleware/sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,24 @@ export const sitemapIndex = function (req, res, next) {
});
};

export const sitemapIndexCompatibility = function (req, res, next) {
generateSitemapIndex(req, true).then((sitemapIndex) => {
res.set('Content-Type', 'application/x-gzip');
res.set('Content-Disposition', 'attachment; filename="sitemap.xml.gz"');
res.send(sitemapIndex);
});
};

export default function sitemapMiddleware() {
const middleware = express.Router();

middleware.all('**/sitemap.xml.gz', sitemap);
// For backwards compatibility, and allow a graceful transition for
// sites that are already set up on the Google Search Console, we continue delivering
// the new batched sitemap under the old sitemap.xml.gz name.
middleware.all('**/sitemap.xml.gz', sitemapIndexCompatibility);
middleware.all('**/sitemap:batch.xml.gz', sitemap);
// For new setups, `sitemap-index.xml` should be added to the
// Google Search Console.
middleware.all('**/sitemap-index.xml', sitemapIndex);
middleware.id = 'sitemap.xml.gz';
return middleware;
Expand Down
11 changes: 9 additions & 2 deletions packages/volto/src/helpers/Sitemap/Sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const generateSitemap = (_req, start = 0, size = undefined) =>
* @param {Object} _req Request object
* @return {string} Generated sitemap index
*/
export const generateSitemapIndex = (_req) =>
export const generateSitemapIndex = (_req, gzip = false) =>
new Promise((resolve) => {
const { settings } = config;
const APISUFIX = settings.legacyTraverse ? '' : '/++api++';
Expand All @@ -88,7 +88,14 @@ export const generateSitemapIndex = (_req) =>
const result = `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${items.join('\n')}\n</sitemapindex>`;
resolve(result);

if (gzip) {
zlib.gzip(Buffer.from(result, 'utf8'), (_err, buffer) => {
resolve(buffer);
});
} else {
resolve(result);
}
}
});
});

0 comments on commit 4212296

Please sign in to comment.