npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@quasibit/eleventy-plugin-sitemap

v2.2.0

Published

Eleventy plugin to generate a sitemap.

Downloads

4,654

Readme

eleventy-plugin-sitemap

npm Release workflow Test workflow codecov semantic-release Commitizen friendly License: MIT

Eleventy plugin to generate a sitemap using ekalinin/sitemap generator.

Installation

Install the package:

npm install --save @quasibit/eleventy-plugin-sitemap

Add the plugin to your Eleventy configuration (usually .eleventy.js):

const sitemap = require("@quasibit/eleventy-plugin-sitemap");

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(sitemap, {
    sitemap: {
      hostname: "https://example.com",
    },
  });
};

Usage

Create a sitemap file and use the shortcode to generate the sitemap XML:

---
permalink: /sitemap.xml
layout: null
eleventyExcludeFromCollections: true
---
{% sitemap collections.all %}

The above snippet does the following:

  • Sets the permalink to /sitemap.xml.
  • Disables any layout.
  • Excludes the sitemap file from Eleventy collections.
  • Calls the sitemap shortcode.

As the first argument to the shortcode, you pass the collection of items that you want to use in the sitemap.

This shortcode is available for Liquid, Nunjucks, and Javascript templates.

You can also copy this sample from the examples and adapt it to your needs:

cp node_modules/@quasibit/eleventy-plugin-sitemap/examples/default/sitemap.njk sitemap.njk

After creating the sitemap, you should add the sitemap to robots.txt:

Sitemap: https://example.com/sitemap.xml

Options

The following options are available:

eleventyConfig.addPlugin(sitemap, {
  // Name of the property for the last modification date.
  // By default it is undefined and the plugin will fallback to `date`.
  // When set, the plugin will try to use this property and it will fallback
  // to the `date` property when needed.
  lastModifiedProperty: "modified",

  sitemap: {
    // Options for SitemapStream. See https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemapstream
    // Hostname is needed when the URLs of the items don't include it.
    hostname: "https://example.com",
  },
});

Examples

See ./examples folder.

Advanced usage

Customizing sitemap properties

You can customize the values used for the sitemap links by adding front matter to your pages.

---
sitemap:
  changefreq: weekly
  priority: 0.8
---

Alternatively, you can apply the properties to a folder of items by creating a directory data file.

Assuming you want to apply this to all the files in posts/*:

// posts/posts.11tydata.js
module.exports = () => {
  return {
    sitemap: {
      changefreq: "weekly",
      priority: 0.8,
    },
  }
}

For a full list of options, please refer to Sitemap Item Options.

Exclude pages from the sitemap

You have several options to exclude pages from the sitemap.

You can exclude them using the standard eleventyExcludeFromCollections property, which will exclude them from all collections.

---
eleventyExcludeFromCollections: true
---

You can use the ignore property on the sitemap data, which will only exclude it from the sitemap.

---
sitemap:
  ignore: true
---

Or you can create a custom collection that excludes the items that you don't want in the sitemap and then pass that collection to the shortcode.

eleventyConfig.addCollection("sitemap", function(collectionApi) {
  return collectionApi.getAll().filter(item => {
    // Place your condition here for excluding items from the sitemap.
    return true;
  });
});

Specify the collection in the shortcode:

{% sitemap collections.sitemap %}

Create a multilingual sitemap

For creating a multilingual sitemap you need to add alternate language child links for each link.

You could do this with front matter and the links property , but in most cases it's easier to do this with a custom collection.

eleventyConfig.addCollection("sitemap", function(collectionApi) {
  return collectionApi
    .getAll()
    .map((item, index, all) => {
      return {
        url: item.url,
        date: item.date,
        data: {
          ...item.data,
          sitemap: {
            ...item.data.sitemap,
            links:
              all
                // Find all the translations of the current item.
                // This assumes that all translated items that belong together
                // have the same `translationKey` value in the front matter.
                .filter(other => other.data.translationKey === item.data.translationKey)

                // Map each translation into an alternate link. See https://github.com/ekalinin/sitemap.js/blob/master/api.md#ILinkItem
                // Here we assume each item has a `lang` value in the front
                // matter. See https://support.google.com/webmasters/answer/189077#language-codes
                .map(translation => {
                  return {
                    url: translation.url,
                    lang: translation.data.lang,
                  };
                }),
          },
        },
      }
    });
});

And then pass that collection to the sitemap shortcode:

{% sitemap collections.sitemap %}

You can see an example with dynamic hostnames in ./examples/multilingual/.

Related plugins

Maintainers

License

MIT. See LICENSE.