-
Notifications
You must be signed in to change notification settings - Fork 156
/
sitemapAndIndex.js
56 lines (50 loc) · 1.68 KB
/
sitemapAndIndex.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
53
54
55
56
const { /* createReadStream, */ createWriteStream } = require('fs');
const { resolve } = require('path');
const { createGzip } = require('zlib');
const {
SitemapAndIndexStream,
SitemapStream,
// lineSeparatedURLsToSitemapOptions,
} = require('sitemap');
const sms = new SitemapAndIndexStream({
limit: 10000, // defaults to 45k
// SitemapAndIndexStream will call this user provided function every time
// it needs to create a new sitemap file. You merely need to return a stream
// for it to write the sitemap urls to and the expected url where that sitemap will be hosted
getSitemapStream: (i) => {
const sitemapStream = new SitemapStream({
hostname: 'https://example.ru/',
});
const path = `./sitemap-${i}.xml`;
const ws = createWriteStream(resolve(path + '.gz'));
sitemapStream
.pipe(createGzip()) // compress the output of the sitemap
.pipe(ws); // write it to sitemap-NUMBER.xml
return [
new URL(path, 'https://example.com/subdir/').toString(),
sitemapStream,
ws,
];
},
});
// // when reading from a file
// lineSeparatedURLsToSitemapOptions(createReadStream('./your-data.json.txt'))
// .pipe(sms)
// .pipe(createGzip())
// .pipe(createWriteStream(resolve('./sitemap-index.xml.gz')));
// or reading straight from an in-memory array
sms
.pipe(createGzip())
.pipe(createWriteStream(resolve('./sitemap-index.xml.gz')));
const arrayOfSitemapItems = [
{ url: '/page-1/', changefreq: 'daily' },
{
url: '/docs',
links: [
{ lang: 'ru', url: 'https://example.ru/docs' },
{ lang: 'en', url: 'https://example.com/docs' },
],
},
];
arrayOfSitemapItems.forEach((item) => sms.write(item));
sms.end();