[angular-18 | DONE | FIXED] changes for SSR to work

This commit is contained in:
argirok 2024-09-17 11:02:53 +03:00
parent 2aa4de8634
commit 078b25a849
3 changed files with 20 additions and 11 deletions

View File

@ -59,7 +59,7 @@
"@types/express": "^4.17.0",
"@types/jasmine": "~3.6.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^16.18.50",
"@types/node": "^18.19.1",
"browser-sync": "^3.0.0",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.8.0",
@ -73,4 +73,4 @@
"ts-node": "~7.0.0",
"typescript": "~5.4.0"
}
}
}

View File

@ -6,7 +6,8 @@ import { CommonEngine } from '@angular/ssr';
import * as express from 'express';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import bootstrap from './src/main.server';
import { AppServerModule } from './src/app/app.server.module';
import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens";
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
@ -34,15 +35,25 @@ export function app(): express.Express {
commonEngine
.render({
bootstrap,
bootstrap: AppServerModule,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: distFolder,
providers: [
{ provide: APP_BASE_HREF, useValue: baseUrl },],
{ provide: APP_BASE_HREF, useValue: baseUrl },
{provide: REQUEST, useValue: (req)},
{provide: RESPONSE, useValue: (res)}
],
})
.then((html) => res.send(html))
.catch((err) => next(err));
.then((html) => {
res.send(html)})
.catch((err) =>
{
console.error('Error during SSR rendering:', err);
res.status(500).send('Internal Server Error');
next(err)
}
);
});
return server;
@ -67,5 +78,3 @@ const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export default bootstrap;

View File

@ -1,7 +1,7 @@
import {APP_ID, NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {CommonModule} from '@angular/common';
import {HTTP_INTERCEPTORS, HttpClientModule} from "@angular/common/http";
import {HTTP_INTERCEPTORS, provideHttpClient, withFetch} from "@angular/common/http";
import {BrowserModule} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
@ -34,7 +34,6 @@ import {CommunityAccessGuard} from "./utils/communityAccess.guard";
imports: [
SharedModule,
CommonModule,
HttpClientModule,
ErrorModule,
FormsModule,
NavigationBarModule,
@ -49,6 +48,7 @@ import {CommunityAccessGuard} from "./utils/communityAccess.guard";
declarations: [AppComponent, OpenaireErrorPageComponent],
exports: [AppComponent],
providers: [
provideHttpClient(withFetch()),
CommunitiesService, CustomizationService, CommunityAccessGuard, SubscribeService,
{provide: APP_ID, useValue: 'serverApp'},
{