-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext-sitemap.config.js
52 lines (44 loc) · 1.39 KB
/
next-sitemap.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// next-sitemap.config.mjs and add dotenv for process.env.SITE_URL//
// Importing dependencies
import { SitemapStream, streamToPromise } from 'sitemap';
import { createGzip } from 'zlib';
import { Readable } from 'stream';
import { Config } from 'dotenv';
// Ensure the environment variable is defined
if (!process.env.SITE_URL) {
throw new Error('Missing environment variable: SITE_URL');
}
// Exporting the configuration object
export default {
siteUrl: process.env.SITE_URL,
generateRobotsTxt: true,
exclude: ['/404'],
robotsTxtOptions: {
additionalSitemaps: [
`${process.env.SITE_URL}/sitemap.xml`, // Specify the absolute URLs to your sitemaps generated elsewhere
],
},
transform: async (config, paths) => {
const sitemap = new SitemapStream({ hostname: config.siteUrl });
paths.forEach(path => sitemap.write(path));
sitemap.end();
const sitemapData = await streamToPromise(sitemap);
const gzippedSitemap = await getGzippedSitemap(sitemapData);
return [
{
loc: '/sitemap.xml',
content: gzippedSitemap,
encoding: 'gzip',
},
];
},
};
async function getGzippedSitemap(sitemapData) {
const sitemapStream = new Readable();
sitemapStream.push(sitemapData);
sitemapStream.push(null);
const gzip = createGzip();
sitemapStream.pipe(gzip);
const gzippedSitemap = await streamToPromise(gzip);
return gzippedSitemap;
}