Unhead: Universal Head Manager

3.0.4 · active · verified Sun Apr 19

Unhead is a full-stack, framework-agnostic head manager designed to simplify and standardize how metadata (like `<title>`, `<meta>`, `<link>`) is managed in JavaScript applications. It is currently at version 3.0.4, with frequent patch releases addressing bug fixes and minor improvements, and major versions (like the recent v3.0.0) introducing significant architectural changes, such as the rebuild for streaming SSR. Its key differentiators include comprehensive framework agnosticism, reactive head management, robust server-side rendering support with a focus on streaming capabilities, SEO-friendliness, type safety via TypeScript, and a lightweight, tree-shakable design optimized for performance with minimal runtime overhead. It integrates seamlessly with popular frameworks through dedicated packages like `@unhead/vue` and `@unhead/react`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a head instance, add common head elements (title, meta, link, script) using `useHead`, and then synchronously render the collected head and body tags, which is typically done in a Server-Side Rendering (SSR) context.

import { createHead, useHead } from 'unhead';

// 1. Create a head instance. For SSR, use `createHead` from 'unhead/server'.
const head = createHead();

// 2. Add reactive head entries using `useHead`.
// This example updates the title and adds a meta description.
useHead({
  title: 'My Dynamic App Title',
  meta: [
    { name: 'description', content: 'This is a dynamic description for my awesome application.' },
    { property: 'og:title', content: 'Open Graph Title' }
  ],
  link: [
    { rel: 'canonical', href: 'https://example.com/my-page' }
  ],
  script: [
    { src: 'https://unpkg.com/some-script.js', defer: true, body: true }
  ]
}, { head });

// 3. For SSR, render the head tags.
// In a real SSR environment, you'd call this after all components have rendered.
const { headTags, bodyTags } = head.renderSync();

console.log('Generated Head Tags:\n', headTags);
console.log('Generated Body Tags (scripts in body):\n', bodyTags);

// Output example (simplified):
// <title>My Dynamic App Title</title>
// <meta name="description" content="This is a dynamic description for my awesome application.">
// ...
// <script src="https://unpkg.com/some-script.js" defer></script>

view raw JSON →