Skip to content

hassanzohdy/sitemap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

Sitemap Generator

A simple yet powerful sitemap generator written for Node.js.

Installation

Yarn

yarn add @mongez/sitemap

NPM

npm i @mongez/sitemap

PNPM

pnpm i @mongez/sitemap

BUN

bun add @mongez/sitemap

Usage

Import the sitemap class, pass the base url and locale codes to it, then add the pages to the sitemap.

import { Sitemap } from "@mongez/sitemap";

const sitemap = new Sitemap("https://example.com", ["en", "ar"]);

sitemap.add("/").add("/about-us").add("/contact-us");

await sitemap.saveTo("/path/to/sitemap.xml");

And that's it

This will create a sitemap with links, each link will have the original path along with the locale code as well.

Add more options to path

If you want to customize the path, you can pass an object to the add method.

sitemap.add({
  path: "/",
  lastModified: new Date(),
  changeFrequency: "daily",
  priority: 0.8,
});

The object can have the following properties:

type SitemapPath = {
  /**
   * The relative path to the website
   */
  path: string;
  /**
   * Last modification date
   */
  lastModified?: Date | string;
  /**
   * Change frequency
   */
  changeFrequency?: string;
  /**
   * Priority
   */
  priority?: number;
  /**
   * Hreflang
   * If you have multiple languages, you can add the hreflang, it will be used automatically if locale codes are set
   */
  hreflang?: {
    [localeCOde: string]: string;
  };
};

Generate Only

If you want just the xml sitemap without saving it to a file, you can use the generate method.

const xml = await sitemap.generate();

Alternatively, you can use the toXml method.

const xml = await sitemap.toXml();

ChangeLog

  • Fixed route path when generating sitemap.

About

Sitemap Generator For Nodejs Projects

Resources

Stars

Watchers

Forks

Packages

No packages published