Giter Club home page Giter Club logo

hapi-amp's Introduction

hapi-amp

Build Status Coverage Status

A simple hapi plugin that automates switching to an Accelerated Mobile Page (AMP) version of your view.

Install

npm install hapi-amp

Usage

Register the plugin as you normally would, along with hapi's vision plugin and your preferred view engine:

// index.js
await server.register(require('vision'));
await server.register(require('hapi-amp'));
server.views({
  engines: { html: require('vision-nunjucks') },
  path: `/views`
});

If you use the Rapptor server framework then you can accomplish the same thing with a configuration file:

# default.yaml
plugins:
  vision:
  hapi-amp:
views:
  path: '/views'
  engines:
    njk: 'vision-nunjucks'

For each view that you want to provide an AMP version for, just make a corresponding file with -amp at the end of the file name:

  • /views/homepage.html
  • /views/homepage-amp.html
  • /views/profile.html
  • /views/profile-amp.html

Then you can just render your views like you normally would:

return h.view('pages/homepage');

The plugin will detect any time a request contains an ?amp=1 query parameter and switch to the -amp version of the view if so. If unable to locate an -amp version of your view, it will just fall back to the original view.

Further Features

When rendering an AMP page, hapi-amp will also add the following to your view context:

  • __isAMP: true , indicating this is an amp page.
  • __AMPOriginal: <url>, so that your view will still have access to the original request.url.href before the plugin modifies it internally.

hapi-amp's People

Contributors

dawnerd avatar orthagonal avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

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.