koa-meta

Meta <title>
and <meta name="description">
middleware for Koa and Lad
Table of Contents
Install
npm:
npm install koa-meta
yarn:
yarn add koa-meta
Usage
Use middleware:
const path = require('path');
const views = require('koa-views');
const Meta = require('koa-meta');
app.use(views(path.join(__dirname, 'views')));
const meta = new Meta({
'/': [ 'Home', 'Our home page description' ],
'/contact', [ 'Contact', 'Contact us with questions' ]
});
app.use(meta.middleware);
app.use((ctx, next) => {
ctx.render('home');
});
Therefore in your views you can render the meta data easily:
doctype html
html
head
title= meta.title
meta(name="description", content=meta.description)
Programmatically get a meta object/translated version of title
and description
:
const Meta = require('koa-meta');
const meta = new Meta({
'/': [ 'Home', 'Our home page description' ],
'/posts': [ 'Posts', 'Posts by our team' ]
});
console.log(meta.getByPath('/posts/123456'));
Translation Support
This package supports translation out of the box.
It checks for a function set on ctx.request.t
and utilizes that function to translate based off the request's locale.
Child Path Support
This package supports parent meta data lookup for children of paths.
This means if you define in your configuration a path of /posts
and a request is made to /posts/123456
(with this path not being defined in your configuration), then it will use /posts
definition for /posts/123456
.
Error Catching
By default this package will throw an error if a child path was found that does not have a parent defined.
This is extremely useful for retaining quality control with your configuration.
Contributors
License
MIT © Nick Baugh