Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions examples/write-to-file-with-limits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { createWriteStream } = require('fs');
const { SitemapStream } = require('sitemap');

async function writeAsync(sitemap, chunk, encoding) {
return new Promise((resolve, reject) => {
const writeReturned = sitemap.write(chunk, encoding, (error) => {
if (error !== undefined) {
reject(error);
} else {
resolve(writeReturned);
}
});
});
}

function rotateSitemap(basename, countSuffix) {
// Creates a sitemap object given the input configuration with URLs
let sitemap = new SitemapStream({
hostname: 'http://example.com',
byteLimit: 1360,
countLimit: 10,
});

const writeStream = createWriteStream(`./${basename}-${countSuffix}.xml`);
sitemap.pipe(writeStream);
sitemap.on('error', (error) => {
if (
error.name === 'ByteLimitExceededError' ||
error.name === 'CountLimitExceededError'
) {
console.log(error.name);
} else {
console.log('error from sitemap stream', error.message);
}
});

return sitemap;
}

async function example() {
let count = 0;
let sitemap = rotateSitemap('sitemap', count);
count++;

for (let i = 0; i < 100; i++) {
const item = {
url: `/page-${i}/`,
changefreq: 'daily',
priority: 0.3,
};

try {
await writeAsync(sitemap, item);
} catch (error) {
if (
error.name === 'ByteLimitExceededError' ||
error.name === 'CountLimitExceededError'
) {
// Create a new sitemap
sitemap = rotateSitemap('sitemap', count);
count++;

// The current item needs to be written to the new sitemap
// because it was not able to fit in the old sitemap
await writeAsync(sitemap, item);
} else {
throw error;
}
}
}

sitemap.end();
}

example();
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export {
SitemapItemStream,
SitemapItemStreamOptions,
SitemapItemToXMLString,
} from './lib/sitemap-item-stream';
export {
IndexTagNames,
Expand Down
Loading