npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hooked-head

v0.4.1

Published

Hooks to populate the html head.

Downloads

263

Readme

Hooked-Head

npm version Bundle size codecov

This project aims at providing a set of hooks to populate <meta>, ... for each page. With crawlers now supporting client-side alterations it's important to support a fallback model for our <head> tags. The dispatcher located in this library will always make a queue of how we should fallback, ... This way we'll always have some information to give to a visiting crawler.

npm i --save hooked-head
## OR
yarn add hooked-head
import {
  useMeta,
  useLink,
  useLang,
  useTitle,
  useTitleTemplate,
} from 'hooked-head';

const App = () => {
  // Will set <html lang="en">
  useLang('en');

  // Will set title to "Welcome to hooked-head | 💭"
  useTitleTemplate('%s | 💭');
  useTitle('Welcome to hooked-head');

  useMeta({ name: 'author', content: 'Jovi De Croock' });
  useLink({ rel: 'me', href: 'https://jovidecroock.com' });

  return <p>Hooked-Head</p>;
};

Preact

If you need support for Preact you can import from hooked-head/preact instead.

Gatsby

There's a plugin that hooks in with Gatsby and that will fill in the meta, ... in your build process.

Hooks

This package exports useTitle, useTitleTemplate, useMeta, useLink and useLang. These hooks are used to control information conveyed by the <head> in an html document.

useTitle

This hook accepts a string that will be used to set the document.title, every time the given string changes it will update the property.

useTitleTemplate

This hook accepts a string, which will be used to format the result of useTitle whenever it updates. Similar to react-helmet, the placeholder %s will be replaced with the title.

useMeta

This hook accepts the regular <meta> properties, being name, property, httpEquiv, charset and content.

These have to be passed as an object and will update when content changes.

useLink

This hook accepts the regular <link> properties, being rel, as, media, href, sizes and crossorigin.

This will update within the same useLink but will never go outside

useLang

This hook accepts a string that will be used to set the lang property on the base <html> tag. Every time this string gets updated this will be reflected in the dom.

SSR

We expose a method called toStatic that will return the following properties:

  • title, the current title dictated by the deepest useTitleTemplate and useTitle combination
  • lang, the current lang dictated by the deepest useLang
  • metas, an array of unique metas by keyword (property, ...)
  • links, the links aggregated from the render pass.

The reason we pass these as properties is to better support gatsby, ...

If you need to stringify these you can use the following algo:

const stringify = (title, metas, links) => {
  const visited = new Set();
  return `
    <title>${title}</title>

    ${metaQueue.reduce((acc, meta) => {
      if (!visited.has(meta.charset ? meta.keyword : meta[meta.keyword])) {
        visited.add(meta.charset ? meta.keyword : meta[meta.keyword]);
        return `${acc}<meta ${meta.keyword}="${meta[meta.keyword]}"${
          meta.charset ? '' : ` content="${meta.content}"`
        }>`;
      }
      return acc;
    }, '')}

    ${linkQueue.reduce((acc, link) => {
      return `${acc}<link${Object.keys(link).reduce(
        (properties, key) => `${properties} ${key}="${link[key]}"`,
        ''
      )}>`;
    }, '')}
  `;
};
import { toStatic } from 'hooked-head';

const reactStuff = renderToString();
const { metas, links, title, lang } = toStatic();
const stringified = stringify(title, metas, links);

const html = `
  <!doctype html>
    <html lang="${lang}">
      <head>
        ${stringified}
      </head>
      <body>
        <div id="content">
          ${reactStuff}
        </div>
      </body>
  </html>
`;

Goals

  • [x] React support
  • [x] Add majority of types for meta and link.
  • [x] Concurrent friendly
  • [x] Preact support
  • [x] Support <link>
  • [x] Stricter typings
  • [x] Document the hooks
  • [x] Document the dispatcher
  • [x] SSR support
  • [x] Consider moving from doc.title = x to inserting <title>x</title>
  • [x] Golf bytes
  • [ ] improve typings, there are probably missing possibilities in types.ts