UNPKG

10.6 kBMarkdownView Raw
1sitemap.js
2==========
3
4**sitemap.js** is a high-level sitemap-generating framework that
5makes creating [sitemap XML](http://www.sitemaps.org/) files easy.
6
7Maintainers
8-----------
9
10- [@ekalinin](https://github.com/ekalinin)
11- [@derduher](https://github.com/derduher)
12
13[![Build Status](https://travis-ci.org/ekalinin/sitemap.js.svg?branch=master)](https://travis-ci.org/ekalinin/sitemap.js)
14
15Table of Contents
16=================
17
18 * [sitemap.js](#sitemapjs)
19 * [Table of Contents](#table-of-contents)
20 * [Installation](#installation)
21 * [Usage](#usage)
22 * [Example of using sitemap.js with <a href="https://github.com/visionmedia/express">express</a>:](#example-of-using-sitemapjs-with-express)
23 * [Example of synchronous sitemap.js usage:](#example-of-synchronous-sitemapjs-usage)
24 * [Example of dynamic page manipulations into sitemap:](#example-of-dynamic-page-manipulations-into-sitemap)
25 * [Example of pre-generating sitemap based on existing static files:](#example-of-pre-generating-sitemap-based-on-existing-static-files)
26 * [Example of images with captions:](#example-of-images-with-captions)
27 * [Example of indicating alternate language pages:](#example-of-indicating-alternate-language-pages)
28 * [Example of indicating Android app deep linking:](#example-of-indicating-android-app-deep-linking)
29 * [Example of Sitemap Styling](#example-of-sitemap-styling)
30 * [Example of mobile URL](#example-of-mobile-url)
31 * [Example of using HH:MM:SS in lastmod](#example-of-using-hhmmss-in-lastmod)
32 * [Example of Sitemap Index as String](#example-of-sitemap-index-as-string)
33 * [Example of Sitemap Index](#example-of-sitemap-index)
34 * [Example of overriding default xmlns* attributes in urlset element](#example-of-overriding-default-xmlns-attributes-in-urlset-element)
35 * [Example of news usage](#example-of-news)
36 * [Testing](#testing)
37 * [License](#license)
38
39TOC created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
40
41Installation
42------------
43
44It's recommended to install via [npm](https://github.com/isaacs/npm/):
45
46 npm install --save sitemap
47
48Usage
49-----
50The main functions you want to use in the sitemap module are
51
52```javascript
53var sm = require('sitemap')
54// Creates a sitemap object given the input configuration with URLs
55var sitemap = sm.createSitemap({ options });
56// Generates XML with a callback function
57sitemap.toXML( function(err, xml){ if (!err){ console.log(xml) } });
58// Gives you a string containing the XML data
59var xml = sitemap.toString();
60```
61
62### Example of using sitemap.js with [express](https://github.com/visionmedia/express):
63
64```javascript
65var express = require('express')
66 , sm = require('sitemap');
67
68var app = express()
69 , sitemap = sm.createSitemap ({
70 hostname: 'http://example.com',
71 cacheTime: 600000, // 600 sec - cache purge period
72 urls: [
73 { url: '/page-1/', changefreq: 'daily', priority: 0.3 },
74 { url: '/page-2/', changefreq: 'monthly', priority: 0.7 },
75 { url: '/page-3/'}, // changefreq: 'weekly', priority: 0.5
76 { url: '/page-4/', img: "http://urlTest.com" }
77 ]
78 });
79
80app.get('/sitemap.xml', function(req, res) {
81 sitemap.toXML( function (err, xml) {
82 if (err) {
83 return res.status(500).end();
84 }
85 res.header('Content-Type', 'application/xml');
86 res.send( xml );
87 });
88});
89
90app.listen(3000);
91```
92
93### Example of synchronous sitemap.js usage:
94
95```javascript
96var express = require('express')
97 , sm = require('sitemap');
98
99var app = express()
100 , sitemap = sm.createSitemap ({
101 hostname: 'http://example.com',
102 cacheTime: 600000, // 600 sec cache period
103 urls: [
104 { url: '/page-1/', changefreq: 'daily', priority: 0.3 },
105 { url: '/page-2/', changefreq: 'monthly', priority: 0.7 },
106 { url: '/page-3/' } // changefreq: 'weekly', priority: 0.5
107 ]
108 });
109
110app.get('/sitemap.xml', function(req, res) {
111 res.header('Content-Type', 'application/xml');
112 res.send( sitemap.toString() );
113});
114
115app.listen(3000);
116```
117
118### Example of dynamic page manipulations into sitemap:
119
120```javascript
121var sitemap = sm.createSitemap ({
122 hostname: 'http://example.com',
123 cacheTime: 600000
124 });
125sitemap.add({url: '/page-1/'});
126sitemap.add({url: '/page-2/', changefreq: 'monthly', priority: 0.7});
127sitemap.del({url: '/page-2/'});
128sitemap.del('/page-1/');
129```
130
131
132
133### Example of pre-generating sitemap based on existing static files:
134
135```javascript
136var sm = require('sitemap')
137 , fs = require('fs');
138
139var sitemap = sm.createSitemap({
140 hostname: 'http://www.mywebsite.com',
141 cacheTime: 600000, //600 sec (10 min) cache purge period
142 urls: [
143 { url: '/' , changefreq: 'weekly', priority: 0.8, lastmodrealtime: true, lastmodfile: 'app/assets/index.html' },
144 { url: '/page1', changefreq: 'weekly', priority: 0.8, lastmodrealtime: true, lastmodfile: 'app/assets/page1.html' },
145 { url: '/page2' , changefreq: 'weekly', priority: 0.8, lastmodrealtime: true, lastmodfile: 'app/templates/page2.hbs' } /* useful to monitor template content files instead of generated static files */
146 ]
147});
148
149fs.writeFileSync("app/assets/sitemap.xml", sitemap.toString());
150```
151
152### Example of images with captions:
153
154```javascript
155var sitemap = sm.createSitemap({
156 urls: [{
157 url: 'http://test.com/page-1/',
158 img: [
159 {
160 url: 'http://test.com/img1.jpg',
161 caption: 'An image',
162 title: 'The Title of Image One',
163 geoLocation: 'London, United Kingdom',
164 license: 'https://creativecommons.org/licenses/by/4.0/'
165 },
166 {
167 url: 'http://test.com/img2.jpg',
168 caption: 'Another image',
169 title: 'The Title of Image Two',
170 geoLocation: 'London, United Kingdom',
171 license: 'https://creativecommons.org/licenses/by/4.0/'
172 }
173 ]
174 }]
175 });
176```
177
178### Example of videos:
179
180[Description](https://support.google.com/webmasters/answer/80471?hl=en&ref_topic=4581190) specifications. Required fields are thumbnail_loc, title, and description.
181
182```javascript
183var sitemap = sm.createSitemap({
184 urls: [{
185 url: 'http://test.com/page-1/',
186 video: [
187 { thumbnail_loc: 'http://test.com/tmbn1.jpg', title: 'A video title', description: 'This is a video' },
188 {
189 thumbnail_loc: 'http://test.com/tmbn2.jpg',
190 title: 'A video with an attribute',
191 description: 'This is another video',
192 'player_loc': 'http://www.example.com/videoplayer.mp4?video=123',
193 'player_loc:autoplay': 'ap=1'
194 }
195 ]
196 }]
197 });
198```
199
200
201### Example of indicating alternate language pages:
202
203[Description](https://support.google.com/webmasters/answer/2620865?hl=en) in
204the google's Search Console Help.
205
206```javascript
207var sitemap = sm.createSitemap({
208 urls: [{
209 url: 'http://test.com/page-1/',
210 changefreq: 'weekly',
211 priority: 0.3,
212 links: [
213 { lang: 'en', url: 'http://test.com/page-1/', },
214 { lang: 'ja', url: 'http://test.com/page-1/ja/', },
215 ]
216 },]
217 });
218```
219
220
221### Example of indicating Android app deep linking:
222
223[Description](https://developer.android.com/training/app-indexing/enabling-app-indexing.html#sitemap) in
224the google's Search Console Help.
225
226```javascript
227var sitemap = sm.createSitemap({
228 urls: [{
229 url: 'http://test.com/page-1/',
230 changefreq: 'weekly',
231 priority: 0.3,
232 androidLink: 'android-app://com.company.test/page-1/'
233 }]
234 });
235```
236
237### Example of Sitemap Styling
238
239```javascript
240var sitemap = sm.createSitemap({
241 urls: [{
242 url: 'http://test.com/page-1/',
243 changefreq: 'weekly',
244 priority: 0.3,
245 links: [
246 { lang: 'en', url: 'http://test.com/page-1/', },
247 { lang: 'ja', url: 'http://test.com/page-1/ja/', },
248 ]
249 },],
250 xslUrl: 'sitemap.xsl'
251 });
252```
253
254### Example of mobile URL
255
256[Description](https://support.google.com/webmasters/answer/34648?hl=en) in
257the google's Search Console Help.
258
259```javascript
260var sitemap = sm.createSitemap({
261 urls: [{
262 url: 'http://mobile.test.com/page-1/',
263 changefreq: 'weekly',
264 priority: 0.3,
265 mobile: true
266 },],
267 xslUrl: 'sitemap.xsl'
268 });
269```
270
271### Example of using HH:MM:SS in lastmod
272
273```javascript
274var sm = require('sitemap')
275 , sitemap = sm.createSitemap({
276 hostname: 'http://www.mywebsite.com',
277 urls: [{
278 url: 'http://mobile.test.com/page-1/',
279 lastmodISO: '2015-06-27T15:30:00.000Z',
280 changefreq: 'weekly',
281 priority: 0.3
282 }]
283 });
284```
285
286### Example of Sitemap Index as String
287
288```javascript
289var sm = require('sitemap')
290 , smi = sm.buildSitemapIndex({
291 urls: ['https://example.com/sitemap1.xml', 'https://example.com/sitemap2.xml'],
292 xslUrl: 'https://example.com/style.xsl' // optional
293 });
294```
295
296### Example of Sitemap Index
297
298```javascript
299var sm = require('sitemap')
300 , smi = sm.createSitemapIndex({
301 cacheTime: 600000,
302 hostname: 'http://www.sitemap.org',
303 sitemapName: 'sm-test',
304 sitemapSize: 1,
305 targetFolder: require('os').tmpdir(),
306 urls: ['http://ya.ru', 'http://ya2.ru']
307 // optional:
308 // callback: function(err, result) {}
309 });
310```
311
312### Example of overriding default xmlns* attributes in urlset element
313
314Also see 'simple sitemap with dynamic xmlNs' test in [tests/sitemap.js](https://github.com/ekalinin/sitemap.js/blob/master/tests/sitemap.test.js)
315
316```javascript
317var sitemap = sm.createSitemapIndex({
318 xmlns: 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'
319 });
320```
321
322### Example of news
323
324```javascript
325const sm = require('sitemap')
326const smi = new sm.SitemapItem({
327 url: 'http://www.example.org/business/article55.html',
328 news: {
329 publication: {
330 name: 'The Example Times',
331 language: 'en'
332 },
333 genres: 'PressRelease, Blog',
334 publication_date: '2008-12-23',
335 title: 'Companies A, B in Merger Talks',
336 keywords: 'business, merger, acquisition, A, B',
337 stock_tickers: 'NASDAQ:A, NASDAQ:B'
338 }
339})
340```
341
342Testing
343-------
344
345```bash
346➥ git clone https://github.com/ekalinin/sitemap.js.git
347➥ cd sitemap.js
348➥ make env
349➥ . env/bin/activate
350(env) ➥ make test
351./node_modules/expresso/bin/expresso ./tests/sitemap.test.js
352
353 100% 33 tests
354
355```
356
357License
358-------
359
360See [LICENSE](https://github.com/ekalinin/sitemap.js/blob/master/LICENSE)
361file.