Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(seo)!: remove explicit dep on ngrx/router-store #2817

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions libs/seo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
"@angular/router": "0.0.0-PLACEHOLDER",
"@angular/platform-browser": "0.0.0-PLACEHOLDER",
"@daffodil/core": "0.0.0-PLACEHOLDER",
"rxjs": "0.0.0-PLACEHOLDER"
},
"optionalDependencies": {
"@ngrx/effects": "0.0.0-PLACEHOLDER",
"@ngrx/store": "0.0.0-PLACEHOLDER",
"@ngrx/router-store": "0.0.0-PLACEHOLDER",
"rxjs": "0.0.0-PLACEHOLDER"
"@ngrx/router-store": "0.0.0-PLACEHOLDER"
},
"devDependencies": {
"@daffodil/core": "0.0.0-PLACEHOLDER"
Expand Down
6 changes: 6 additions & 0 deletions libs/seo/router/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-entrypoint.schema.json",
"lib": {
"entryFile": "src/index.ts"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
daffProvideCanonicalUrlRouterUpdates,
DAFF_SEO_CANONICAL_URL_ROUTER_UPDATES,
} from './updates.token';
import { DaffSeoUpdateEventPair } from '../../../models/update-event-pair.interface';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';


describe('daffProvideCanonicalUrlUpdates', () => {
let updates: DaffSeoUpdateEventPair<Event, string>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
} from '@angular/core';
import { Event } from '@angular/router';

import { DaffSeoUpdateEventPair } from '../../../models/update-event-pair.interface';

import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';

/**
* A multi-provider injection token for providing canonical URL update logic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {

import { DaffRestoreableCanonicalService } from '@daffodil/seo';


import { DaffSeoPageHookRouterEffects } from './page-hook.effects';
import { DAFF_SEO_CANONICAL_URL_ROUTER_UPDATES } from '../../injection-tokens/canonical/router/updates.token';
import { DaffSeoUpdateEventPair } from '../../models/update-event-pair.interface';
import { DAFF_SEO_CANONICAL_URL_ROUTER_UPDATES } from '../canonical/updates.token';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';

/**
* Page hook effects for the canonical URL.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
DaffSeoMetaDefinition,
} from '@daffodil/seo';


import { DaffSeoPageHookRouterEffects } from './page-hook.effects';
import { DAFF_SEO_META_ROUTER_UPDATES } from '../../injection-tokens/meta/router/updates.token';
import { DaffSeoUpdateEventPair } from '../../models/update-event-pair.interface';
import { DAFF_SEO_META_ROUTER_UPDATES } from '../meta/updates.token';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';

/**
* Page hook effects for the page meta tags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Event,
ActivatedRoute,
} from '@angular/router';
import { createEffect } from '@ngrx/effects';
import { EMPTY } from 'rxjs';
import {
map,
Expand All @@ -18,7 +17,7 @@ import {

import { DaffSeoRestoreableServiceInterface } from '@daffodil/seo';

import { DaffSeoUpdateEventPair } from '../../models/update-event-pair.interface';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';

export const shouldHandleEvent = <T extends DaffSeoUpdateEventPair>(routingEvent: Event, operators: T[]): boolean =>
!!operators.find((trackableEvents) => routingEvent instanceof trackableEvents.event);
Expand All @@ -45,71 +44,51 @@ export abstract class DaffSeoPageHookRouterEffects<
*
* @docs-private
*/
getData$ = createEffect(
() => this.router.events.pipe(
filter((e) => shouldHandleEvent(e, this.updates)),
map(event => this.updates.filter(update =>
event instanceof update.event,
).map(({ getData }) =>
getData(event, this.activatedRoute),
)),
tap((data: V[]) => data.forEach(datum => {
if (datum) {
this.service.upsert(datum);
};
})),
switchMap(() => EMPTY),
),
{
dispatch: false,
},
getData$ = () => this.router.events.pipe(
filter((e) => shouldHandleEvent(e, this.updates)),
map(event => this.updates.filter(update =>
event instanceof update.event,
).map(({ getData }) =>
getData(event, this.activatedRoute),
)),
tap((data: V[]) => data.forEach(datum => {
if (datum) {
this.service.upsert(datum);
};
})),
switchMap(() => EMPTY),
);

/**
* Removes SEO data when navigation starts.
*
* @docs-private
*/
remove$ = createEffect(
() => this.router.events.pipe(
filter((e) => e instanceof NavigationStart),
tap(() => this.service.clear()),
switchMap(() => EMPTY),
),
{
dispatch: false,
},
remove$ = () => this.router.events.pipe(
filter((e) => e instanceof NavigationStart),
tap(() => this.service.clear()),
switchMap(() => EMPTY),
);

/**
* Restores previously removed SEO data when navigation is canceled or fails.
*
* @docs-private
*/
restore$ = createEffect(
() => this.router.events.pipe(
filter((e) => e instanceof NavigationCancel || e instanceof NavigationError),
tap(() => this.service.restore()),
switchMap(() => EMPTY),
),
{
dispatch: false,
},
restore$ = () => this.router.events.pipe(
filter((e) => e instanceof NavigationCancel || e instanceof NavigationError),
tap(() => this.service.restore()),
switchMap(() => EMPTY),
);

/**
* Empties the SEO data restore cache when navigation completes.
*
* @docs-private
*/
emptyRestoreCache$ = createEffect(
() => this.router.events.pipe(
filter((e) => e instanceof NavigationEnd),
tap(() => this.service.emptyRestoreCache()),
switchMap(() => EMPTY),
),
{
dispatch: false,
},
emptyRestoreCache$ = () => this.router.events.pipe(
filter((e) => e instanceof NavigationEnd),
tap(() => this.service.emptyRestoreCache()),
switchMap(() => EMPTY),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import {
import { DaffRestoreableTitleService } from '@daffodil/seo';

import { DaffSeoPageHookRouterEffects } from './page-hook.effects';
import { DAFF_SEO_TITLE_ROUTER_UPDATES } from '../../injection-tokens/title/router/updates.token';
import { DaffSeoUpdateEventPair } from '../../models/update-event-pair.interface';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';
import { DAFF_SEO_TITLE_ROUTER_UPDATES } from '../title/updates.token';

/**
* Page hook effects for the page title.
*
* @deprecated since Angular 14. See https://angular.io/api/router/TitleStrategy
*/
@Injectable()
export class DaffSeoNativeTitleEffects extends DaffSeoPageHookRouterEffects<
Expand Down
1 change: 1 addition & 0 deletions libs/seo/router/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public_api';
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
daffProvideMetaRouterUpdates,
DAFF_SEO_META_ROUTER_UPDATES,
} from './updates.token';
import { DaffSeoUpdateEventPair } from '../../../models/update-event-pair.interface';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';


describe('daffProvideMetaUpdates', () => {
let updates: DaffSeoUpdateEventPair<Event, DaffSeoMetaDefinition>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Event } from '@angular/router';

import { DaffSeoMetaDefinition } from '@daffodil/seo';

import { DaffSeoUpdateEventPair } from '../../../models/update-event-pair.interface';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';

/**
* A multi-provider injection token for providing page meta update logic.
Expand Down
34 changes: 34 additions & 0 deletions libs/seo/router/src/providers/canonical-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
APP_INITIALIZER,
Provider,
} from '@angular/core';

import { initializeRouterService } from './initializer';
import { DaffSeoNativeCanonicalUrlEffects } from '../effects/canonical.effects';

/**
* Configures an Angular application to track routing configuration
* and subsequently set canonical url information in the document.
*
* This provider is intended to only be imported exactly once in the root of the application.
*
* ## Usage
*
* ```ts
* @NgModule({
* providers: [
* daffSeoRouterCanonicalProvider()
* ],
* })
* export class AppModule { }
* ```
*/
export const daffSeoRouterCanonicalProvider = (): Provider[] => [
DaffSeoNativeCanonicalUrlEffects,
{
provide: APP_INITIALIZER,
useFactory: initializeRouterService,
deps: [DaffSeoNativeCanonicalUrlEffects],
multi: true,
},
];
8 changes: 8 additions & 0 deletions libs/seo/router/src/providers/initializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DaffSeoPageHookRouterEffects } from '../effects/page-hook.effects';

export const initializeRouterService = (effects: DaffSeoPageHookRouterEffects<any, any, any>) => () => {
effects.getData$().subscribe();
effects.emptyRestoreCache$().subscribe();
effects.remove$().subscribe();
effects.restore$().subscribe();
};
33 changes: 33 additions & 0 deletions libs/seo/router/src/providers/meta-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
APP_INITIALIZER,
Provider,
} from '@angular/core';

import { initializeRouterService } from './initializer';
import { DaffSeoNativeMetaEffects } from '../effects/meta.effects';

/**
* Configures an Angular application to track routing configuration and subsequently set meta information in the document.
*
* This provider is intended to only be imported exactly once in the root of the application.
*
* ## Usage
*
* ```ts
* @NgModule({
* providers: [
* daffSeoRouterMetaProvider()
* ],
* })
* export class AppModule { }
* ```
*/
export const daffSeoRouterMetaProvider = (): Provider[] => [
DaffSeoNativeMetaEffects,
{
provide: APP_INITIALIZER,
useFactory: initializeRouterService,
deps: [DaffSeoNativeMetaEffects],
multi: true,
},
];
35 changes: 35 additions & 0 deletions libs/seo/router/src/providers/title-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
APP_INITIALIZER,
Provider,
} from '@angular/core';

import { initializeRouterService } from './initializer';
import { DaffSeoNativeTitleEffects } from '../effects/title.effects';

/**
* Configures an Angular application to track routing configuration and subsequently set title information in the document.
*
* This provider is intended to only be imported exactly once in the root of the application.
*
* @deprecated since Angular 14. See https://angular.io/api/router/TitleStrategy
*
* ## Usage
*
* ```ts
* @NgModule({
* providers: [
* daffSeoRouterTitleProvider()
* ],
* })
* export class AppModule { }
* ```
*/
export const daffSeoRouterTitleProvider = (): Provider[] => [
DaffSeoNativeTitleEffects,
{
provide: APP_INITIALIZER,
useFactory: initializeRouterService,
deps: [DaffSeoNativeTitleEffects],
multi: true,
},
];
16 changes: 16 additions & 0 deletions libs/seo/router/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export { DaffSeoUpdateEventPair } from './model/update-event-pair.interface';
export {
DAFF_SEO_META_ROUTER_UPDATES,
daffProvideMetaRouterUpdates,
} from './meta/updates.token';
export {
DAFF_SEO_CANONICAL_URL_ROUTER_UPDATES,
daffProvideCanonicalUrlRouterUpdates,
} from './canonical/updates.token';
export {
DAFF_SEO_TITLE_ROUTER_UPDATES,
daffProvideTitleRouterUpdates,
} from './title/updates.token';
export { daffSeoRouterTitleProvider } from './providers/title-provider';
export { daffSeoRouterMetaProvider } from './providers/meta-provider';
export { daffSeoRouterCanonicalProvider } from './providers/canonical-provider';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
daffProvideTitleRouterUpdates,
DAFF_SEO_TITLE_ROUTER_UPDATES,
} from './updates.token';
import { DaffSeoUpdateEventPair } from '../../../models/update-event-pair.interface';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';


describe('daffProvideTitleUpdates', () => {
let updates: DaffSeoUpdateEventPair<Event, string>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@angular/core';
import { Event } from '@angular/router';

import { DaffSeoUpdateEventPair } from '../../../models/update-event-pair.interface';
import { DaffSeoUpdateEventPair } from '../model/update-event-pair.interface';

/**
* A multi-provider injection token for providing canonical URL update logic.
Expand Down
Empty file.
12 changes: 0 additions & 12 deletions libs/seo/state/src/injection-tokens/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,13 @@ export {
daffProvideTitleUpdates,
DAFF_SEO_TITLE_UPDATES,
} from './title/updates.token';
export {
daffProvideTitleRouterUpdates,
DAFF_SEO_TITLE_ROUTER_UPDATES,
} from './title/router/updates.token';

export {
daffProvideMetaUpdates,
DAFF_SEO_META_UPDATES,
} from './meta/updates.token';
export {
daffProvideMetaRouterUpdates,
DAFF_SEO_META_ROUTER_UPDATES,
} from './meta/router/updates.token';

export {
daffProvideCanonicalUrlUpdates,
DAFF_SEO_CANONICAL_URL_UPDATES,
} from './canonical/updates.token';
export {
daffProvideCanonicalUrlRouterUpdates,
DAFF_SEO_CANONICAL_URL_ROUTER_UPDATES,
} from './canonical/router/updates.token';
Loading
Loading