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

@app-elements/helmet

v3.0.6

Published

Basic Component to set title and meta tags in your HTML.

Downloads

84

Readme

Helmet

A very basic means of setting and updating document title and meta tags from your (P)React components.

Installation

npm install --save @app-elements/helmet

Usage

Helmet can be declared in your JSX many times. Each time, the result will be merged with the previous. It follows the order of your React tree, meaning the last (or "most nested") Helmet declared in your React tree will take precedence.

Say, in your root Component:

import Helmet from '@app-elements/helmet'

<Helmet
  title='Welcome'
  titleTemplate='PWA Starter | %s'
  defaultTitle='Welcome'
/>

Then, in some nested child Component:

<Helmet
  title='Nested Title'
/>

The resulting object representation would be:

{
  "title": "Nested Title",
  "titleTemplate": "PWA Starter | %s",
  "defaultTitle": "Welcome"
}

The document.title will be updated each time a Helmet instance is rendered. However, the meta tags are only rendered once (and still give precedence to the last Helmet in the tree). This means the meta tags are set properly on initial load and when rendering on the server. So, when you share a link on social media, the correct OG tags or other meta tags will be used. But, the meta tags will not update after initial load. From what I can tell, this has no negative affect.

Server-Side Rendering

You can see full working code in PWA Starter, but here are the key bits:

import render from 'preact-render-to-string'

import Helmet, {rewind} from '@app-elements/helmet'

const head = render(<Helmet {...rewind()} />).slice(5, -6) // slice off `<head>`, `</head>` tags

// ...
app.get('*', (req, res) => {
  /// ...
  res.send(`
    <html lang="en">
      <head>
        <base href="/">
        <meta charset="utf-8">
        <link rel="stylesheet" href="./bundle.css" />
        ${head}
      </head>
      <body>
        <div class='main-app-container'>${html}</div>
        <script>window.__initial_store__ = ${JSON.stringify(state)};</script>
        <script src="./bundle.js"></script>
      </body>
    </html> 
  `) 
})

Props

| Prop | Type | Default | Description | |------------------------|-------------|---------------|---------------------| | title | String | None | The title to set. Will be used inside the titleTemplate string. | titleTemplate | String | '%s' | Format your title strings. %s will be replaced with current title. Ex. '%s | My Cool Site' | defaultTitle | String | None | The default value to replace %s with in your titleTemplate. Ex. 'Home' | meta | Array | None | An array of objects representing <meta /> tags. Supports name, property, content attributes.