-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
index with multiple categorized sitemaps #408
Comments
perhaps its out of scope of this project. I managed to split it with |
@tasiotas Can you please show me how did you manage to create different multiple sitemaps with indexing. |
sorry, in the end I dropped sitemap.js lib and went full with custom code writing my own XMLs. I dont have a code at hand where I had it working with |
@tasiotas Thanks for the quick response. |
It's a little late to be here but here is how I managed it. import fs from "node:fs";
import { type SitemapItemLoose, SitemapAndIndexStream, SitemapStream, SitemapIndexStream } from "sitemap";
const hostname = "https://...";
export default class SitemapApplication {
generateSitemap() {
const blogs= []; /* get items from somewhere */
const books= []; /* get items from somewhere */
const movies= []; /* get items from somewhere */
this.createSitemapCategory(blogs, "blog", true);
this.createSitemapCategory(books, "books", true);
this.createSitemapCategory(movies, "movies", true);
this.createIndexStream(["blog", "books", "movies"]);
}
private createIndexStream(categories: string[]) {
const indexStream = new SitemapIndexStream();
categories.forEach(category => indexStream.write({ url: `${hostname}/sitemap_${category}.xml` }));
indexStream.pipe(fs.createWriteStream("sitemap_index.xml"));
indexStream.end();
}
private createSitemapCategory(data: SitemapItemLoose[], name: string, singleFile = false) {
const stream = new SitemapAndIndexStream({
getSitemapStream: index => {
const sitemapStream = new SitemapStream({
hostname: hostname
});
const filePath = singleFile && index === 0 ? `sitemap_${name}.xml` : `sitemap_${name}-${index}.xml`;
const ws = sitemapStream
.pipe(fs.createWriteStream(filePath));
return [new URL(filePath, hostname).toString(), sitemapStream, ws];
}
});
data.forEach(item => stream.write(item));
stream.end();
}
} |
Hi,
I would like to split sitemaps per category and have them all referenced in sitemap-index.xml.
It does not seem to be trivial given existing examples. Does anyone have done similar setup?
This is what I would like to achieve, currently I can only write to single sitemap stream. Following
SitemapAndIndexStream
example.The text was updated successfully, but these errors were encountered: