Socket
Socket
Sign inDemoInstall

preact-head-tag

Package Overview
Dependencies
4
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    preact-head-tag

A tiny ssr-compatible preact component for managing head with preact-portal


Version published
Weekly downloads
4
Maintainers
1
Install size
246 kB
Created
Weekly downloads
 

Readme

Source

A very tiny SSR-compatible head component for Preact using preact-portal.

It's around 1.5 kB including preact-portal and only ~500 bytes by itself.

Heavily inspired by react-head by tizmagic.

Installation

npm install preact-head-tag

Usage

Client

import { h, Component } from "preact";
import HeadTag from "preact-head-tag";

const HelloWorld = () => (
  <div>
    <HeadTag tag="title">About this website</HeadTag>
    <HeadTag tag="meta" name="example" content="whatever" />
    <h1>Hello world!</h1>
  </div>
);
// Alternatively you can use the following components for convenience:
import { Title, Meta, Style, Link } from "preact-head-tag";

const HelloWorld = () => (
  <div>
    <Title>About this website</Title>
    <Meta name="example" content="whatever" />
    <h1>Hello world!</h1>
  </div>
);

Server

import { h } from "preact";
import renderToString from "preact-render-to-string";
import { TagCollector } from "preact-head-tag/server";

const headTags = [];

// Wrap you App component in the TagCollector
const app = renderToString(
  <TagCollector headTags={headTags}>
    <App />
  </TagCollector>
);

// Put the extracted headTags inside the head of the html.
// headTags is an array of strings so use headTags.join("")
res.send(`
  <!doctype html>
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      ${headTags.join("")}
    </head>
    <body>
      <div id="root">${app}</div>
    </body>
  </html>
`);

License

MIT

Keywords

FAQs

Last updated on 22 Jun 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc