Giter VIP home page Giter VIP logo

set-head's Introduction

SET-HEAD
API stability Standard NPM version Downloads Gitter Chat

Isomorphic <head> updating and rendering utility.

Installation

npm install set-head

Example

import Head from 'set-head'

// Create a new head element, this should be done once per request.
const head = new Head()

// Add elements to head via method chaining.
head
  .meta({ charset: 'utf8' })
  .title('My App')
  .base({ href: '/admin/' })
  .meta({ name: 'description' content: 'Cool stuff' })
  .link({ rel: 'stylesheet', href: 'index.css' })
  .script({ async: true, src: 'index.js' })

Overriding

set-head makes overriding default header elements easy. If you invoke head again later it will replace any existing similar element. The module uses special keying scheme found in ./src/base.js under const KEYS to understand which elements to replace and which to add.

// Setting defaults somewhere (perhaps in a middleware or plugin.)
head
  .title('My App')
  .meta({ name: 'author', content: 'Dylan Piercey' })
  .meta({ name: 'description': content: 'Welcome to the site' })
  .link({ rel: 'stylesheet', href: 'index.css' })

// The later in a specific route you can continue chaining like so to override.
head
  .title('My App > My sub page')
  .meta({ name: 'description', content: 'Sub page description' })

// And then if we render we get (formatted for clarity)
head.renderToString() === html`
  <title>My App > My sub page</title>
  <meta name="author" content="Dylan Piercey">
  <meta name="description" content="Sub page description">
  <link rel="stylesheet" href="index.css">
`

Browser Rendering

To render to the browser, simply invoke 'render' after all of the head elements have been set.

head.render()

Server Rendering

Server side rendering is accomplished by invoking renderToString after all of the head elements have been set.

export default (req, res) => {
  res.end(`
    <!doctype html>
    <html>
      <head>${head.renderToString()}</head>
      <body>
        ...
      </body>
    </html>
  `)
}

Contributions

  • Use npm test to run tests.

Please feel free to create a PR!

set-head's People

Contributors

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