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

@ngxuniversaltools/seo

v2.1.0

Published

Provides helper functions to manipulate meta tags, titles and JsonLD microdata. Much of the code is based on the [ngx-seo](https://github.com/samvloeberghs/kwerri-oss/tree/master/projects/ngx-seo) package.

Downloads

75

Readme

@ngxuniversaltools/seo

Provides helper functions to manipulate meta tags, titles and JsonLD microdata. Much of the code is based on the ngx-seo package.

Installation

npm install @ngxuniversaltools/seo

Add the following to your app.module.ts:

import {SeoModule} from '@ngxuniversaltools/seo';

imports: [
  SeoModule
]

In Standalone projects you can simply use SeoService without importing the module.

Add the directive to every instance of <router-outlet> in your application:

<router-outlet seoOutlet></router-outlet>

Usage

Setting title and meta tags

The SeoService provides methods to set different properties:

  • setTitle(title: string) - Sets the title of the page
  • setJsonLd(jsonLd: JsonLd[]) - Sets the JsonLD microdata.
  • setMeta(name: string, content: string) - Sets a meta tag with the given name and content. Overwrites existing meta tags with the same name if present.
  • setMetas(values: { [ key: string ]: string }) - Allows settings multiple meta tags at once

Using PageAbstract

This opinionated part of the library can be used to allow components to easily provide metadata for the page. The component declared in your route should extend the PageAbstract class and override properties when applicable. Finally you can use @ngrx/effects to subscribe to the page$ observable and set the title and meta tags.

Example:

{
    path: "category",
    component: CategoryComponent,
}

Your component should look like this:

import {PageAbstract} from '@ngxuniversaltools/seo';
import {of} from 'rxjs';

@Component({
  selector: 'app-category',
  templateUrl: './category.component.html',
  styleUrls: ['./category.component.scss']
})
export class CategoryComponent extends PageAbstract {
  title = of('Category'); // Simple static title
  description = this.translate.get("category.description"); // Translated description
}

You must create subscribers to actually set the title and meta tags. If you use @ngrx/effects this can be easily achieved:

export class PageEffects {
  title$ = createEffect(() => this.seo.page$.pipe(
    switchMap(page => page?.title || of("")),
    map(title => title ? [title, "My cool website"].join(" | ") : "My cool website"),
    tap(({title}) => this.seo.setTitle(title))
  ), {dispatch: false});

  constructor (private seo: SeoService) {
  }
}

Extending PageAbstract

You may need additional properties to be controlled by the page component. This can be achieved by creating your own PageAbstract class:

export class MyAppPageAbstract extends PageAbstract {
  image = of("https://example.com/image.png");  
}

export class CategoryComponent extends MyAppPageAbstract {
  title = of('Category'); // Simple static title
  description = this.translate.get("category.description"); // Translated description
  image = this.api.getFirstPost().pipe(map => map.image); // Dynamic image
}

Finally add an effect to set og:image meta tags:

export class PageEffects {
image$ = createEffect(() => this.seo.page$.pipe(
  switchMap(page => page?.image || of("")),
  tap(image => this.seo.setMetas({
    "og:image": image?.url || null,
    "og:image:alt": image?.alt || null,
    "og:image:width": image?.width ? image.width.toString() : null,
    "og:image:height": image?.height ? image.height.toString() : null,
  }))
), {dispatch: false});

constructor (private seo: SeoService<MyAppPageAbstract>) {
}
}

Pay attention to the constructor of the effect. You must provide the type of your custom PageAbstract class when importing SeoService.