Coder Social home page Coder Social logo

Comments (5)

tasiotas avatar tasiotas commented on September 6, 2024 1

perhaps its out of scope of this project. I managed to split it with simpleSitemapAndIndex and lots of additional code

from sitemap.js.

Shekhar-Zealous avatar Shekhar-Zealous commented on September 6, 2024

@tasiotas Can you please show me how did you manage to create different multiple sitemaps with indexing.

from sitemap.js.

tasiotas avatar tasiotas commented on September 6, 2024

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 simpleSitemapAndIndex. I believe I was calling it multiple times, and then moving/renaming output file. I had to generate indexes by hand anyway...

from sitemap.js.

Shekhar-Zealous avatar Shekhar-Zealous commented on September 6, 2024

@tasiotas Thanks for the quick response.
Even I considered doing custom code to generate XML as I want as the package is less supportive for creating multiple sitemaps at once and index it in sitemap.xml.

from sitemap.js.

sepehrsamavati avatar sepehrsamavati commented on September 6, 2024

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();
    }
}

from sitemap.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.