Skip to content

Commit a48b519

Browse files
author
Your Name
committed
Angular Universal In Depth
1 parent 373f853 commit a48b519

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

src/app/app.module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {AppShellNoRenderDirective} from "./directives/app-shell-norender.directi
4949
],
5050
imports: [
5151
BrowserModule.withServerTransition({ appId: 'serverApp' }),
52+
BrowserTransferStateModule,
5253
BrowserAnimationsModule,
5354
MatMenuModule,
5455
MatButtonModule,

src/app/app.server.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NgModule } from '@angular/core';
2-
import { ServerModule } from '@angular/platform-server';
2+
import {ServerModule, ServerTransferStateModule} from '@angular/platform-server';
33

44
import { AppModule } from './app.module';
55
import { AppComponent } from './app.component';
@@ -8,6 +8,7 @@ import { AppComponent } from './app.component';
88
imports: [
99
AppModule,
1010
ServerModule,
11+
ServerTransferStateModule
1112
],
1213
bootstrap: [AppComponent],
1314
})

src/app/course/course.component.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {Meta, Title} from '@angular/platform-browser';
1818
})
1919
export class CourseComponent implements OnInit {
2020

21-
2221
course:Course;
2322

2423
dataSource: MatTableDataSource<Lesson>;
@@ -30,11 +29,7 @@ export class CourseComponent implements OnInit {
3029
private route: ActivatedRoute,
3130
private coursesService: CoursesService,
3231
private title: Title,
33-
private meta: Meta) {
34-
35-
}
36-
37-
32+
private meta: Meta) {}
3833

3934
ngOnInit() {
4035

src/app/services/course.resolver.ts

+27-7
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,45 @@ import {Observable, of} from 'rxjs';
55
import {CoursesService} from './courses.service';
66
import {first, tap} from 'rxjs/operators';
77
import {isPlatformServer} from '@angular/common';
8+
import {makeStateKey, TransferState} from "@angular/platform-browser";
89

910

1011
@Injectable()
1112
export class CourseResolver implements Resolve<Course> {
1213

13-
constructor(private coursesService: CoursesService) {
14+
constructor(
15+
private coursesService: CoursesService,
16+
private transferState:TransferState,
17+
@Inject(PLATFORM_ID) private platformId) {
1418

1519
}
1620

17-
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Course> {
21+
resolve(route: ActivatedRouteSnapshot,
22+
state: RouterStateSnapshot): Observable<Course> {
1823

1924
const courseId = route.params['id'];
2025

21-
return this.coursesService.findCourseById(courseId)
22-
.pipe(
23-
first()
24-
);
26+
const COURSE_KEY = makeStateKey<Course>("courseKey-" + courseId);
2527

28+
if (this.transferState.hasKey(COURSE_KEY)) {
2629

30+
const course = this.transferState.get(COURSE_KEY, null);
31+
32+
this.transferState.remove(COURSE_KEY);
33+
34+
return of(course);
35+
}
36+
else {
37+
return this.coursesService.findCourseById(courseId)
38+
.pipe(
39+
first(),
40+
tap(course => {
41+
if (isPlatformServer(this.platformId)) {
42+
this.transferState.set(COURSE_KEY, course);
43+
}
44+
})
45+
);
46+
}
2747
}
2848

29-
}
49+
}

0 commit comments

Comments
 (0)