@davidlj95/ngx-meta
Version:
Set your Angular site's metadata: standard meta tags, Open Graph, Twitter Cards, JSON-LD structured data and more. Supports SSR (and Angular Universal). Use a service. Use routes' data. Set it up in a flash! 🚀
110 lines (102 loc) • 4.47 kB
JavaScript
import * as i0 from '@angular/core';
import { inject, makeEnvironmentProviders, ENVIRONMENT_INITIALIZER, NgModule } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { _routeMetadataStrategy, _makeInjectionToken, NgxMetaService, _formatDevMessage } from '@davidlj95/ngx-meta/core';
import { filter } from 'rxjs';
const DEFAULT_ROUTE_METADATA_STRATEGY_PROVIDER = {
provide: _routeMetadataStrategy(),
useFactory: (activatedRoute) => () => {
let currentRouteSnapshot = activatedRoute.snapshot;
while (currentRouteSnapshot.firstChild != null) {
currentRouteSnapshot = currentRouteSnapshot.firstChild;
}
return currentRouteSnapshot.data['meta'];
},
deps: [ActivatedRoute],
};
const MODULE_NAME = 'routing';
// WTF is this? Why not just import `EventType`? Well, compatibility reasons 🙃
// See https://github.com/davidlj95/ngx/pull/246 for the details
const NAVIGATION_END_EVENT_TYPE = new NavigationEnd(0, '', '').type;
const routerListener = () => _makeInjectionToken(ngDevMode ? 'Router listener' : 'RL', () => {
const router = inject(Router);
const ngxMetaService = inject(NgxMetaService);
let subscription;
return {
listen() {
/* istanbul ignore next https://github.com/istanbuljs/istanbuljs/issues/719 */
if (ngDevMode && subscription) {
console.warn(_formatDevMessage([
'prevented listening for route changes twice',
'Ensure routing provider or module is only imported once',
].join('\n'), { module: MODULE_NAME }));
}
/* istanbul ignore next https://github.com/istanbuljs/istanbuljs/issues/719 */
if (subscription) {
return;
}
subscription = router.events
.pipe(filter(({ type }) => type === NAVIGATION_END_EVENT_TYPE))
.subscribe({
next: () => ngxMetaService.set(),
});
},
// Can't use DestroyRef is introduced in Angular v16
// https://github.com/angular/angular/pull/49158
// It uses EnvironmentInjector's internal `onDestroy` under the hood
// Tried that too, but it's @internal + raises some errors. So back to the `ngOnDestroy`
ngOnDestroy: () => subscription?.unsubscribe(),
};
});
/**
* Enables managing metadata of a page by specifying it in its Angular
* {@link https://angular.dev/api/router/Route#data | Route#data}
*
* @remarks
*
* This is the standalone, recommended API. Using this API is preferred.
* However, you may also use {@link NgxMetaRoutingModule.(forRoot:1)} as the Angular module-based equivalent API.
*
* @public
*/
const provideNgxMetaRouting = () => makeEnvironmentProviders([
DEFAULT_ROUTE_METADATA_STRATEGY_PROVIDER,
{
provide: ENVIRONMENT_INITIALIZER,
multi: true,
useFactory: () => inject(routerListener()).listen,
},
]);
/**
* Enables managing metadata of a page by specifying it in its Angular
* {@link https://angular.dev/api/router/Route#data | Route#data}
*
* Use {@link NgxMetaRoutingModule.forRoot} method. Importing the module class alone does nothing.
*
* Check out {@link provideNgxMetaRouting} for the standalone, recommended API.
*
* @public
*/
class NgxMetaRoutingModule {
/**
* {@inheritDoc NgxMetaRoutingModule}
*/
static forRoot() {
/* istanbul ignore next - simple enough */
return {
ngModule: NgxMetaRoutingModule,
providers: [provideNgxMetaRouting()],
};
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: NgxMetaRoutingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.2.13", ngImport: i0, type: NgxMetaRoutingModule }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: NgxMetaRoutingModule }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: NgxMetaRoutingModule, decorators: [{
type: NgModule
}] });
/**
* Generated bundle index. Do not edit.
*/
export { NgxMetaRoutingModule, provideNgxMetaRouting };
//# sourceMappingURL=davidlj95-ngx-meta-routing.mjs.map