Skip to content

Commit 58b720c

Browse files
author
Your Name
committed
Angular Universal Course
1 parent 9096cb8 commit 58b720c

File tree

4 files changed

+224
-73
lines changed

4 files changed

+224
-73
lines changed

src/app/app.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {AppShellRenderDirective} from './directives/app-shell-render.directive';
4545
],
4646
imports: [
4747
BrowserModule.withServerTransition({ appId: 'serverApp' }),
48+
BrowserTransferStateModule,
4849
BrowserAnimationsModule,
4950
MatMenuModule,
5051
MatButtonModule,

src/app/app.server.module.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { NgModule } from '@angular/core';
2-
import { ServerModule } from '@angular/platform-server';
1+
import {NgModule} from '@angular/core';
2+
import {ServerModule, ServerTransferStateModule} from '@angular/platform-server';
33

4-
import { AppModule } from './app.module';
5-
import { AppComponent } from './app.component';
4+
import {AppModule} from './app.module';
5+
import {AppComponent} from './app.component';
66

77
@NgModule({
8-
imports: [
9-
AppModule,
10-
ServerModule,
11-
],
12-
bootstrap: [AppComponent],
8+
imports: [
9+
AppModule,
10+
ServerModule,
11+
ServerTransferStateModule
12+
],
13+
bootstrap: [AppComponent],
1314
})
14-
export class AppServerModule {}
15+
export class AppServerModule {
16+
17+
}

src/app/services/course.resolver.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,48 @@ import {CoursesService} from './courses.service';
66
import {first, tap} from 'rxjs/operators';
77
import {of} from 'rxjs/observable/of';
88
import {isPlatformServer} from '@angular/common';
9+
import {makeStateKey, TransferState} from '@angular/platform-browser';
910

1011

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

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

1621
}
1722

1823
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Course> {
1924

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

22-
return this.coursesService.findCourseById(courseId)
23-
.pipe(
24-
first()
25-
);
27+
const COURSE_KEY = makeStateKey<Course>('course-' + courseId);
28+
29+
if (this.transferState.hasKey(COURSE_KEY)) {
30+
31+
const course = this.transferState.get<Course>(COURSE_KEY, null);
32+
33+
this.transferState.remove(COURSE_KEY);
34+
35+
return of(course);
36+
}
37+
else {
38+
return this.coursesService.findCourseById(courseId)
39+
.pipe(
40+
first(),
41+
tap(course => {
42+
43+
if (isPlatformServer(this.platformId)) {
44+
this.transferState.set(COURSE_KEY, course);
45+
}
46+
47+
})
48+
);
49+
50+
}
2651

2752

2853
}

0 commit comments

Comments
 (0)