File tree 4 files changed +224
-73
lines changed
4 files changed +224
-73
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ import {AppShellRenderDirective} from './directives/app-shell-render.directive';
45
45
] ,
46
46
imports : [
47
47
BrowserModule . withServerTransition ( { appId : 'serverApp' } ) ,
48
+ BrowserTransferStateModule ,
48
49
BrowserAnimationsModule ,
49
50
MatMenuModule ,
50
51
MatButtonModule ,
Original file line number Diff line number Diff line change 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' ;
3
3
4
- import { AppModule } from './app.module' ;
5
- import { AppComponent } from './app.component' ;
4
+ import { AppModule } from './app.module' ;
5
+ import { AppComponent } from './app.component' ;
6
6
7
7
@NgModule ( {
8
- imports : [
9
- AppModule ,
10
- ServerModule ,
11
- ] ,
12
- bootstrap : [ AppComponent ] ,
8
+ imports : [
9
+ AppModule ,
10
+ ServerModule ,
11
+ ServerTransferStateModule
12
+ ] ,
13
+ bootstrap : [ AppComponent ] ,
13
14
} )
14
- export class AppServerModule { }
15
+ export class AppServerModule {
16
+
17
+ }
Original file line number Diff line number Diff line change @@ -6,23 +6,48 @@ import {CoursesService} from './courses.service';
6
6
import { first , tap } from 'rxjs/operators' ;
7
7
import { of } from 'rxjs/observable/of' ;
8
8
import { isPlatformServer } from '@angular/common' ;
9
+ import { makeStateKey , TransferState } from '@angular/platform-browser' ;
9
10
10
11
11
12
@Injectable ( )
12
13
export class CourseResolver implements Resolve < Course > {
13
14
14
- constructor ( private coursesService : CoursesService ) {
15
+ constructor (
16
+ private coursesService : CoursesService ,
17
+ @Inject ( PLATFORM_ID ) private platformId ,
18
+ private transferState :TransferState
19
+ ) {
15
20
16
21
}
17
22
18
23
resolve ( route : ActivatedRouteSnapshot , state : RouterStateSnapshot ) : Observable < Course > {
19
24
20
25
const courseId = route . params [ 'id' ] ;
21
26
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
+ }
26
51
27
52
28
53
}
You can’t perform that action at this time.
0 commit comments