0

I'm generating sitemaps file using sitemap package. It's working fine I have some static pages and I also want to generate a sitemap-static.xml that will contain all the static links and that file will be linked in sitemap-index.xml file. How can I achieve the static one.

sitemap.ts

const sitemapGeneration = () => {
    const products = [{ sku: "123" }]; // assume more items
    const sitemapItems = products.map((p: SearchDataProduct) => ({
      links: [
        { lang: 'en', url: BASE_URL + '/en/p/' + p.sku },
        { lang: 'th', url: BASE_URL + '/th/p/' + p.sku },
      ],
      url: '/th/p/' + p.sku,
    }));
    console.log('total sitemap item: ', sitemapItems?.length);
    const sms = new SitemapAndIndexStream({
      limit: 5000,
      lastmodDateOnly: false,
      getSitemapStream: (i) => {
        const index = i + 1;
        const sitemapStream = new SitemapStream({
          hostname: BASE_URL,
        });
        const path = `sitemap-product-${index}.xml`;
        const ws = sitemapStream.pipe(fs.createWriteStream(resolve(path)));
        streamToPromise(sitemapStream).then(async (data) => {
          return data.toString(); // sitemap product file buffer
        });
        return [new URL(path, BASE_URL).toString(), sitemapStream, ws];
      },
    });
    await streamToPromise(Readable.from(sitemapItems).pipe(sms)).then(
      async (data) => {
        fs.writeFile('sitemap.xml', data, () => {});
        return data.toString(); // sitemap-index buffer
      },
    );
    sms.end();
};

current sitemap-index.xml

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <sitemap>
      <loc>https://myurl.com/sitemap-product-1.xml</loc>
   </sitemap>
   <sitemap>
      <loc>https://myurl.com/sitemap-product-2.xml</loc>
   </sitemap>
</sitemapindex>

I want sitemap-index.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <sitemap>
      <loc>https://myurl.com/sitemap-product-1.xml</loc>
   </sitemap>
   <sitemap>
      <loc>https://myurl.com/sitemap-product-2.xml</loc>
   </sitemap>
   <sitemap>
      <loc>https://myurl.com/sitemap-static.xml</loc>
   </sitemap>
</sitemapindex>

right now, it's generating the files like below:

sitemap-index.xml
sitemap-product-1.xml
sitemap-product-2.xml

but I want the static content as well:

sitemap-index.xml
sitemap-static.xml
sitemap-product-1.xml
sitemap-product-2.xml
Zain Khan
  • 1,644
  • 5
  • 31
  • 67

0 Answers0