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

metapatcher

v4.4.0

Published

HTML document head management library with convenient api. Manage social media tags, icons, device specific tags and event structured data with html meta tags and JSONLD documents.

Downloads

87

Readme

metapatcher

HTML document head management library with convenient api. Manage social media tags, icons, device specific tags and event structured data with html meta tags and JSONLD documents. Supports both browser and server environments. Fully typed.

It injects head tags based on features you enabled upon start. The features are device, browser, platform specific. You give the data you have on page load and it injects appropriately.

  • structuredData controls tags related to the Google Search Structured Data.
  • openGraphTags controls Open Graph tags which many social media sites rely to fetch page data
  • webAppManifest controls tags related to Web Application Manifest spec.
  • twitterTags controls meta tags related to Twitter Cards
  • msTags controls Microsoft device and browser specific tags including name, description and icons
  • appleTags controls Apple device specific tags such as icons, name and style

Install

npm install metapatcher

Usage

Import

import { metapatcher } from 'metapatcher'
// or const { metapatcher } = require('metapatcher') for commonjs

or inject with script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/metapatcher@4/dist/index.js"></script>

Configure

All features mentioned above are enabled by default. You can disable some of them:

metapatcher.configure(['openGraphTags', 'structuredData', 'twitterTags', 'msTags', 'appleTags'], { idPrefix: 'metapatcher' })
// configure also does some injections so you gonna call it anyway
metapatcher.configure()

Basic, High Level Methods

You have to use at least the following methods to create satisfying frontends:

// all parameters are optional
metapatcher.setProjectDetails({
    favicon: '/path/favicon.ico',
    name: 'My Project',
    url: 'https://example.com',
    robots: 'noindex, nofollow', // or "all" for example
    logo: '/path/logo.png',
    themeColor: '#333333',
    twitterSite: '@twitter',
    safariPinnedTab: { href: '/path/mark.svg', color: '#333333' },
    // icons need to follow this naming: *-[width]x[height].ext
    icons: ['/path/icon-16x16.png', '/path/icon-512x512.png', '/path/icon-150x150.png']
})

All of the parameters above are optional and has their own individual methods such as setFavicon, setIcons or setName.

Consider also the following methods at the project level:

metapatcher.setMsApplicationConfig (param: string | MetapatcherMsApplicationConfigAttrs)
metapatcher.setAppleStatusBarStyle (content: 'default' | 'black' | 'black-translucent' = 'default')
metapatcher.addPreload (param: string | MetapatcherPreloadAttrs)
metapatcher.addPrefetch (param: string | MetapatcherPrefetchAttrs)
metapatcher.addPreconnect (param: string | MetapatcherPreconnectAttrs)
metapatcher.addDnsPrefetch (param: string | MetapatcherDnsPrefetchAttrs)

Use the following methods from page to page:

// all parameters are optional
metapatcher.setPageDetails({
    title: 'Page | Project Name',
    description: 'Lorem ipsum.',
    path: '/en-us/some/page',
    image: '/media/page-cover.jpg',
    // both xx_XX and xx-XX syntax are okay for locales
    locale: 'en_US',
    canonical: 'https://example.com/en-us/some/page',
    // both xx_XX and xx-XX syntax are okay for hreflang
    localVersions: [
        { hreflang: 'en-US', href: '/en-us/some/page' }, 
        { hreflang: 'tr-TR', href: '/tr-tr/some/page' }],
    breadcrumb: [
        { title: 'Page', url: '/en-us/some/page' },
        { title: 'Some', url: '/en-us/some' },
        { title: 'Home', url: '/en-us' }
    ]
})

All of the parameters above are optional and has their own individual methods such as setPageTitle or setBreadcrumb.

Consider also the following methods at the page level:

metapatcher.setMobileVariant (param: string | MetapatcherMobileVariantLinkAttrs)

It will inject appropriate meta tags, link tags, jsonld scripts for specific platforms and devices depending on the enabled features.

To set any other kind of meta tags:

metapatcher.setMeta('google', 'notranslate')

Server-side Usage

The library has a simple isDomAvailable property which get its value upon init by a simple check typeof document !== 'undefined'. This property decides if the library should inject tags or collect them in memory. So, when you run the library in a non-browser environment, they will be kept in memory. You can print them all with:

metapatcher.dump()

It returns:

<meta id="metapatcher-msapplication-config" name="msapplication-config" content="none">
<meta id="metapatcher-apple-mobile-web-app-capable" name="apple-mobile-web-app-capable" content="yes">
<meta id="metapatcher-twitter-card" name="twitter:card" content="summary">
...

Low Level Usage, Custom Tags

There are methods that you can use to set custom meta tags, link tags and jsonld scripts without leaving the control of the library.

Set custom tags:

// all parameters are optional
metapatcher.set('meta', { id: 'my-id', name: 'viewport', content: 'width=device-width, initial-scale=1, viewport-fit=cover' })

metapatcher.setJsonLd('my-script', { abc: 'def' })

and remove later if neccessary:

metapatcher.removeOne('link[id="my-id"]')
metapatcher.removeMany('link[hreflang]')

Inject Script and Stylesheet Resources

await metapatcher.setScript({ id: 'my-script', src: 'https://cdn.jsdelivr.net/npm/metapatcher@4/dist/index.js', async: true })
// wait for global "metapatcher" to become available under window
await metapatcher.setScript({ id: 'my-script', src: 'https://cdn.jsdelivr.net/npm/metapatcher@4/dist/index.js', async: true }, { waitForLoad: 'metapatcher' })

await metapatcher.setStylesheet({ id: 'my-style', href: 'https://example.com/style.css' })

Supports Boolean Attributes

Many methods takes a parameter to be used as html attrs and you can always provide a boolean value for any attribute to render it as boolean html element attribute:

metapatcher.set('link', { rel: 'manifest', href: '/app.webmanifest', crossorigin: true })

Contributing

If you're interested in contributing, read the CONTRIBUTING.md first, please.


Version management of this repository done by releaser 🚀


Thanks for watching 🐬

Support me on Patreon