Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/firstandthird/hapi-generate-sitemap
https://github.com/firstandthird/hapi-generate-sitemap
hapi-plugin hapi-v17 has-tests needs-coverage
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/firstandthird/hapi-generate-sitemap
- Owner: firstandthird
- License: mit
- Created: 2017-10-19T20:17:15.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-03-09T02:18:05.000Z (almost 4 years ago)
- Last Synced: 2024-04-16T00:42:30.222Z (9 months ago)
- Topics: hapi-plugin, hapi-v17, has-tests, needs-coverage
- Language: JavaScript
- Size: 64.5 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hapi-generate-sitemap
hapi-generate-sitemap is a simple [hapi](https://hapi.dev/) plugin that automates generating and serving your sitemap.html / sitemap.xml files.
## Install
```
npm install hapi-generate-sitemap
```## Basic Usage
```js
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:
```
```
GET _/sitemap.xml_ will return:
```xml
http://localhost:8080/foopath
```
and GET _/sitemap.json_ will give:
```js
[{
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.
## Advanced Usage
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](https://handlebarsjs.com/) for rendering and you have a template called _sitemap_template.html_:
```html
a {
color: green;
}
My Site Looks Like:
{{#each sitemap}}
{{url}}
{{/each}}
```
You can have hapi-generate-sitemap use this template:
```js
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_:
```js
{
sitemap: [
{
url: 'http://localhost:8080/foopath',
section: 'none',
lastmod: '2005-01-01',
changefreq: 'monthly',
priority: 0.8
}
]
}
```
And you will get back something like:
```html
a {
color: green;
}
My Site Looks Like:
http://localhost:8080/foopath
```
## Query Options
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:
```javascript
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:
```javascript
[{
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.
## Plugin Options
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:
```js
{
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:
```html
a car video
description of a car video
car.png
http://youtube.com/1234
```
- __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:
```js
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:
```js
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.