Giter Site home page Giter Site logo

sitemap-stream's Introduction

Build Status Coverage Status

sitemap-stream

SitemapStream is a simple and fast tool that build sitemaps using streams.
It automatically creates separated files, following the Google specifications

You can find information about the sitemap's syntax here (you'll find the authorized values the #inject method can receive).

Notes:

  • This package respects the semver and the keep a changelog specifications.
  • This module's code is using ES6 features, so you'll probably need to run your application with the flag --harmony (depending to your Node version).

Install

npm i sitemap-stream

Methods

SitemapStream#Constructor(options):

{
  hostname: String,
  limit: Number, // (default: 50000)
  isMobile: Boolean, // (default: false)
  outputFolder: String // (default: './')
  toCompress: Boolean // (default: true)
}

SitemapStream#inject(url || options)

Url Case:

// The url

Options Case:

{
  url: String, // (required)
  changeFreq: String,
  priority: String
}

SitemapStream#reset

Useful if you want to start writing a new sitemap using the same SitemapStream instance

Events

SitemapStream use streams, so it emit events to let you know what happens. Here are the events you can listen to:

error

sg.on('error', err => {
  console.error(`Something wrong happened: ${err}`);
});

drain

let i = 0;

function injectUrls() {
  while (i<1000000) {
    const isInjected = sg.inject(`http://test-${i}.com`);

    if (!isInjected) return i;

    i++;
  }
}

sg.on('drain', () => {
  console.info(`Because we have injected a big amount of lines in a short time, the stream could need to be drained, in this case, the sg#inject method returns false and the drain event is emitted when the stream is ready to write again`);

  injectUrls();
});

injectUrls();

sitemap-created

sg.on('sitemap-created', path => {
  console.info(`A sitemap file has just been written here: ${path}`);
});

sitemap-index

sg.on('sitemap-index', path => {
  console.info(`A sitemapindex file has just been written here: ${path}`);
});

done

sg.on('done', nbFiles => {
  console.info(`The job is done, we have written ${nbFiles} files !`);
});

Examples

Basic

  const sg = require('sitemap-stream')();

  sg.inject('/some-path');

  sg.inject({ url: '/another-path' });

  sg.inject({ url: '/my-last-path', changeFreq: 'daily', priority: 0.7 });

  sg.done();

With options

  const sg = require('sitemap-stream')({ isMobile: true });

  sg.inject('/some-path');

  sg.inject({ url: '/another-path' });

  sg.inject({ url: '/my-last-path', changeFreq: 'daily', priority: 0.7 });

  sg.done();

With Events

  const sg = require('sitemap-stream')();

  sg.on('sitemap-created', (fileName) => {
    // This listener will be trigger twice, one when the first 50 000 urls will be injected, and another time when you'll call the #done method  
    console.log('A sitemap has been created !');
  });

  sg.on('sitemapindex-index', () => {
    // When  listener will be trigger once the sitemapindex file will be written
    console.log('The sitemapindex has been created, we are done !');
  });

  sg.on('done', () => {
    console.info('Everything is done !');
  });

  for (let i=0; i<60000; i++) sg.inject(`/some-path-${i}`);

  sg.done();

sitemap-stream's People

Contributors

ludoblues avatar

Watchers

Tom Adler avatar James Cloos avatar  avatar

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.