[angular-18 | DONE | FIXED] changes for SSR to work
This commit is contained in:
parent
2aa4de8634
commit
078b25a849
|
@ -59,7 +59,7 @@
|
||||||
"@types/express": "^4.17.0",
|
"@types/express": "^4.17.0",
|
||||||
"@types/jasmine": "~3.6.0",
|
"@types/jasmine": "~3.6.0",
|
||||||
"@types/jasminewd2": "~2.0.3",
|
"@types/jasminewd2": "~2.0.3",
|
||||||
"@types/node": "^16.18.50",
|
"@types/node": "^18.19.1",
|
||||||
"browser-sync": "^3.0.0",
|
"browser-sync": "^3.0.0",
|
||||||
"codelyzer": "^6.0.0",
|
"codelyzer": "^6.0.0",
|
||||||
"jasmine-core": "~3.8.0",
|
"jasmine-core": "~3.8.0",
|
||||||
|
|
23
server.ts
23
server.ts
|
@ -6,7 +6,8 @@ import { CommonEngine } from '@angular/ssr';
|
||||||
import * as express from 'express';
|
import * as express from 'express';
|
||||||
import { existsSync } from 'node:fs';
|
import { existsSync } from 'node:fs';
|
||||||
import { join } from 'node:path';
|
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.
|
// The Express app is exported so that it can be used by serverless Functions.
|
||||||
export function app(): express.Express {
|
export function app(): express.Express {
|
||||||
|
@ -34,15 +35,25 @@ export function app(): express.Express {
|
||||||
|
|
||||||
commonEngine
|
commonEngine
|
||||||
.render({
|
.render({
|
||||||
bootstrap,
|
bootstrap: AppServerModule,
|
||||||
documentFilePath: indexHtml,
|
documentFilePath: indexHtml,
|
||||||
url: `${protocol}://${headers.host}${originalUrl}`,
|
url: `${protocol}://${headers.host}${originalUrl}`,
|
||||||
publicPath: distFolder,
|
publicPath: distFolder,
|
||||||
providers: [
|
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))
|
.then((html) => {
|
||||||
.catch((err) => next(err));
|
res.send(html)})
|
||||||
|
.catch((err) =>
|
||||||
|
{
|
||||||
|
console.error('Error during SSR rendering:', err);
|
||||||
|
res.status(500).send('Internal Server Error');
|
||||||
|
next(err)
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
|
@ -67,5 +78,3 @@ const moduleFilename = mainModule && mainModule.filename || '';
|
||||||
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
|
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
|
||||||
run();
|
run();
|
||||||
}
|
}
|
||||||
|
|
||||||
export default bootstrap;
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import {APP_ID, NgModule} from '@angular/core';
|
import {APP_ID, NgModule} from '@angular/core';
|
||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {CommonModule} from '@angular/common';
|
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 {BrowserModule} from '@angular/platform-browser';
|
||||||
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
|
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
|
||||||
|
|
||||||
|
@ -34,7 +34,6 @@ import {CommunityAccessGuard} from "./utils/communityAccess.guard";
|
||||||
imports: [
|
imports: [
|
||||||
SharedModule,
|
SharedModule,
|
||||||
CommonModule,
|
CommonModule,
|
||||||
HttpClientModule,
|
|
||||||
ErrorModule,
|
ErrorModule,
|
||||||
FormsModule,
|
FormsModule,
|
||||||
NavigationBarModule,
|
NavigationBarModule,
|
||||||
|
@ -49,6 +48,7 @@ import {CommunityAccessGuard} from "./utils/communityAccess.guard";
|
||||||
declarations: [AppComponent, OpenaireErrorPageComponent],
|
declarations: [AppComponent, OpenaireErrorPageComponent],
|
||||||
exports: [AppComponent],
|
exports: [AppComponent],
|
||||||
providers: [
|
providers: [
|
||||||
|
provideHttpClient(withFetch()),
|
||||||
CommunitiesService, CustomizationService, CommunityAccessGuard, SubscribeService,
|
CommunitiesService, CustomizationService, CommunityAccessGuard, SubscribeService,
|
||||||
{provide: APP_ID, useValue: 'serverApp'},
|
{provide: APP_ID, useValue: 'serverApp'},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue