Skip to content

Commit dde2366

Browse files
committed
Prepare to use @ngx-utils/cookies as backend for SSR cookies.
This will be used to store authentication data. The reason for this is to make this data available to the server during the SSR so it can be used to render values that come from API queries. This uses temporarily a fork of @ngx-utils/cookies, while the support needed for Angular 10 is not merged. The associated PR for this support is ngx-utils/cookies#24.
1 parent 0d210dc commit dde2366

File tree

8 files changed

+99
-30
lines changed

8 files changed

+99
-30
lines changed

frontend/package-lock.json

Lines changed: 25 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
"@angular/router": "^10.0.10",
3838
"@ng-toolkit/universal": "^8.1.0",
3939
"@nguniversal/express-engine": "^10.0.2",
40+
"@ngx-utils/cookies": "https://github.com/kenkeiras/ngx-cookies/releases/download/angular-10-support/ngx-cookies-angular-10.tgz",
41+
"@types/cookie-parser": "^1.4.2",
4042
"bootstrap": "^4.5.0",
43+
"cookie-parser": "^1.4.5",
4144
"core-js": "^2.6.11",
4245
"express": "^4.15.2",
4346
"fuse.js": "^5.2.3",

frontend/server.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import { join } from 'path';
77
import { AppServerModule } from './src/main.server';
88
import { APP_BASE_HREF } from '@angular/common';
99
import { existsSync } from 'fs';
10+
import { REQUEST } from '@nguniversal/express-engine/tokens';
11+
import cookieParser from 'cookie-parser';
1012

1113
// The Express app is exported so that it can be used by serverless Functions.
1214
export function app(): express.Express {
1315
const server = express();
1416
const distFolder = join(process.cwd(), 'dist/programaker/browser');
1517
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
1618

19+
server.use(cookieParser());
20+
1721
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
1822
server.engine('html', ngExpressEngine({
1923
bootstrap: AppServerModule,
@@ -31,7 +35,14 @@ export function app(): express.Express {
3135

3236
// All regular routes use the Universal engine
3337
server.get('*', (req, res) => {
34-
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
38+
res.render(indexHtml, {
39+
req,
40+
res,
41+
providers: [
42+
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
43+
{ provide: 'RESPONSE', useValue: res },
44+
{ provide: 'REQUEST', useValue: req },
45+
] });
3546
});
3647

3748
return server;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { NgModule } from '@angular/core';
2+
import { BrowserModule } from '@angular/platform-browser';
3+
import { ANIMATION_MODULE_TYPE, BrowserAnimationsModule } from '@angular/platform-browser/animations';
4+
import { CookiesService } from '@ngx-utils/cookies';
5+
import { BrowserCookiesModule, BrowserCookiesService } from '@ngx-utils/cookies/browser';
6+
import { AppComponent } from './app.component';
7+
import { AppModule } from './app.module';
8+
9+
@NgModule({
10+
imports: [
11+
BrowserModule.withServerTransition({ appId: 'serverApp' }),
12+
BrowserCookiesModule.forRoot(),
13+
BrowserAnimationsModule,
14+
AppModule,
15+
],
16+
bootstrap: [AppComponent],
17+
providers: [
18+
{
19+
provide: CookiesService,
20+
useClass: BrowserCookiesService,
21+
},
22+
{
23+
provide: ANIMATION_MODULE_TYPE,
24+
useValue: 'BrowserAnimations',
25+
},
26+
],
27+
})
28+
export class AppBrowserModule {}

frontend/src/app/app.module.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
2323
import { MatBadgeModule } from '@angular/material/badge';
2424
import { MatTooltipModule } from '@angular/material/tooltip';
2525

26-
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
27-
2826
import { AlertModule } from 'ngx-bootstrap/alert';
2927

3028
import { AppComponent } from './app.component';
@@ -143,18 +141,17 @@ import { EditCollaboratorsDialogComponent } from './dialogs/editor-collaborators
143141
MatTooltipModule,
144142
MatSelectModule,
145143

146-
BrowserAnimationsModule,
147-
148-
BrowserModule.withServerTransition({ appId: 'serverApp' }),
149144
FormsModule,
150145
ReactiveFormsModule,
151146
HttpClientModule,
152147
AlertModule.forRoot(),
153148
AppRoutingModule,
149+
150+
BrowserModule.withServerTransition({ appId: 'serverApp' }),
154151
],
155-
exports: [],
152+
exports: [AppComponent],
156153
providers: [],
157-
bootstrap: [AppComponent],
154+
bootstrap: [],
158155
})
159156

160157
export class AppModule {
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import { NgModule } from '@angular/core';
2+
import { BrowserModule } from '@angular/platform-browser';
3+
import { ANIMATION_MODULE_TYPE, NoopAnimationsModule } from '@angular/platform-browser/animations';
24
import { ServerModule } from '@angular/platform-server';
3-
4-
import { AppModule } from './app.module';
5+
import { CookiesService } from '@ngx-utils/cookies';
6+
import { ServerCookiesModule, ServerCookiesService } from '@ngx-utils/cookies/server';
57
import { AppComponent } from './app.component';
8+
import { AppModule } from './app.module';
69

710
@NgModule({
8-
imports: [
9-
AppModule,
10-
ServerModule,
11-
],
12-
bootstrap: [AppComponent],
11+
imports: [
12+
NoopAnimationsModule,
13+
BrowserModule.withServerTransition({appId: 'serverApp'}),
14+
ServerModule,
15+
ServerCookiesModule.forRoot(),
16+
AppModule,
17+
],
18+
bootstrap: [AppComponent],
19+
providers: [
20+
{
21+
provide: CookiesService,
22+
useClass: ServerCookiesService,
23+
},
24+
{
25+
provide: ANIMATION_MODULE_TYPE,
26+
useValue: 'NoopAnimations',
27+
},
28+
],
1329
})
1430
export class AppServerModule {}

frontend/src/app/session.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as progbar from './ui/progbar';
77
import * as API from './api-config';
88
import { ContentType } from './content-type';
99
import { BrowserService } from './browser.service';
10+
import { CookiesService } from '@ngx-utils/cookies';
1011

1112
export type SessionInfoUpdate = { session: Session };
1213

@@ -34,6 +35,7 @@ export class SessionService {
3435
constructor(
3536
private http: HttpClient,
3637
private browser: BrowserService,
38+
private cookies: CookiesService,
3739
) {
3840
this.http = http;
3941
}

frontend/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { enableProdMode } from '@angular/core';
22
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
33

4-
import { AppModule } from './app/app.module';
4+
import { AppBrowserModule } from './app/app.browser.module';
55
import { environment } from './environments/environment';
66

77
if (environment.production) {
88
enableProdMode();
99
}
1010

1111
document.addEventListener('DOMContentLoaded', () => {
12-
platformBrowserDynamic().bootstrapModule(AppModule);
12+
platformBrowserDynamic().bootstrapModule(AppBrowserModule);
1313
});

0 commit comments

Comments
 (0)