hapi-generate-sitemap is a simple hapi plugin that automates generating and serving your sitemap.html / sitemap.xml files.
npm install hapi-generate-sitemap
await server.register(require('hapi-generate-sitemap'), {});
server.route({
method: 'get',
path: '/foopath1',
config: {
plugins: {
sitemap: true
}
},
handler(request, h) {
return { success: true };
}
});
Now GET /sitemap.html and it will return:
<ul>
<li><a href="http://localhost:8080/foopath">http://localhost:8080/foopath</a></li>
</ul>
GET /sitemap.xml will return:
<?xml version="1.0" encoding="UTF-8"?>
<url>
<loc>http://localhost:8080/foopath</loc>
</url>
</xml>
and GET /sitemap.json will give:
[{
path: '/foopath',
}]
Note that you have to have sitemap: true in your route config, or hapi-generate-sitemap will assume you do not want to show that route.
hapi-generate-sitemap also supports populating a sitemap template so you can customize the appearance of your HTML sitemap and render it with your view engine:
Say you are using handlebars for rendering and you have a template called sitemap_template.html:
<style>
a {
color: green;
}
</style>
<h1>My Site Looks Like:</h1>
{{#each sitemap}}
<a href="{{url}}">{{url}}</a>
{{/each}}
You can have hapi-generate-sitemap use this template:
server.views({
engines: { html: require('handlebars') },
relativeTo: __dirname,
path: 'views'
});
await server.register([
{
// hapi's template rendering library:
plugin: require('vision'),
},
{
plugin: require('hapi-generate-sitemap'),
options: {
htmlView: 'sitemap_template'
}
},
]);
Now when you GET /sitemap.html the server will pass the context to sitemap_template.html:
{
sitemap: [
{
url: 'http://localhost:8080/foopath',
section: 'none',
lastmod: '2005-01-01',
changefreq: 'monthly',
priority: 0.8
}
]
}
And you will get back something like:
<style>
a {
color: green;
}
</style>
<h1>My Site Looks Like:</h1>
<a href="http://localhost:8080/foopath">
http://localhost:8080/foopath
</a>
You can pass these options as query parameters when you HTTP GET the sitemap:
- meta
Pass a positive value to this to include additional route metadata. For example, if you have a route like this:
server.route({
method: 'get',
path: '/path1',
config: {
plugins: {
sitemap: {
section: 'Interviews',
lastmod: '2005-01-01',
changefreq: 'monthly',
priority: 0.8,
}
}
},
handler(request, h) {
return { success: true };
}
});
GET sitemap.json?meta=1 will return:
[{
path: '/path1',
section: 'Interviews',
lastmod: '2005-01-01',
priority: 0.8,
changefreq: 'monthly',
}]
- all
By default hapi-generate-sitemap will skip routes that don't have 'sitemap: true' specified in their route config. Passing the all query parameter will ignore this and list all routes.
- limit
Limits the number of entries to show, eg ?limit=10 will only show ten entries.
Pass these options when you register() the plugin with hapi:
-
videoPages
A function which returns an array of videos on your site. Each video should be returned in the format:
{ url: '/the-video-url', video: { title: 'a car video', thumbnail_loc: 'car.png', description: 'description of a car video', content_loc: 'http://youtube.com/1234' }
and the video data will be listed as:
<video:video> <video:title>a car video</video:title> <video:description>description of a car video</video:description> <video:thumbnail_loc>car.png</video:thumbnail_loc> <video:content_loc>http://youtube.com/1234</video:content_loc> </video:video>
-
htmlView
Name of the template to use for rendering the HTML version of the sitemap, will use the built-in template if not specified.
-
forceHttps
Forces each listed route to be 'https', default is false.
-
excludeTags
An array listing tags to be ignored. Routes containing one or more of these tags will not appear in the sitemap.
-
excludeUrls
An array of urls to be ignored. These routes wiill not appear in the sitemap.
-
additionalRoutes
A function that returns a list of any additional routes you want to be listed on your sitemap.
-
dynamicRoutes
hapi allows you to specify routes that contain dynamic path parameters, for example /user/{userName} will match both /user/roberto and /user/jin. Since hapi does not know all the possible path options for dynamic parameters, you can pass a dynamicRoutes function to manage these. dynamicRoutes will take in the current path and the request (for access to query values) as parameters and should return a list of routes that mapping for the dynamic route like so:
function dynamicRoutes(path, request) { const routes = { '/path/{param}': [ { path: '/path/param1', lastmod: '2005-01-01', changefreq: 'monthly', priority: 0.8, }, '/path/param2', '/path/param3' ] }; return (routes[path]) ? routes[path] : [];
-
getRouteMetaData
You can also pass a getRouteMetaData function to augment or override the path information contained by the server. The current page data will be passed to the function. If the function returns a false value then there is no effect. If the function returns an object, it will be combined with the existing page object, matching values that also exist in page will be over-written by the metadata:
getRouteMetaData(page) {
if (page.path === '/path1') {
return {
lastmod: '2005-01-01',
priority: 0.8,
};
}
return false;
}
-
endpoint
The root path where you can get the sitemap, default is "/sitemap".
-
logRequest
Specifies whether to log each request for the sitemap, default is false.
-
maxPerPage
Max number of route entries to return per page, default is 1000.