meteor-sitemaps ![Build Status](https://api.travis-ci.org/gadicc/meteor-sitemaps.svg)
Quickly create valid sitemaps using your own functions.
Copyright (c) 2013 by Gadi Cohen dragon@wastelands.net and released under the MIT license (see LICENSE.txt).
Quick Start
meteor add gadicohen:sitemaps
- Create
server/sitemaps.js
which contains something like:
1sitemaps.add('/sitemap.xml', function() { 2 // required: page 3 // optional: lastmod, changefreq, priority, xhtmlLinks, images, videos 4 return [ 5 { page: '/x', lastmod: new Date() }, 6 { page: '/y', lastmod: new Date(), changefreq: 'monthly' }, 7 { page: '/z', lastmod: new Date().getTime(), changefreq: 'monthly', priority: 0.8 }, 8 // https://support.google.com/webmasters/answer/178636?hl=en 9 { page: '/pageWithViedeoAndImages', 10 images: [ 11 { loc: '/myImg.jpg', }, // Only loc is required 12 { loc: '/myOtherImg.jpg', // Below properties are optional 13 caption: "..", geo_location: "..", title: "..", license: ".."} 14 ], 15 videos: [ 16 { loc: '/myVideo.jpg', }, // Only loc is required 17 { loc: '/myOtherVideo.jpg', // Below properties are optional 18 thumbnail_loc: "..", title: "..", description: ".." etc } 19 ] 20 }, 21 // https://support.google.com/webmasters/answer/2620865?hl=en 22 { page: 'lang/english', xhtmlLinks: [ 23 { rel: 'alternate', hreflang: 'de', href: '/lang/deutsch' }, 24 { rel: 'alternate', hreflang: 'de-ch', href: '/lang/schweiz-deutsch' }, 25 { rel: 'alternate', hreflang: 'en', href: '/lang/english' } 26 ]} 27 ]; 28});
You can call sitemaps.add()
as many times as you like.
More details on the format below.
Note that the url
is automatically added to the data served from
/robots.txt
(since 0.0.4, using the robots.txt smart package).
Important: The above example uses a brand new Date() for every link. This
is just for demonstration purposes. Of course you should use the real date
of the last page update (updatedAt
from the database?). If you always use
the current time, Google will penalize you (or at the very least, ignore this
field on future crawls).
Full Usage
1sitemaps.add(url, list);
URL
The obvious example is /sitemap.xml
. You can call the function
more than once to have many different (types of) sitemaps. The URL is added
to the output of /robots.txt automatically (since 0.0.4).
Note that the location is
important. A sitemap can only
reference other URLs in its own path or descendant paths. e.g. /sitemap.xml
can reference all URLs on the site. /articles/sitemap.xml
can only reference
other pages in the /articles/
directory/path/route.
List (Array or Function)
The list can either be an array in the following format, or a function that returns an array in the following format (e.g. a function that iterates over information in a Collection).
1[ 2 { 3 // Required. http[s]://sitename.com automatically prepended */ 4 page: '/pageName', 5 // Optional. Timestamp of when the page was last modified. 6 lastmod: new Date(), // or new Date().getTime() 7 // Optional. always, hourly, daily, weekly, monthly, yearly, never 8 // http://www.sitemaps.org/protocol.html#changefreqdef 9 changefreq: 'monthly', 10 // Optional. http://www.sitemaps.org/protocol.html#prioritydef 11 priority: 0.8 12 // Optional. https://support.google.com/webmasters/answer/2620865 13 // Again, the base URL is automatically prepended to the href key 14 xHtmlLinks: [ 15 { ref: 'alternate', 'hreflang': 'en', 'href': 'en/blah' }, 16 { ref: 'alternate', 'hreflang': 'de', 'href': 'de/blah' } 17 ], 18 // Optional. https://support.google.com/webmasters/answer/2620865?hl=en 19 // Again, the base URL is automatically prepended to the loc key 20 images: [ 21 { loc: '/myImg.jpg' }, // Only loc is required 22 { loc: '/myOtherImg.jpg', // Below properties are optional 23 caption: "..", geo_location: "..", title: "..", license: ".."} 24 ], 25 // Optional. https://support.google.com/webmasters/answer/80472?hl=en 26 // Again, the base URL is automatically prepended to loc, *_loc 27 videos: [ 28 { loc: '/myVideo.jpg' }, // Only loc is required 29 { loc: '/myOtherVideo.jpg' // Below properties are optional 30 thumbnail_loc: "..", title: "..", description: "..", etc: ".." } 31 ] 32 } 33]
Other options might come soon, e.g. to automatically use routes in your app to build the sitemap.
Example (from Meteorpedia)
1sitemaps.add('/mw_AllPages_sitemap.xml', function() { 2 var out = [], pages = WikiPages.find().fetch(); 3 _.each(pages, function(page) { 4 out.push({ 5 page: 'read/' + page.name, 6 lastmod: page.lastUpdated 7 }); 8 }); 9 return out; 10});
You can see this output here: http://www.meteorpedia.com/mw_AllPages_sitemap.xml
Locations (page, loc, href, etc)
Anywhere where a url can be provided, you can provide a "relative URL" (with
or without a leading /), and Meteor.absoluteUrl() will be prepended. You
can override this by calling sitemaps.config('rootUrl', 'myRootUrl')
. For
individual links, providing an absoluet URL (beginning with http://
or
https://
) will avoid this behaviour. URI components are escaped for you.
Contributors
Thanks to @zol, @tarang, @dandv, @DirkStevens, @picsoung, @SashaG for various PRs as listed in History.md.