Fast and minimal JS server-side writer and client-side manager.

Overview

unihead

Fast and minimal JS <head> server-side writer and client-side manager.

Nearly every SSR framework out there relies on server-side components to update the <head>. This is the case for Next, Nuxt 2, Nuxt 3 and Remix. The problem with that approach is that the <head> becomes dependent on your entire component stack being server-side rendered first, or at least a big part of it, which is generally expensive and prevents you from streaming the <head> right away to the client.

unihead is a library conveniently packing both a server-side API, that lets you generate <head> elements programatically, and a client-side API that hydrates a data model from the rendered elements independently of the framework you're using (all vanilla DOM manipulation) and lets you mutate it and also reset it to its original state (useful for managing <head> inbetween client-side route navigation).

Install

npm install unihead --save

Server usage

The server module of this package supports the adoption of an alternative pattern where all data required for rendering <head> elements is fetched prior to any framework-level component rendering, so it can be streamed to the client right away.

The server-side <head> cannot be mutated, it must be created only once from an object.

import Head from 'unihead'

const head = new Head({
  title: 'Page Title'
  meta: [
    { name: 'twitter:title', content: 'Page Title' }
  ],
})

There are two methods available: render(), which produces a full string with all rendered <head> elements.

const head = new Head({ ... }).render()
const html = `<head>${head}</head><body>...</body>`

And stream(), which returns a Readable Node.js stream (built from an Iterator) that yields one <head> element at a time.

See a full streaming example here.

Client usage

If you're using a tool such as Vite to build your client application build, import the client module of the package and instantiate Head the same way it is done on the server.

A global registration distribution is also available, that lets you simply include unihead as the last script import of your <head> and have it globally available (and automatically instantiated) in the window object.

The client Head class will load the rendered <head> from the page internally in a data model and let you modify it or reset it to its orignal state if needed.

Import using a build tool

import Head from 'unihead/client'

const head = new Head()

Vanilla script include

<head>
  <!-- Head elements -->
  <script src="https://unpkg.com/unihead"></script>
</head>

In which case window.head is available immediately afterwards, automatically instantied from the Head class.

This option is provided for easily integrating unihead with any kind of vanilla HTML/JS application.

Mutation methods

The main difference between the server and client modules is that the latter allows you to mutate the data, i.e., change existing elements or add new ones if needed. API follows:

  • head.title=
  • head.base=
  • head.meta[].set()
  • head.link[].set()
  • head.meta[].push()
  • head.link[].push()

For standalone self-closing elements, use assignment:

head.title = 'Page title'
head.base = { href: 'https://...', target: '_blank' }

For collective self-closing elements, use set():

head.meta.set({ name: '...', content: '...' })

You can also use push(arr) to set multiple tags at once.

In the case of <meta> tags, name and property are used to uniquely identify tags and mutate them without having to add new ones if they already exist.

You can also remove collection items:

head.meta.remove({name: 'twitter:title'})

Finally, you can also fully reset any changes made:

head.reset()

Limitations

When compared to libraries like vueuse/head, which is integrated with a framework, you'll notice unihead is missing a few features:

  • There's no way to set <html> and <body> attributes.

  • There's no way to create <script> tags with content, just attributes.

  • Similarly, there's no way to create <style> tags.

unihead limits itself to either empty or self-closing elements under <head>.

You might also like...

WebVM is a server-less virtual Linux environment running fully client-side in HTML5/WebAssembly.

WebVM This repository hosts the source code of the https://webvm.io live demo page. WebVM is a server-less virtual Linux environment running fully cli

Jan 8, 2023

This is an email scheduler made using MERN stack. This repo contains client, server side is linked in readme

Email Scheduler Client This is an email scheduler client (server in different repository). It is made using react. Overview User can sign-up/sign-in,

Dec 3, 2022

A vanilla javascript library to generate Avataaars on the client or server side!

A vanilla javascript library to generate Avataaars on the client or server side!

Use the awesome Avataaars Library by Pablo Stanley (avataaars.com) in any javascript application. This Project uses parts of the Dicebear Avatars Libr

Dec 4, 2022

Gatsby-starter-minimal-blog - Typography driven, feature-rich blogging theme with minimal aesthetics.

Gatsby-starter-minimal-blog - Typography driven, feature-rich blogging theme with minimal aesthetics. Includes tags/categories support and extensive features for code blocks such as live preview, line numbers, and line highlighting.

Jan 29, 2022

A project for experimenting with Server Sent Events (SSE), a way of communication going from server to client.

A project for experimenting with Server Sent Events (SSE), a way of communication going from server to client.

May 16, 2022

NestJS implementation of client and strategy hasn't enough features for work with RabbitMQ so i developed this one (basically, just a wrapper for amqp-connection-manager)

NestJS RabbitMQ Client and strategy NestJS implementation of client and strategy hasn't enough features for work with RabbitMQ so i developed this one

Sep 6, 2022

Monolithic repo for api server, image server, web server

Onsecondary Market Deployed at https://market.onsecondary.com Monolithic repo for api server, image server, web server TODO -use a script to cull expi

Jan 11, 2022

A minimal yet powerful HTTP client/API testing tool made for speed.

A minimal yet powerful HTTP client/API testing tool made for speed.

req req is a lightweight, minimal yet powerful HTTP client slash API testing tool designed for speed. Contents Features Installation Documentation Con

Aug 29, 2022

A minimal, mobile-ready, album-centered music client for subsonic compatible music servers.

A minimal, mobile-ready, album-centered music client for subsonic compatible music servers.

Soniclair An album-centered subsonic client for PWA, Android, Android TV and Desktop SonicLair is a minimal, mobile-ready, album-centered music client

Dec 13, 2022
Owner
Jonas Galvez
Engineering Manager @nearform. Frontend Tooling @fastify. Hardcore minimalist and ponderer of things. Former Nuxt.js core team member.
Jonas Galvez
toXML - Pure JavaScript XML Writer

toXML - Pure JavaScript XML Writer Live Demo: https://kawanet.github.io/from-xml/ FEATURES Simple: single writer function toXML() which returns XML st

Yusuke Kawasaki 5 Apr 1, 2022
writer.js is a simple text editor in the web

✍ writer.js writer.js is a simple text editor in the web ❓ About writer.js: This is a simple and lightweight editor for the web demo: https://alirezak

alireza kefayati 9 Sep 29, 2022
A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fast, stores the Cache before stopping and restores it again!

remote-map-cache A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fa

Tomato6966 8 Oct 31, 2022
Fully-typed utilities for defining, validating and building your document

zhead Typed utilities for defining, validating and building best-practice document <head>'s. Status: Pre-release Please report any issues ?? Made poss

Harlan Wilton 70 Dec 21, 2022
A simple guide to HTML elements

?? HEAD A simple guide to HTML <head> elements Table of Contents Recommended Minimum Elements Meta Link Icons Social Facebook Open Graph Twitter Card

Josh Buchea 29.5k Jan 1, 2023
Quickly create an interactive HTML mock-up by auto sourcing lorem ipsum/images generators, with minimal html markup, and no server side code

RoughDraft.js v0.1.5 Quickly mockup / prototype HTML pages with auto-generated content, without additional JavaScript or server side code. <section>

Nick Dreckshage 464 Dec 21, 2022
A minimal routing library designed to sit on top of Bun's fast HTTP server.

siopao A minimal routing library designed to sit on top of Bun's fast HTTP server. Based on Radix Tree. Sio=Hot Pao=Bun Installation bun add siopao Us

Robert Soriano 69 Nov 8, 2022
A devtool improve your pakage manager use experience no more care about what package manager is this repo use; one line, try all.

pi A devtool improve your pakage manager use experience no more care about what package manager is this repo use; one line, try all. Stargazers over t

tick 11 Nov 1, 2022
Meogic-tab-manager is an extensible, headless JavaScript tab manager framework.

MeogicTabManager English document MeogicTabManager是一个有可拓展性的、headless的JavaScript标签页管理框架。 MeogicTabManager旨在提供可自由组装页面框架、自定义页面组件、甚至覆盖框架自带事件响应的开发体验。 Meogi

meogic-tech 5 Oct 8, 2022
Free, open-source client or server-side APIs to "lint" user input.

passbird Free, open-source client or server-side APIs to lint user input. Right now, you can check type for an email address i.e., either of disposabl

Vaibhav Pandey 1 Dec 26, 2021