Skip to content
This repository was archived by the owner on Oct 1, 2018. It is now read-only.

Commit 0cf78b3

Browse files
committed
fix(seo): Set SEO data in route config
1 parent ac8a90e commit 0cf78b3

12 files changed

+86
-54
lines changed

src/app/app.component.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
44
import { AppComponent } from './app.component';
55
import { ToolbarModule } from './toolbar/toolbar.module';
66
import { MatSidenavModule, MatListModule } from '@angular/material';
7+
import { SeoService } from './services/seo.service';
78

89
describe('AppComponent', () => {
910
let component: AppComponent;
@@ -19,7 +20,8 @@ describe('AppComponent', () => {
1920
MatSidenavModule,
2021
MatListModule
2122
],
22-
declarations: [AppComponent]
23+
declarations: [AppComponent],
24+
providers: [SeoService]
2325
}).compileComponents();
2426
})
2527
);

src/app/app.component.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Component } from "@angular/core";
1+
import { Component, OnInit } from '@angular/core';
2+
import {
3+
Router,
4+
ActivatedRoute,
5+
NavigationEnd,
6+
RouterEvent
7+
} from '@angular/router';
8+
import { filter, map, mergeMap } from 'rxjs/operators';
9+
import { SeoService } from './services/seo.service';
210

311
interface Menu {
412
title: string;
@@ -7,31 +15,58 @@ interface Menu {
715
}
816

917
@Component({
10-
selector: "app-root",
11-
templateUrl: "./app.component.html",
12-
styleUrls: ["./app.component.scss"]
18+
selector: 'app-root',
19+
templateUrl: './app.component.html',
20+
styleUrls: ['./app.component.scss']
1321
})
14-
export class AppComponent {
22+
export class AppComponent implements OnInit {
1523
menus: Menu[] = [
1624
{
17-
title: "Home",
18-
link: "/",
25+
title: 'Home',
26+
link: '/',
1927
options: { exact: true }
2028
},
2129
{
22-
title: "Operators",
23-
link: "/operators",
30+
title: 'Operators',
31+
link: '/operators',
2432
options: { exact: false }
2533
},
2634
{
27-
title: "Companies",
28-
link: "/companies",
35+
title: 'Companies',
36+
link: '/companies',
2937
options: { exact: false }
3038
},
3139
{
32-
title: "Team",
33-
link: "/team",
40+
title: 'Team',
41+
link: '/team',
3442
options: { exact: false }
3543
}
3644
];
45+
46+
constructor(
47+
private _router: Router,
48+
private _activatedRoute: ActivatedRoute,
49+
private _seo: SeoService
50+
) {}
51+
52+
ngOnInit() {
53+
this._router.events
54+
.pipe(
55+
filter((e: RouterEvent) => e instanceof NavigationEnd),
56+
map(() => {
57+
let route = this._activatedRoute;
58+
while (route.firstChild) {
59+
route = route.firstChild;
60+
}
61+
return route;
62+
}),
63+
filter(route => route.outlet === 'primary'),
64+
mergeMap(route => route.data)
65+
)
66+
.subscribe(data => {
67+
if (data !== {}) {
68+
this._seo.setHeaders(data.title || [], data.description || '');
69+
}
70+
});
71+
}
3772
}

src/app/companies/companies-routing.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { Routes, RouterModule } from '@angular/router';
33

44
import { CompaniesComponent } from './companies.component';
55

6-
const routes: Routes = [{ path: '', component: CompaniesComponent }];
6+
const routes: Routes = [
7+
{
8+
path: '',
9+
component: CompaniesComponent,
10+
data: { title: ['Companies'], description: 'Companies that use RxJS...' }
11+
}
12+
];
713

814
@NgModule({
915
imports: [RouterModule.forChild(routes)],
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { Component, OnInit } from '@angular/core';
2-
import { SeoService } from '../services/seo.service';
1+
import { Component } from '@angular/core';
32

43
@Component({
54
selector: 'app-companies',
65
templateUrl: './companies.component.html',
76
styleUrls: ['./companies.component.scss']
87
})
9-
export class CompaniesComponent implements OnInit {
10-
constructor(private _seo: SeoService) {}
11-
12-
ngOnInit() {
13-
this._seo.setHeaders(['Companies'], this._seo.companiesDescription);
14-
}
8+
export class CompaniesComponent {
9+
constructor() {}
1510
}

src/app/operators/operators-routing.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const routes: Routes = [
88
{
99
path: '',
1010
component: OperatorsComponent,
11+
data: { title: ['Operators'], description: 'All the RxJS operators...' },
1112
children: [{ path: ':operator', component: OperatorComponent }]
1213
}
1314
];

src/app/operators/operators.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { BreakpointObserver } from '@angular/cdk/layout';
1717
import { Subscription } from 'rxjs/Subscription';
1818
import { Observable } from 'rxjs/Observable';
1919
import { OperatorDoc } from '../../operator-docs/operator.model';
20-
import { SeoService } from '../services/seo.service';
2120

2221
const OPERATOR_MENU_GAP_LARGE = 64;
2322
const OPERATOR_MENU_GAP_SMALL = 54;
@@ -60,14 +59,12 @@ export class OperatorsComponent implements OnInit {
6059

6160
constructor(
6261
private _breakpointObserver: BreakpointObserver,
63-
@Inject(OPERATORS_TOKEN) public operators: OperatorDoc[],
64-
private _seo: SeoService
62+
@Inject(OPERATORS_TOKEN) public operators: OperatorDoc[]
6563
) {}
6664

6765
ngOnInit() {
6866
this.groupedOperators = groupOperatorsByType(this.operators);
6967
this.categories = Object.keys(this.groupedOperators);
70-
this._seo.setHeaders(['Operators'], this._seo.operatorsDescription);
7168
}
7269

7370
get extraSmallScreen() {

src/app/operators/specs/operators.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
groupOperatorsByType
1111
} from '../operators.component';
1212
import { OperatorDoc } from '../../../operator-docs';
13-
import { SeoService } from '../../services/seo.service';
1413

1514
const mockActivatedRoute = {
1615
snapshot: {},
@@ -42,7 +41,6 @@ describe('Operators', () => {
4241
imports: [RouterTestingModule, LayoutModule],
4342
declarations: [OperatorsComponent],
4443
providers: [
45-
SeoService,
4644
{ provide: OPERATORS_TOKEN, useValue: mockOperators },
4745
{ provide: ActivatedRoute, useValue: mockActivatedRoute }
4846
],

src/app/rxjs/rxjs-routing.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
3-
43
import { RxjsComponent } from './rxjs.component';
54

6-
const routes: Routes = [{ path: '', component: RxjsComponent }];
5+
const routes: Routes = [
6+
{
7+
path: '',
8+
component: RxjsComponent,
9+
data: { title: [], description: 'The complete RxJS documentation...' }
10+
}
11+
];
712

813
@NgModule({
914
imports: [RouterModule.forChild(routes)],

src/app/rxjs/rxjs.component.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import { Component, OnInit } from '@angular/core';
2-
import { SeoService } from '../services/seo.service';
1+
import { Component } from '@angular/core';
32

43
@Component({
54
selector: 'app-rxjs',
65
templateUrl: './rxjs.component.html',
76
styleUrls: ['./rxjs.component.scss']
87
})
9-
export class RxjsComponent implements OnInit {
10-
constructor(private _seo: SeoService) {}
11-
12-
ngOnInit() {
13-
this._seo.setHeaders([], this._seo.homeDescription);
14-
}
8+
export class RxjsComponent {
9+
constructor() {}
1510
}

src/app/services/seo.service.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ export class SeoService {
66
// This part is happended at the end of head>title
77
private siteTitle = 'RxJS Documentation';
88

9-
// head>meta>description for common pages
10-
public homeDescription = 'The complete RxJS documentation...';
11-
public operatorsDescription = 'All the RxJS operators...';
12-
public companiesDescription = 'Companies that use RxJS...';
13-
public teamDescription = 'People behind the RxJS Documentation project...';
14-
159
constructor(private _title: Title, private _meta: Meta) {}
1610

1711
public setHeaders(titleParts: string[], description: string) {

0 commit comments

Comments
 (0)