Giter Club home page Giter Club logo

hapi-generate-sitemap's Introduction

hapi-generate-sitemap

hapi-generate-sitemap is a simple hapi plugin that automates generating and serving your sitemap.html / sitemap.xml files.

Install

npm install hapi-generate-sitemap

Basic Usage

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.

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 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>

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:

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.

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:

    {
      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.

hapi-generate-sitemap's People

Contributors

dawnerd avatar ecwillis avatar jgallen23 avatar orthagonal avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

hapi-generate-sitemap's Issues

HapiJS v18 Support?

Hello, i've tried to use this plugin with "@hapi/hapi": "^18.3.1" but i get an empty sitemap (screenshot below) despite setting up as best i could follow - before i ask for more help i wondered if this plugin officially supports hapi v18 ? Thanks!

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.