4.1.1 • Published 9 months ago

@cognizone/seo v4.1.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
9 months ago

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.

5.0.0-beta.17

9 months ago

5.0.0-beta.16

9 months ago

5.0.0-beta.9

10 months ago

5.0.0-beta.11

10 months ago

5.0.0-beta.12

10 months ago

5.0.0-beta.10

10 months ago

5.0.0-beta.15

10 months ago

5.0.0-beta.13

10 months ago

5.0.0-beta.14

10 months ago

5.0.0-beta.8

10 months ago

5.0.0-beta.6

11 months ago

5.0.0-beta.7

10 months ago

5.0.0-beta.5

11 months ago

5.0.0-beta.4

11 months ago

5.0.0-beta.2

11 months ago

5.0.0-beta.3

11 months ago

3.13.2

1 year ago

5.0.0-beta.1

11 months ago

3.13.1

1 year ago

3.15.1

11 months ago

3.13.3

12 months ago

4.0.0-rc.9

12 months ago

4.0.0-rc.8

12 months ago

4.0.0-rc.1

12 months ago

4.0.0-rc.3

12 months ago

4.0.0-rc.2

12 months ago

4.0.0-rc.5

12 months ago

4.0.0

11 months ago

4.0.0-rc.4

12 months ago

4.0.0-rc.7

12 months ago

4.0.0-rc.6

12 months ago

4.1.0-beta.1

11 months ago

3.14.1

12 months ago

3.14.0

12 months ago

3.14.2

11 months ago

3.13.0

1 year ago

4.1.0

11 months ago

4.1.1

11 months ago

3.12.3

1 year ago

3.12.2

1 year ago

3.12.4

1 year ago

3.12.1

1 year ago

3.12.0

1 year ago

3.11.0

1 year ago

3.10.0

1 year ago

3.9.4

1 year ago

3.9.3

1 year ago

3.9.2

1 year ago

3.9.1

1 year ago

3.9.0

1 year ago

3.8.4

1 year ago

3.8.3

1 year ago

3.8.2

2 years ago

3.8.0

2 years ago

3.8.1

2 years ago

3.7.1

2 years ago

3.7.0

2 years ago

4.0.0-beta.14

2 years ago

3.6.4

2 years ago

3.6.3

2 years ago

3.6.2

2 years ago

4.0.0-beta.13

2 years ago

3.6.1

2 years ago

4.0.0-beta.12

2 years ago

3.6.0

2 years ago