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

@cognizone/seo

v3.13.0

Published

## Setup in an app

Downloads

91

Readme

seo

Setup in an app

Setting options

Quite a few options are present by default, but they can be completely overridden if necessary. A complete override can be done by either providing SEO_OPTIONS like so

@NgModule({
  providers: [
    provide: SEO_OPTIONS,
    useValue: {
      // here comes the options
    }
  ]
})
export class AppModule {}

Or making use of SeoService::setOptions.

A more subtle way to go about would be to modify the existing options, for example by making use of immer's produce function:

@Injectable()
export class MySeoInitService {
  private seoService = inject(SeoService);

  init(): void {
    const modifiedOptions = produce(this.seoService.getOptions(), draft => {
      // we need to specify the processor for each even if they are linked, as they would be updated with the raw value otherwise
      draft.metaDescriptors[MetaIds.TITLE_TAG].preProcessors = Processors.html();
      draft.metaDescriptors[MetaIds.OG_TITLE].preProcessors = Processors.html();
      draft.metaDescriptors[MetaIds.TWITTER_TITLE].preProcessors = Processors.html();
      // we don't set a suffix for other title-like properties so only <title> gets it
      draft.metaDescriptors[MetaIds.TITLE_TAG].postProcessors = Processors.withMaxLength(RECOMMENDED_TITLE_LENGTH, ' - MyAppName');

      draft.metaDescriptors[MetaIds.CANONICAL_URL_LINK].postProcessors = Processors.url({ forceW3: true });
      draft.metaDescriptors[MetaIds.OG_URL].postProcessors = Processors.url({ forceW3: true });
    });
    this.seoService.setOptions(modifiedOptions);
  }
}

You can also make use of our opinionated options, but it is then advised to use them as-is instead od using them as a base to be modified. This would then look like this:

@NgModule({
  providers: [
    provide: SEO_OPTIONS,
    useValue: createOpinionatedOptions({titleSuffix: ' | MyApp'})
  ]
})
export class AppModule {}

Setting default values

There are most likely a few meta properties that are not meant to change at all and that should be present on all pages. For this, we can augment a bit our init logic seen in the previous section:

@Injectable()
export class MySeoInitService {
  private seoService = inject(SeoService);

  init(): void {
    // ...
    this.seoOptions.setMetaValue(MetaIds.OG_TYPE, 'website', { setAsDefault: true });
    this.seoOptions.setMetaValue(MetaIds.OG_SITE_NAME, 'My App', { setAsDefault: true });
    // no need to set twitter:image as, by default, og:image and twitter:image are linked together
    this.seoOptions.setMetaValue(MetaIds.OG_IMAGE, `https://path/to/image.png`, { setAsDefault: true });
    this.seoOptions.setMetaValue(MetaIds.TWITTER_CARD, 'summary', { setAsDefault: true });
    // etc.
  }
}

Running unit tests

Run nx test seo to execute the unit tests.