Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including contentful-sitemap with all npm packages installed. Try it out:

require("contentful/package.json"); // contentful is a peer dependency. var contentfulSitemap = require("contentful-sitemap")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

contentful-sitemap v0.3.6

Easily generate a sitemap.xml file that consists of both static routes and dynamic routes generated from Contentful content.

🗺 contentful-sitemap

npm version npm Coverage Status CircleCI Greenkeeper badge

Build a dynamic sitemap.xml file based on content pulled in from Contentful via the Contentful API.

Install

Via npm

npm install contentful-sitemap

Via Yarn

yarn add contentful-sitemap

How to use

contentful-sitemap requires a Contentful client in order to fetch entries from your Contenful space in order to build your dynamic sitemap entries. So, make sure you have contentful as a dependency in your project.

Install contentful

npm i -S contentful

Create Contentful client

const contentful = require('contentful');
const ContentfulSitemap = require('contentful-sitemap');

const client = contentful.createClient({
  accessToken: '[ACCESS_TOKEN]',
  space: '[SPACE_ID]',
});

Next comes setting up the ContentfulSitemap instance that will be used by your /sitemap.xml route to generate and return the sitemap. ContentfulSitemap requires a client instance, so make sure you pass one into the constructor.

Create ContentfulSitemap instance

const sitemap = new ContentfulSitemap([], client, {options});

Parameters

  • routes - Array of route definitions to use to generate sitemap.

  • client - Contentful client that will be used for fetching locales and entries from Contentful.

  • options - Options that you can use to customize how sitemaps are generated.

Routes

contentful-sitemap uses the sitemap package behind the scenes to generate the final XML that is returned, so all route options supported by that package are also supported. Below are the few specifics that directly relate to how contentful-sitemap will process your routes entries.

NameDefaultDescription
urlnullUse this property if the path you are providing does not require any segments to be swapped out dynamically.
idnullContentful entry id to fetch for the specific path. Only used if the dynamicLastmod option is set to true.
patternnullpath-to-regexp pattern to use for generating dynamic listings based on Contentful content or setting locale specific paths.
priority1Default priority to set for each url. Set to 1 as a default so it can be excluded in your routes definitions.
paramsnullObject used to specify the mapping of pattern parameter to Contentful response data.
querynullQuery used by the Contentful client for fetching entries. All contentful getEntries query options are supported.

Default Options

The following options can be used to customize the way your sitemap is generated. The one option that you’ll definitely want to set, if not defined in your routes is the origin option, which is prefixed before the urls and patterns defined in your routes.

NameDefaultDescription
locales[]Array of locales to include for all route instances, if not using dynamicLocales.
dynamicLocalesfalseWhether or not to fetch the list of enabled locales from Contentful.
dynamicLastmodfalseAutomatically use the sys.updatedAt value of each entry to set the lastmod value.
origin''The origin to prefix each url or pattern with.
localeParamlocaleThe param to set within your pattern to replace with a locale.
defaultLocalenullThe default locale that will be used in the main loc value for a url.

Methods

  • addRoute(route) - Method to use for adding additional urls after the initial ContentfulSitemap instance has been created.

  • toXML((xml, err) => {}) - The magic✨ Call this method when you want to generate your sitemap.

Example

const express = require('express');
const contentful = require('contentful');
const ContentfulSitemap = require('contentful-sitemap');

const app = express();

const client = contentful.createClient({
  accessToken: '[ACCESS_TOKEN]',
  space: '[SPACE_ID]',
});

const sitemap = new ContentfulSitemap([
  {
    pattern: '/',
    id: '[HOME_PAGE_ID]',
  },
  {
    pattern: '/about',
    id: '[ABOUT_PAGE_ID]',
    priority: 0.7,
  },
  {
    pattern: '/news',
    id: '[NEWS_PAGE_ID]',
  },
  {
    pattern: '/news/:slug',
    priority: 0.5,
    query: {
      select: 'fields.slug',
    },
    params: {
      slug: 'fields.slug',
    },
  },
  {
    url: '/terms',
    priority: 0.3,
  },
  {
    url: '/privacy',
    priority: 0.3,
  }
], client);

app.get('/sitemap.xml', (req, res) => {
  sitemap.toXML((xml, err) => {
    if (err) {
      return res.status(500).end();
    }

    res.header('Content-Type', 'application/xml');
    res.send(xml);
  });
});

...

License

MIT © Ryan Hefner

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free