Skip to content

Commit afab9fc

Browse files
committed
fix(ssr): fixing the ssr build
1 parent fb1e2eb commit afab9fc

File tree

6 files changed

+85
-54
lines changed

6 files changed

+85
-54
lines changed

angular.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@
399399
]
400400
},
401401
"server": "projects/bundle-test/src/main.server.ts",
402-
"prerender": true,
403402
"ssr": {
404403
"entry": "projects/bundle-test/server.ts"
405404
}

projects/bundle-test/server.ts

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,66 @@
1-
import { APP_BASE_HREF } from '@angular/common';
2-
import { CommonEngine } from '@angular/ssr/node';
3-
import express from 'express';
4-
import { fileURLToPath } from 'node:url';
5-
import { dirname, join, resolve } from 'node:path';
6-
import bootstrap from './src/main.server';
1+
import {
2+
AngularNodeAppEngine,
3+
createNodeRequestHandler,
4+
isMainModule,
5+
writeResponseToNodeResponse,
6+
} from '@angular/ssr/node';
7+
import express from 'express';
8+
import { dirname, resolve } from 'node:path';
9+
import { fileURLToPath } from 'node:url';
710

8-
// The Express app is exported so that it can be used by serverless Functions.
9-
export function app(): express.Express {
10-
const server = express();
1111
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
1212
const browserDistFolder = resolve(serverDistFolder, '../browser');
13-
const indexHtml = join(serverDistFolder, 'index.server.html');
1413

15-
const commonEngine = new CommonEngine();
14+
const app = express();
15+
const angularApp = new AngularNodeAppEngine();
1616

17-
server.set('view engine', 'html');
18-
server.set('views', browserDistFolder);
17+
/**
18+
* Example Express Rest API endpoints can be defined here.
19+
* Uncomment and define endpoints as necessary.
20+
*
21+
* Example:
22+
* ```ts
23+
* app.get('/api/**', (req, res) => {
24+
* // Handle API request
25+
* });
26+
* ```
27+
*/
1928

20-
// Example Express Rest API endpoints
21-
// server.get('/api/**', (req, res) => { });
22-
// Serve static files from /browser
23-
server.get('*.*', express.static(browserDistFolder, {
24-
maxAge: '1y'
25-
}));
29+
/**
30+
* Serve static files from /browser
31+
*/
32+
app.use(
33+
express.static(browserDistFolder, {
34+
maxAge: '1y',
35+
index: false,
36+
redirect: false,
37+
}),
38+
);
2639

27-
// All regular routes use the Angular engine
28-
server.get('*', (req, res, next) => {
29-
const { protocol, originalUrl, baseUrl, headers } = req;
30-
31-
commonEngine
32-
.render({
33-
bootstrap,
34-
documentFilePath: indexHtml,
35-
url: `${protocol}://${headers.host}${originalUrl}`,
36-
publicPath: browserDistFolder,
37-
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
38-
})
39-
.then((html) => res.send(html))
40-
.catch((err) => next(err));
40+
/**
41+
* Handle all other requests by rendering the Angular application.
42+
*/
43+
app.use('/**', (req, res, next) => {
44+
angularApp
45+
.handle(req)
46+
.then((response) =>
47+
response ? writeResponseToNodeResponse(response, res) : next(),
48+
)
49+
.catch(next);
4150
});
4251

43-
return server;
44-
}
45-
46-
function run(): void {
47-
const port = process.env['PORT'] || 4000;
48-
49-
// Start up the Node server
50-
const server = app();
51-
server.listen(port, () => {
52-
console.log(`Node Express server listening on http://localhost:${port}`);
53-
});
54-
}
52+
/**
53+
* Start the server if this module is the main entry point.
54+
* The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
55+
*/
56+
if (isMainModule(import.meta.url)) {
57+
const port = process.env['PORT'] || 4000;
58+
app.listen(port, () => {
59+
console.log(`Node Express server listening on http://localhost:${port}`);
60+
});
61+
}
5562

56-
run();
63+
/**
64+
* The request handler used by the Angular CLI (dev-server and during build).
65+
*/
66+
export const reqHandler = createNodeRequestHandler(app);

projects/bundle-test/src/app/app.config.server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { mergeApplicationConfig, ApplicationConfig } from '@angular/core';
22
import { provideServerRendering } from '@angular/platform-server';
3+
import { provideServerRouting } from '@angular/ssr';
34
import { appConfig } from './app.config';
5+
import { serverRoutes } from './app.routes.server';
46

57
const serverConfig: ApplicationConfig = {
68
providers: [
7-
provideServerRendering()
9+
provideServerRendering(),
10+
provideServerRouting(serverRoutes)
811
]
912
};
1013

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { RenderMode, ServerRoute } from '@angular/ssr';
2+
3+
export const serverRoutes: ServerRoute[] = [
4+
{
5+
path: '**',
6+
renderMode: RenderMode.Prerender
7+
}
8+
];

projects/bundle-test/src/app/form/form.component.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import { Component } from '@angular/core';
22
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
3-
import { IGX_INPUT_GROUP_DIRECTIVES, IGX_RADIO_GROUP_DIRECTIVES, IgxButtonDirective, IgxCheckboxComponent, IgxSwitchComponent } from 'igniteui-angular';
3+
import {
4+
IGX_INPUT_GROUP_DIRECTIVES,
5+
IGX_RADIO_GROUP_DIRECTIVES,
6+
IgxButtonDirective,
7+
IgxCheckboxComponent,
8+
IgxSwitchComponent
9+
} from 'igniteui-angular';
410

511
@Component({
612
selector: 'app-form',
7-
imports: [IgxCheckboxComponent, IgxSwitchComponent, IGX_RADIO_GROUP_DIRECTIVES, IgxButtonDirective, IGX_INPUT_GROUP_DIRECTIVES, ReactiveFormsModule],
13+
imports: [
14+
IgxCheckboxComponent,
15+
IgxSwitchComponent,
16+
IGX_RADIO_GROUP_DIRECTIVES,
17+
IgxButtonDirective,
18+
IGX_INPUT_GROUP_DIRECTIVES,
19+
ReactiveFormsModule
20+
],
821
templateUrl: './form.component.html',
922
styleUrls: ['./form.component.scss']
1023
})

projects/igniteui-angular/src/lib/checkbox/checkbox-base.directive.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ViewChild,
99
ElementRef,
1010
ChangeDetectorRef,
11-
Renderer2,
1211
Optional,
1312
Self,
1413
booleanAttribute,
@@ -250,7 +249,6 @@ export class CheckboxBaseDirective implements AfterViewInit {
250249

251250
constructor(
252251
protected cdr: ChangeDetectorRef,
253-
protected renderer: Renderer2,
254252
@Inject(THEME_TOKEN)
255253
protected themeToken: ThemeToken,
256254
@Optional() @Self() public ngControl: NgControl
@@ -261,14 +259,14 @@ export class CheckboxBaseDirective implements AfterViewInit {
261259

262260
this.theme = this.themeToken.theme;
263261

264-
const themeChange = this.themeToken.onChange((theme) => {
262+
const { unsubscribe } = this.themeToken.onChange((theme) => {
265263
if (this.theme !== theme) {
266264
this.theme = theme;
267265
this.cdr.detectChanges();
268266
}
269267
});
270268

271-
this.destroyRef.onDestroy(() => themeChange.unsubscribe());
269+
this.destroyRef.onDestroy(() => unsubscribe());
272270
}
273271

274272
/**

0 commit comments

Comments
 (0)