# koa-head

> A document head manager middleware for koa

Latest version **2.0.0** (published 2019-05-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install koa-head
pnpm add koa-head
yarn add koa-head
bun add koa-head
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-05-29 |
| First published | 2018-12-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Remek Ambroziak |
| Maintainers | reod |

## Links

- npm: https://www.npmjs.com/package/koa-head
- Repository: https://github.com/reod/koa-head
- Homepage: https://github.com/reod/koa-head#readme
- Issues: https://github.com/reod/koa-head/issues
- npm.io page: https://npm.io/package/koa-head

## Recent versions

- 2.0.0 (latest) — 2019-05-29
- 1.2.0 — 2019-04-25
- 1.1.1 — 2018-12-20
- 1.1.0 — 2018-12-20
- 1.0.3 — 2018-12-06
- 1.0.2 — 2018-12-06
- 1.0.1 — 2018-12-05
- 1.0.0 — 2018-12-05

## README

[![Known Vulnerabilities](https://snyk.io/test/github/reod/koa-head/badge.svg?targetFile=package.json)](https://snyk.io/test/github/reod/koa-head?targetFile=package.json)

# koa-head

A document head manager middleware for koa.

## Installation

`npm i koa-head`

## TL;DR example

**note:** this package can be used as native ES6 module but has fallback to CommonJS `require`.

```js
import Koa from "koa";
import koaHead from "koa-head";

const app = new Koa();

app
  .use(koaHead())
  .use(async (ctx, next) => {
    ctx.documentHead.setTitle("Title for my webpage");
    ctx.documentHead.addMetaTag({
      name: "twitter:card",
      content: "summary_large_image"
    });
    ctx.documentHead.addLink({ rel: "canonical", href: "index.html" });
    ctx.documentHead.addStyle("body { background: aliceblue; }");
    ctx.documentHead.addScript("{ fixture: 'test fixture' }");

    await next();
  })
  .use(ctx => {
    const documentHead = ctx.documentHead.toHTML();
    const userData = { name: "John Doe" };

    await ctx.myAwesomeLayoutEngine("user-view", {
      documentHead,
      userData
    });
  });

app.listen(3333);
```

will make `documentHead` variable to contain:

```html
<title>Title for my webpage</title>
<meta name="twitter:card" content="summary_large_image" />
<link rel="canonical" href="index.html" />
<style type="text/css">
  body {
    background: aliceblue;
  }
</style>
<script type="text/javascript">
  {
    fixture: "test fixture";
  }
</script>
```

so you can use it in a place in your layout.

## Available methods

### `.setTitle( string | object )`

Set document title.

### `.addMetaTag( object )`

Add `<meta />` tag.

### `.addLink( object )`

Add `<link />` tag.

### `.addStyle( string | object )`

Add `<style />` tag.

### `.addScript( string | object )`

Add `<script />` tag.

### `.toHtml()`

Render all set content to coresponding HTML tags.

## Middleware factory function config

| Option                   | Description                                                                                                                        | Default value          |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------- |
| `ctxNamespace`           | Name under which middleware is exposed in `cxt` object and is used by other middlewares i.e. `ctx.documentHead.setTitle('Hello')`. | `'documentHead'`       |
| `stateNamespace`         | Name under which middleware stores values in `ctx.state`                                                                           | `'documentHead'`       |
| `documentTitleFormatter` | If set, all values passed to `.setTitle()` function will pe parsed by this formatter.                                              | `title => title`       |
| `toHtml`                 | Config for toHtml function.                                                                                                        | `{ [default_values] }` |
| `toHtml.tagSeparator`    | Separator between tags inside one group.                                                                                           | `\n`                   |
| `toHtml.groupSeparator`  | Separator between group of tags.                                                                                                   | `\n\n`                 |

---
_Source: https://npm.io/package/koa-head · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
