Skip to content

Commit bc87238

Browse files
authored
Merge pull request #2007 from valor-software/alex-migrate-to-ssr
feat(ssr): migrated demo to ssr
2 parents 099ebc9 + f98add1 commit bc87238

File tree

13 files changed

+4872
-29307
lines changed

13 files changed

+4872
-29307
lines changed
-2.85 KB
Loading

apps/ng2-charts-demo/playwright.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const baseURL = process.env['BASE_URL'] || 'http://localhost:4200';
1717
*/
1818
export default defineConfig({
1919
...nxE2EPreset(__filename, { testDir: './e2e' }),
20+
expect: {
21+
toHaveScreenshot: { maxDiffPixels: 100 },
22+
},
2023
projects: [
2124
{
2225
name: 'chromium',

apps/ng2-charts-demo/project.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"loader": {
2525
".html": "text",
2626
".ts": "text"
27+
},
28+
"server": "apps/ng2-charts-demo/src/main.server.ts",
29+
"prerender": true,
30+
"ssr": {
31+
"entry": "apps/ng2-charts-demo/server.ts"
2732
}
2833
},
2934
"configurations": {
@@ -102,6 +107,72 @@
102107
"updateSnapshots": true
103108
}
104109
}
110+
},
111+
112+
"server": {
113+
"executor": "@angular-devkit/build-angular:server",
114+
"options": {
115+
"outputPath": "dist/ng2-charts-demo/server",
116+
"main": "apps/ng2-charts-demo/server.ts",
117+
"tsConfig": "apps/ng2-charts-demo/tsconfig.server.json"
118+
},
119+
"configurations": {
120+
"production": {
121+
"buildOptimizer": true,
122+
"outputHashing": "media",
123+
"fileReplacements": [
124+
{
125+
"replace": "apps/ng2-charts-demo/src/environments/environment.ts",
126+
"with": "apps/ng2-charts-demo/src/environments/environment.prod.ts"
127+
}
128+
],
129+
"optimization": true,
130+
"sourceMap": false,
131+
"extractLicenses": true,
132+
"vendorChunk": false
133+
},
134+
"development": {
135+
"buildOptimizer": false,
136+
"optimization": false,
137+
"sourceMap": true,
138+
"extractLicenses": false,
139+
"vendorChunk": true
140+
}
141+
},
142+
"defaultConfiguration": "production"
143+
},
144+
145+
"serve-ssr": {
146+
"executor": "@angular-devkit/build-angular:ssr-dev-server",
147+
"configurations": {
148+
"development": {
149+
"browserTarget": "ng2-charts-demo:build:development",
150+
"serverTarget": "ng2-charts-demo:server:development"
151+
},
152+
"production": {
153+
"browserTarget": "ng2-charts-demo:build:production",
154+
"serverTarget": "ng2-charts-demo:server:production"
155+
}
156+
},
157+
"defaultConfiguration": "development"
158+
},
159+
160+
"prerender": {
161+
"executor": "@angular-devkit/build-angular:prerender",
162+
"options": {
163+
"routes": ["/"]
164+
},
165+
"configurations": {
166+
"production": {
167+
"browserTarget": "ng2-charts-demo:build:production",
168+
"serverTarget": "ng2-charts-demo:server:production"
169+
},
170+
"development": {
171+
"browserTarget": "ng2-charts-demo:build:development",
172+
"serverTarget": "ng2-charts-demo:server:development"
173+
}
174+
},
175+
"defaultConfiguration": "production"
105176
}
106177
},
107178
"implicitDependencies": ["ng2-charts"]

apps/ng2-charts-demo/server.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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';
7+
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();
11+
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
12+
const browserDistFolder = resolve(serverDistFolder, '../browser');
13+
const indexHtml = join(serverDistFolder, 'index.server.html');
14+
15+
const commonEngine = new CommonEngine();
16+
17+
server.set('view engine', 'html');
18+
server.set('views', browserDistFolder);
19+
20+
// Example Express Rest API endpoints
21+
// server.get('/api/**', (req, res) => { });
22+
// Serve static files from /browser
23+
server.get(
24+
'**',
25+
express.static(browserDistFolder, {
26+
maxAge: '1y',
27+
index: 'index.html',
28+
}),
29+
);
30+
31+
// All regular routes use the Angular engine
32+
server.get('**', (req, res, next) => {
33+
const { protocol, originalUrl, baseUrl, headers } = req;
34+
35+
commonEngine
36+
.render({
37+
bootstrap,
38+
documentFilePath: indexHtml,
39+
url: `${protocol}://${headers.host}${originalUrl}`,
40+
publicPath: browserDistFolder,
41+
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
42+
})
43+
.then((html: any) => res.send(html))
44+
.catch((err: any) => next(err));
45+
});
46+
47+
return server;
48+
}
49+
50+
function run(): void {
51+
const port = process.env['PORT'] || 4000;
52+
53+
// Start up the Node server
54+
const server = app();
55+
server.listen(port, () => {
56+
console.log(`Node Express server listening on http://localhost:${port}`);
57+
});
58+
}
59+
60+
run();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { mergeApplicationConfig, ApplicationConfig } from '@angular/core';
2+
import { provideServerRendering } from '@angular/platform-server';
3+
import { appConfig } from './app.config';
4+
5+
const serverConfig: ApplicationConfig = {
6+
providers: [provideServerRendering()],
7+
};
8+
9+
export const config = mergeApplicationConfig(appConfig, serverConfig);

apps/ng2-charts-demo/src/app/app.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { ScatterChartComponent } from './scatter-chart/scatter-chart.component';
3030
import { DynamicChartComponent } from './dynamic-chart/dynamic-chart.component';
3131
import { FinancialChartComponent } from './financial-chart/financial-chart.component';
3232
import { LandingComponent } from './landing/landing.component';
33+
import { provideClientHydration } from '@angular/platform-browser';
3334

3435
const routes: Route[] = [
3536
{
@@ -114,5 +115,6 @@ export const appConfig: ApplicationConfig = {
114115
provideHttpClient(withInterceptorsFromDi()),
115116
provideAnimations(),
116117
provideRouter(routes),
118+
provideClientHydration(),
117119
],
118120
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { bootstrapApplication } from '@angular/platform-browser';
2+
import { AppComponent } from './app/app.component';
3+
import { config } from './app/app.config.server';
4+
5+
const bootstrap = () => bootstrapApplication(AppComponent, config);
6+
7+
export default bootstrap;

apps/ng2-charts-demo/tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"outDir": "../../dist/out-tsc",
55
"types": ["node"]
66
},
7-
"files": ["src/main.ts"],
7+
"files": ["src/main.ts", "src/main.server.ts", "server.ts"],
88
"include": ["src/**/*.d.ts"],
99
"exclude": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts"]
1010
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* To learn more about this file see: https://angular.io/config/tsconfig. */
2+
{
3+
"extends": "./tsconfig.app.json",
4+
"compilerOptions": {
5+
"outDir": "../../out-tsc/server",
6+
"types": ["node"]
7+
},
8+
"files": ["src/main.server.ts", "server.ts"]
9+
}

libs/ng2-charts/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"name": "ng2-charts",
44
"description": "Reactive, responsive, beautiful charts for Angular based on Chart.js",
55
"peerDependencies": {
6-
"@angular/platform-browser": ">=18.0.0",
7-
"@angular/common": ">=18.0.0",
8-
"@angular/core": ">=18.0.0",
9-
"@angular/cdk": ">=18.0.0",
6+
"@angular/platform-browser": ">=19.0.0",
7+
"@angular/common": ">=19.0.0",
8+
"@angular/core": ">=19.0.0",
9+
"@angular/cdk": ">=19.0.0",
1010
"chart.js": "^3.4.0 || ^4.0.0",
1111
"rxjs": "^6.5.3 || ^7.4.0"
1212
},

0 commit comments

Comments
 (0)