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

ngx-metafrenzy

v8.1.0

Published

Angular 13 module for setting head tags such as title, meta, and link.

Downloads

343

Readme

Coverage Status

ngx-metafrenzy

This angular module will provide help in dynamically setting the head-tags such as meta/link/title. It works with server-side rendering!

Requirements

  • Angular 13+

Older versions of Angular:

  • use v7.0.0 for Angular 12
  • use v6.0.0 for Angular 10
  • use v5.1.0 for Angular 9
  • use v4.1.0 for Angular 8
  • use v3.2.0 for Angular 7
  • use v2.4.1 for Angular 6

Installation

$ npm install ngx-metafrenzy

or

$ yarn add ngx-metafrenzy

Usage

Setup the NgModule first. Always import MetafrenzyModule. Import MetafrenzyGuard if you want to use the guard functionality. See the data section in the example below for more on the syntax.

import { MetafrenzyModule, MetafrenzyGuard } from 'ngx-metafrenzy';

@NgModule({
    declarations: [AppComponent],
    imports: [
        ...
        MetafrenzyModule.forRoot(),
        RouterModule.forRoot([
            {
                path: '',
                component: AppComponent,
                canActivate: [MetafrenzyGuard],
                data: { 
                    metafrenzy: {
                        title: 'My Title',
                        tags: [
                            {
                                name: 'og:title',
                                content: 'My title'
                            }, {
                                name: 'og:description',
                                content: 'My description'
                            }
                        ],
                        links: [
                            {
                                rel: 'canonical',
                                href: 'http://localhost/'
                            }
                        ]
                    }
                }
            }
        ])
        ...
    ],
    ...
})

When navigating between routes you can also add "canDeactivate" to the data object. This will try to reset the metatags, which were set in the previous route. Use this with caution as you might remove tags, which should be global or not removed. See remove-functions in the service class to remove single tags.

...
    canDeactivate: [MetafrenzyGuard],
...

You can use the service class MetafrenzyService in any component. See example below:

import { MetafrenzyService } from 'ngx-metafrenzy';

@Component({
    ...
})
export class AppComponent {

    constructor(private readonly metafrenzyService: MetafrenzyService) {
        this.metafrenzyService.setTitle('My title');

        this.metafrenzyService.setMetaTag('og:title', 'My title');

        this.metafrenzyService.setLinkTag({
            rel: 'canonical',
            href: 'http://localhost/'
        });
    }

}

As shown above the title can be set using setTitle and a meta tag using setMetaTag with the content as parameters. The link tag is a little different since you pass an object to match all possible variations of attributes on the link tag. Properties in this link object could be:

charset, crossorigin, href, hreflang, media, rel, rev, sizes, target, type

Service class functions

// Set title tag
setTitle('')

// Returns the current title as a string
getTitle()

// Set meta tag from specified name and content
setMetaTag('')

// Returns the content value of a metatag matching the selector
getMetaTag('')

// Remove a single meta tag (eg. removeMetaTag('name="test"'))
removeMetaTag('')

// Set the value of meta charset
setMetaCharsetTag('')

// Set link tag from specified object
setLinkTag({
    charset: '';
    crossorigin: '';
    href: '';
    hreflang: '';
    media: '';
    rel: '';
    rev: '';
    sizes: '';
    target: '';
    type: '';
})

// Remove link tags
removeLinkTags(() => true);

// Set title tag and og:title to the same value
setAllTitleTags('')

// Set meta description and og:description to the same value
setAllDescriptionTags('')

// Set canonical url
setCanonical('')

// Set robots tag
setRobots('')

// Set all open graph tags
setOpenGraph({
    title: '', 
    description: '',
    type: '',
    url: '',
    image: '',
    site_name: ''
});

// Set multiple tags with one call
setTags({
    title: '', // setting title and og:title
    description: '', // setting meta description and og:description
    url: '', // setting canonical and og:url
    robots: '', // setting robots
    image: '' // setting og:image:url
});

Docker

For development in a docker container run:

$ docker-compose up --build -d

And go to http://localhost:4200

License

This package is open-sourced software licensed under the MIT license