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 datawebAppManifest
controls tags related to Web Application Manifest spec.twitterTags
controls meta tags related to Twitter CardsmsTags
controls Microsoft device and browser specific tags including name, description and iconsappleTags
controls Apple device specific tags such as icons, name and style
npm install metapatcher
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>
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()
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')
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">
...
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]')
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' })
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 })
If you're interested in contributing, read the CONTRIBUTING.md first, please.
Version management of this repository done by releaser 🚀
Thanks for watching 🐬