|
@@ -4,29 +4,25 @@ import * as express from 'express';
|
|
|
import { join } from 'path';
|
|
import { join } from 'path';
|
|
|
import { AppServerModule } from './src/main.server';
|
|
import { AppServerModule } from './src/main.server';
|
|
|
import { APP_BASE_HREF } from '@angular/common';
|
|
import { APP_BASE_HREF } from '@angular/common';
|
|
|
-import { existsSync } from 'fs';
|
|
|
|
|
|
|
+import { existsSync, readFileSync } from 'fs';
|
|
|
|
|
|
|
|
-// Pour simuler le DOM côté serveur (utile pour les librairies qui accèdent à window/document)
|
|
|
|
|
|
|
+// Simulation DOM (nécessaire pour certaines librairies côté serveur)
|
|
|
const domino = require('domino');
|
|
const domino = require('domino');
|
|
|
-const fs = require('fs');
|
|
|
|
|
-const path = require('path');
|
|
|
|
|
-
|
|
|
|
|
-// Dossiers de build
|
|
|
|
|
const distFolder = join(process.cwd(), 'dist/fatboar/browser');
|
|
const distFolder = join(process.cwd(), 'dist/fatboar/browser');
|
|
|
const indexHtml = existsSync(join(distFolder, 'index.html')) ? 'index.html' : 'index';
|
|
const indexHtml = existsSync(join(distFolder, 'index.html')) ? 'index.html' : 'index';
|
|
|
-const template = fs.readFileSync(join(distFolder, indexHtml)).toString();
|
|
|
|
|
|
|
+const template = readFileSync(join(distFolder, indexHtml)).toString();
|
|
|
|
|
|
|
|
-// Simule le DOM pour SSR
|
|
|
|
|
|
|
+// Simuler un DOM global pour les libs (Leaflet, Material, etc.)
|
|
|
const win = domino.createWindow(template);
|
|
const win = domino.createWindow(template);
|
|
|
global['window'] = win;
|
|
global['window'] = win;
|
|
|
global['document'] = win.document;
|
|
global['document'] = win.document;
|
|
|
global['navigator'] = win.navigator;
|
|
global['navigator'] = win.navigator;
|
|
|
-global['CSS'] = null; // Certaines libs vérifient CSS
|
|
|
|
|
|
|
+global['CSS'] = null;
|
|
|
|
|
|
|
|
export function app() {
|
|
export function app() {
|
|
|
const server = express();
|
|
const server = express();
|
|
|
|
|
|
|
|
- // Configure le moteur de rendu Angular Universal
|
|
|
|
|
|
|
+ // Moteur de rendu Angular Universal
|
|
|
server.engine('html', ngExpressEngine({
|
|
server.engine('html', ngExpressEngine({
|
|
|
bootstrap: AppServerModule,
|
|
bootstrap: AppServerModule,
|
|
|
}));
|
|
}));
|
|
@@ -34,31 +30,50 @@ export function app() {
|
|
|
server.set('view engine', 'html');
|
|
server.set('view engine', 'html');
|
|
|
server.set('views', distFolder);
|
|
server.set('views', distFolder);
|
|
|
|
|
|
|
|
- // Sert les fichiers statiques (assets, images, etc.)
|
|
|
|
|
- server.get('*.*', express.static(distFolder, {
|
|
|
|
|
- maxAge: '1y'
|
|
|
|
|
- }));
|
|
|
|
|
|
|
+ // Fichiers statiques (CSS, JS, images…)
|
|
|
|
|
+ server.get('*.*', express.static(distFolder, { maxAge: '1y' }));
|
|
|
|
|
|
|
|
- // Sert sitemap.xml pour le SEO
|
|
|
|
|
|
|
+ // Routes SEO
|
|
|
server.get('/sitemap.xml', (req, res) => {
|
|
server.get('/sitemap.xml', (req, res) => {
|
|
|
res.header('Content-Type', 'application/xml');
|
|
res.header('Content-Type', 'application/xml');
|
|
|
res.sendFile(join(distFolder, 'sitemap.xml'));
|
|
res.sendFile(join(distFolder, 'sitemap.xml'));
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- // Sert robots.txt pour le SEO
|
|
|
|
|
server.get('/robots.txt', (req, res) => {
|
|
server.get('/robots.txt', (req, res) => {
|
|
|
res.sendFile(join(distFolder, 'robots.txt'));
|
|
res.sendFile(join(distFolder, 'robots.txt'));
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- // Sert toutes les autres routes via Angular Universal
|
|
|
|
|
|
|
+ // Toutes les autres routes => rendu SSR
|
|
|
server.get('*', (req, res) => {
|
|
server.get('*', (req, res) => {
|
|
|
res.render(indexHtml, {
|
|
res.render(indexHtml, {
|
|
|
req,
|
|
req,
|
|
|
providers: [
|
|
providers: [
|
|
|
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
|
|
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
|
|
|
- // Fournit l'URL canonique pour les balises <link rel="canonical">
|
|
|
|
|
{ provide: 'CANONICAL_URL', useValue: `https://angular-dev.foodgame.fr${req.originalUrl}` }
|
|
{ provide: 'CANONICAL_URL', useValue: `https://angular-dev.foodgame.fr${req.originalUrl}` }
|
|
|
]
|
|
]
|
|
|
|
|
+ }, (err, html) => {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ console.error(err);
|
|
|
|
|
+ return res.status(500).send(err);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Injection Preboot pour capturer les interactions avant bootstrap Angular
|
|
|
|
|
+ const prebootScript = `
|
|
|
|
|
+ <script src="https://unpkg.com/preboot"></script>
|
|
|
|
|
+ <script>preboot.init({ appRoot: 'app-root' });</script>
|
|
|
|
|
+ `;
|
|
|
|
|
+
|
|
|
|
|
+ // Injection des CSS directement dans le SSR pour éviter le flash
|
|
|
|
|
+ const styles = `
|
|
|
|
|
+ <link rel="stylesheet" href="styles.css">
|
|
|
|
|
+ <link rel="stylesheet" href="assets/theme.css">
|
|
|
|
|
+ `;
|
|
|
|
|
+
|
|
|
|
|
+ res.send(
|
|
|
|
|
+ html
|
|
|
|
|
+ .replace('</head>', `${styles}</head>`)
|
|
|
|
|
+ .replace('</body>', `${prebootScript}</body>`)
|
|
|
|
|
+ );
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -73,7 +88,7 @@ function run() {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Démarre le serveur si ce fichier est lancé directement
|
|
|
|
|
|
|
+// Exécuter le serveur si lancé directement
|
|
|
declare const __non_webpack_require__: NodeRequire;
|
|
declare const __non_webpack_require__: NodeRequire;
|
|
|
const mainModule = __non_webpack_require__.main;
|
|
const mainModule = __non_webpack_require__.main;
|
|
|
const moduleFilename = mainModule && mainModule.filename || '';
|
|
const moduleFilename = mainModule && mainModule.filename || '';
|
|
@@ -82,81 +97,3 @@ if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export * from './src/main.server';
|
|
export * from './src/main.server';
|
|
|
-
|
|
|
|
|
-// import 'zone.js/dist/zone-node';
|
|
|
|
|
-// import { ngExpressEngine } from '@nguniversal/express-engine';
|
|
|
|
|
-// import * as express from 'express';
|
|
|
|
|
-// import { join } from 'path';
|
|
|
|
|
-// import { AppServerModule } from './src/main.server';
|
|
|
|
|
-// import { APP_BASE_HREF } from '@angular/common';
|
|
|
|
|
-// import { existsSync } from 'fs';
|
|
|
|
|
-
|
|
|
|
|
-// const domino = require('domino');
|
|
|
|
|
-// const fs = require('fs');
|
|
|
|
|
-// const path = require('path');
|
|
|
|
|
-
|
|
|
|
|
-// const distFolder = join(process.cwd(), 'dist/fatboar/browser');
|
|
|
|
|
-// const indexHtml = existsSync(join(distFolder, 'index.html')) ? 'index.html' : 'index';
|
|
|
|
|
-// const template = fs.readFileSync(join(distFolder, indexHtml)).toString();
|
|
|
|
|
-
|
|
|
|
|
-// const win = domino.createWindow(template);
|
|
|
|
|
-// global['window'] = win;
|
|
|
|
|
-// global['document'] = win.document;
|
|
|
|
|
-// global['navigator'] = win.navigator;
|
|
|
|
|
-// global['CSS'] = null;
|
|
|
|
|
-
|
|
|
|
|
-// export function app() {
|
|
|
|
|
-// const server = express();
|
|
|
|
|
-
|
|
|
|
|
-// server.engine('html', ngExpressEngine({
|
|
|
|
|
-// bootstrap: AppServerModule,
|
|
|
|
|
-// }));
|
|
|
|
|
-
|
|
|
|
|
-// server.set('view engine', 'html');
|
|
|
|
|
-// server.set('views', distFolder);
|
|
|
|
|
-
|
|
|
|
|
-// // Fichiers statiques
|
|
|
|
|
-// server.get('*.*', express.static(distFolder, {
|
|
|
|
|
-// maxAge: '1y'
|
|
|
|
|
-// }));
|
|
|
|
|
-
|
|
|
|
|
-// // Routes SEO (déplacées depuis Express)
|
|
|
|
|
-// server.get('/sitemap.xml', (req, res) => {
|
|
|
|
|
-// res.header('Content-Type', 'application/xml');
|
|
|
|
|
-// res.sendFile(join(distFolder, 'sitemap.xml'));
|
|
|
|
|
-// });
|
|
|
|
|
-
|
|
|
|
|
-// server.get('/robots.txt', (req, res) => {
|
|
|
|
|
-// res.sendFile(join(distFolder, 'robots.txt'));
|
|
|
|
|
-// });
|
|
|
|
|
-
|
|
|
|
|
-// // Toutes les autres routes
|
|
|
|
|
-// server.get('*', (req, res) => {
|
|
|
|
|
-// res.render(indexHtml, {
|
|
|
|
|
-// req,
|
|
|
|
|
-// providers: [
|
|
|
|
|
-// { provide: APP_BASE_HREF, useValue: req.baseUrl },
|
|
|
|
|
-// { provide: 'CANONICAL_URL', useValue: `https://angular-dev.foodgame.fr${req.originalUrl}` }
|
|
|
|
|
-// ]
|
|
|
|
|
-// });
|
|
|
|
|
-// });
|
|
|
|
|
-
|
|
|
|
|
-// return server;
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
-// function run() {
|
|
|
|
|
-// const port = process.env.PORT || 4000;
|
|
|
|
|
-// const server = app();
|
|
|
|
|
-// server.listen(port, () => {
|
|
|
|
|
-// console.log(`Node Express server listening on http://localhost:${port}`);
|
|
|
|
|
-// });
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
-// declare const __non_webpack_require__: NodeRequire;
|
|
|
|
|
-// const mainModule = __non_webpack_require__.main;
|
|
|
|
|
-// const moduleFilename = mainModule && mainModule.filename || '';
|
|
|
|
|
-// if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
|
|
|
|
|
-// run();
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
-// export * from './src/main.server';
|
|
|