xml-sitemap
Utilities for quickly making custom XML sitemaps
Table of Contents
Install
Add it to a project with npm
:
$ npm install xml-sitemap --save
Then just require
it in your project:
var XmlSitemap = ;var sitemap = ;
Usage
For more info on anything check out the docs.
Build a sitemap from scratch
All methods that don't return values return the sitemap, so you can chain them all together.
var sitemap = ; // add some urlssitemap ;
We could have made the same sitemap with a single array:
var sitemap = ;
Setting a host
Setting a host with .setHost(url)
allows for all methods to accept relative links. .setHost
accepts a string or an object with options. Building the above with a host set:
var sitemap = // allows relative links, automatically adds host url ;
Updating and reading sitemap info
Update and read some info from the sitemap we built above:
// update some valuessitemap ; // an array of the urls is in sitemap.urlssitemapurls//=> [ 'http://domain.com/', 'http://domain.com/magic', 'http://domain.com/another-page', 'http://domain.com/sitemapz-rule' ] // and the xml is in sitemap.xml; // we can also read info from the sitemapsitemap;//=> true sitemap;//=> {today's date!} sitemap;//=> {today's date!} sitemap;//=> null
Editing an existing sitemap
We can read in an existing sitemap (like the one we made above!) and modify it on the fly.
// get the XML as a stringvar xmlString = ;// and pass it to the constructorvar sitemap = xmlString ; sitemap;//=> true // modify itsitemap ; sitemap;//=> falsesitemap;//=> '0.9' // quickly update lastmod vlauessitemap;//=> '2012-12-21' sitemap;sitemap;//=> {today's date}
Set lastmod values from files
For easily updating a url's lastmod, we can link a file to it. Here are a few ways to link the file:
var filename = 'path/to/file.html'; // last modified on '2012-12-21' // link by adding a filename to the 'file' key to its options when adding itsitemap; // or after it is already in the sitemapsitemap; // the lastmod has been updated accordingly in both casessitemap;//=> '2012-12-21' // linked files are listed in sitemap.filessitemapfiles//=> {'http://domain.com/': 'path/to/file.html'}
Linked files automatically update the lastmod of their respective urls when updateAll()
is called. So you can link many files to urls already in the sitemap by assigning the files object and then updating all.
sitemapfiles = url1: filename1 url2: filename2 ...;sitemap;
To update a lastmod from a file without linking it, use sitemap.updateFromFile(url, filepath)
. Unlink a file with sitemap.unlinkFile(url)
.
API
Read the full documentation on the API here.
Licence
MIT © 2016 Robert Pirtle