sitemap.xml generation in node.js
Site Map Generation in node.js
This module is intended to be used as a dependency in a website specific site map building project. Add the module to the "dependencies" section of a package.json file:
There is nothing stopping you from installing it locally and editing the embedded configuration files, but that is not the intent.
npm install site-mapper
Create a directory to hold your site map generation configuration. This directory will hold all the files needed to tell site-mapper what to create.
Create a package.json file similar to the following:
If you want to use coffeescript for configuration, you will have to add the coffee script module as a dependency:
The configuration file can contain any of the following keys. The values below are defaults that will be used unless overridden in your configuration file.
config =config.sources =config.sitemaps =config.defaultSitemapConfig =targetDirectory: "/tmp/sitemaps/"sitemapIndex: "sitemap.xml"sitemapRootUrl: ""sitemapFileDirectory: "/sitemaps"maxUrlsPerFile: 50000urlBase: ""config.sitemapIndexHeader = '<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns:xsi="" xsi:schemaLocation=" " xmlns="">'config.sitemapHeader = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns:xsi="" xmlns:image="" xsi:schemaLocation=" " xmlns="" xmlns:video="" xmlns:geo="" xmlns:news="">'=if '/' == hrefoptionsurlBaseelse if href && hreflength && href0== '/'""else if href && hreflength && hrefmatch/^https?:\/\//hrefelseif hreflength"/"elseoptionsurlBase
The sitemaps object contains named keys pointing at objects that define a particular sitemap. The sitemap definition can contain (and override) any of the keys in the config.defaultSitemapConfig object. The produced sitemap consists of a sitemap index xml file referencing one or more gzipped sitemap xml files, created from urls produced by the config.sources objects. The configuration allows for defining 1 or more sitemaps to create, for example, you might configure one sitemap for the http version of a site and another sitemap for the https version of the site. Or you might define one sitemap for the www subdomain and another for the foobar subdomain. By default, all sources defined in config.sources are used to generate urls for all sitemaps. To use different sources for different sitemaps, provide in each sitemap configuration object a sources key like one of the following:
# Specify which sources to include. All others are ignoredsources:includes: 'source1''source2'...
# Specify which sources to exclude. All others are includedsources:excludes: 'source1''source2'...
The sources object contains arbitrarily named keys pointing at functions that take a single sitemapConfig object and return an object with the following keys: type, options. The input parameter, sitemapConfig, is an object formed by merging the config.defaultSitemapConfig object with a specific sitemap configuration (more about this later).
In the returned object, the type is either one of the built in Source types (see below) or a site specific class derived from the Source base class. See the test suite for examples of creating Source subclasses.
A minimal config might be:
require 'site-mapper'appConfig =sitemaps:main:sitemapRootUrl: ""urlBase: ""sitemapIndex: "sitemap_index.xml"targetDirectory: "/tmp/sitemaps//http"sources:staticUrls:type: StaticSetSourceoptions:channel: 'static'changefreq: 'weekly'priority: 1urls:'/''/about''/faq''/jobs'serviceUrls:type: HttpSourceoptions:changefreq: 'weekly'priority: 0.8serviceUrl: "":urlcategory:urls = JSONparsebodyurlsmappermalink: urlpermalinkupdatedAt: urlupdated_atcategory: urlcategory:"/"module.exports = appConfig
Finally, putting it all together, you can generate the sitemaps as follows:
rm -rf node_modules; npm install
Below is a make file that encapsulates the above recipe. It can be run by running:
make setup generate
usage :@echo ''@echo 'Core tasks : Description'@echo '-------------------- : -----------'@echo 'make setup : Install dependencies'@echo 'make generate : Generate the sitemaps'@echo ''COFFEE=./node_modules/.bin/coffeeSITEMAPPER=./node_modules/.bin/site-mapperNPM_ARGS=NODE_ENV=stagingsetup :@rm -rf node_modules@echo npm $(NPM_ARGS) install@npm $(NPM_ARGS) installgenerate :@rm -rf tmp@NODE_ENV=$(NODE_ENV) $(SITEMAPPER)
The site-mapper module views the sitemap generation process as follows:
+--------+ +------------+ +--------------+ +---------+ | Source | | SiteMapper | | SitemapGroup | | Sitemap | |--------|produces |------------| adds |--------------| adds |---------| | +--------->| |------->| |------>| | | | urls | | urls | | urls | | +--------+ +------------+ +--------------+ +---------+
There are three included Source implementations:
The urls produced by Source objects can be simple strings or objects, if finer grained control of sitemap.xml tag values is required.
Each url is associated with a channel. The channel can be set per source, derived from the url path or set explicitly if the url is an object. The channel is used to name individual sitemap files. The default behavior is to take the first path segment as the channel. Thus, the url
would be assigned to the 'stuff' channel and would be put in sitemaps named stuff0.xml.gz, stuff1.xml.gz, etc.
A sitemap group is created for each URL channel. As urls are added to their corresponding SitemapGroup, the group will create sequentially numbered Sitemap files, each containing a configurable number of urls, 50,000 by default. The name of the sitemap files is of the form
site-mapper will create a sitemap index and as many sitemap files as are required. The sitemap files are gzipped. There is no way to turn gzipping off.
It is up to you how you expose the site maps generated by site mapper to Google and other search engines. Each company does this differently, so there are no default publishing mechanisms in site-mapper.
This should install all dependencies and run the test suite using coffee