Skip to content

Commit

Permalink
SSR attempt (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelMarks committed Apr 15, 2024
1 parent 54a10cc commit 3a9677f
Show file tree
Hide file tree
Showing 14 changed files with 557 additions and 320 deletions.
9 changes: 7 additions & 2 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@
"styles": [
"src/styles.scss"
],
"scripts": []
"scripts": [],
"server": "src/main.server.ts",
"prerender": true,
"ssr": {
"entry": "server.ts"
}
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "800kb",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
Expand Down
651 changes: 375 additions & 276 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"start": "ng serve --proxy-config proxy.conf.json",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
"test": "ng test",
"serve:ssr:ng-material-scaffold": "node dist/ng-material-scaffold/server/server.mjs",
"serve": "ts-node server.ts"
},
"private": true,
"dependencies": {
Expand All @@ -34,7 +36,10 @@
"@angular/material": "^17.3.4",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/platform-server": "^17.3.0",
"@angular/router": "^17.3.0",
"@angular/ssr": "^17.3.4",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
Expand All @@ -43,13 +48,16 @@
"@angular-devkit/build-angular": "^17.3.4",
"@angular/cli": "^17.3.4",
"@angular/compiler-cli": "^17.3.0",
"@types/express": "^4.17.17",
"@types/jasmine": "~5.1.0",
"@types/node": "^18.18.0",
"jasmine-core": "~5.1.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"ts-node": "^10.9.2",
"typescript": "~5.4.2"
}
}
56 changes: 56 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { APP_BASE_HREF } from '@angular/common';
import { CommonEngine } from '@angular/ssr';
import express from 'express';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';
import bootstrap from './src/main.server';

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

const commonEngine = new CommonEngine();

server.set('view engine', 'html');
server.set('views', browserDistFolder);

// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get('*.*', express.static(browserDistFolder, {
maxAge: '1y'
}));

// All regular routes use the Angular engine
server.get('*', (req, res, next) => {
const { protocol, originalUrl, baseUrl, headers } = req;

commonEngine
.render({
bootstrap,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: browserDistFolder,
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
})
.then((html) => res.send(html))
.catch((err) => next(err));
});

return server;
}

function run(): void {
const port = process.env['PORT'] || 4000;

// Start up the Node server
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}

run();
34 changes: 34 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { Subject } from "rxjs";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { HttpClientModule } from "@angular/common/http";
import { LayoutModule } from "@angular/cdk/layout";
import { MatToolbarModule } from "@angular/material/toolbar";
import { MatButtonModule } from "@angular/material/button";
import { MatSidenavModule } from "@angular/material/sidenav";
import { MatIconModule } from "@angular/material/icon";
import { MatListModule } from "@angular/material/list";
import { MatGridListModule } from "@angular/material/grid-list";
import { MatCardModule } from "@angular/material/card";
import { MatMenuModule } from "@angular/material/menu";
import { AlertsModule } from "./alerts/alerts.module";
import { SidenavModule } from "./sidenav/sidenav.module";

@Component({
selector: 'app-root',
standalone: true,
imports: [
RouterOutlet,
BrowserModule,
// AppRoutingModule,
BrowserAnimationsModule,
HttpClientModule,
LayoutModule,
BrowserAnimationsModule,
MatToolbarModule,
MatButtonModule,
MatSidenavModule,
MatIconModule,
MatListModule,
MatGridListModule,
MatCardModule,
MatMenuModule,
// AlertsModule.forRoot(),
SidenavModule
],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
Expand Down
11 changes: 11 additions & 0 deletions src/app/app.config.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { mergeApplicationConfig, ApplicationConfig } from '@angular/core';
import { provideServerRendering } from '@angular/platform-server';
import { appConfig } from './app.config';

const serverConfig: ApplicationConfig = {
providers: [
provideServerRendering()
]
};

export const config = mergeApplicationConfig(appConfig, serverConfig);
10 changes: 10 additions & 0 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideClientHydration } from '@angular/platform-browser';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';

import { routes } from "./app.routes.module";

export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes), provideClientHydration(), provideAnimationsAsync()]
};
70 changes: 37 additions & 33 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,46 @@ import { SidenavModule } from './sidenav/sidenav.module';
import { AlertsModule } from './alerts/alerts.module';
import { AuthGuard } from './auth/auth.guard';
import { AuthInterceptor } from './auth/auth.interceptors';
import { AppRoutingModule } from './app.routes.module';
import { AppComponent } from './app.component';
// import { AppRoutingModule } from './app.routes.module';
// import { AppComponent } from './app.component';

export const AppImports = [
BrowserModule,
// AppRoutingModule,
BrowserAnimationsModule,
HttpClientModule,
LayoutModule,
BrowserAnimationsModule,
MatToolbarModule,
MatButtonModule,
MatSidenavModule,
MatIconModule,
MatListModule,
MatGridListModule,
MatCardModule,
MatMenuModule,
AlertsModule.forRoot(),
SidenavModule
];

export const providers = [
AuthGuard,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
provideClientHydration(),
provideAnimationsAsync()
];

/*
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
HttpClientModule,
LayoutModule,
BrowserAnimationsModule,
MatToolbarModule,
MatButtonModule,
MatSidenavModule,
MatIconModule,
MatListModule,
MatGridListModule,
MatCardModule,
MatMenuModule,
AlertsModule.forRoot(),
SidenavModule
],
providers: [
AuthGuard,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
provideClientHydration(),
provideAnimationsAsync()
],
declarations: [AppComponent],
imports,
providers,
bootstrap: [AppComponent]
})
export class AppModule {
}
*/
4 changes: 3 additions & 1 deletion src/app/app.routes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from './auth/auth.guard';


const routes: Routes = [
export const routes: Routes = [
{
path: '',
loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule)
Expand All @@ -28,9 +28,11 @@ const routes: Routes = [

export const getRedirectUrl = (url: string): string | null => new URLSearchParams(url.slice(1)).get('redirectUrl');

/*
@NgModule({
imports: [RouterModule.forRoot(routes, {})],
exports: [RouterModule]
})
export class AppRoutingModule {
}
*/
7 changes: 7 additions & 0 deletions src/main.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { config } from './app/app.config.server';

const bootstrap = () => bootstrapApplication(AppComponent, config);

export default bootstrap;
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { platformBrowser } from '@angular/platform-browser';

import { AppModule } from "./app/app.module";
import { AppComponent } from './app/app.component';

platformBrowser().bootstrapModule(AppModule)
platformBrowser().bootstrapModule(AppComponent)
.catch((err) => console.error(err));
9 changes: 6 additions & 3 deletions tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": []
"types": [
"node"
]
},
"files": [
"src/main.ts"
"src/main.ts",
"src/main.server.ts",
"server.ts"
],
"include": [
"src/**/*.d.ts"
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
Expand Down
1 change: 0 additions & 1 deletion tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
Expand Down

0 comments on commit 3a9677f

Please sign in to comment.