commit-for-intial-render-fix

This commit is contained in:
dhiva03 2019-07-24 01:14:38 +05:30
parent a4c96299b1
commit eef62dfd02
4 changed files with 53 additions and 43 deletions

View File

@ -10,7 +10,7 @@
"build:prerender": "npm run build:client-and-server-bundles && npm run compile:server && npm run generate:prerender", "build:prerender": "npm run build:client-and-server-bundles && npm run compile:server && npm run generate:prerender",
"build:ssr": "npm run build:client-and-server-bundles && npm run compile:server && npm run move:build", "build:ssr": "npm run build:client-and-server-bundles && npm run compile:server && npm run move:build",
"compile:server": "tsc -p server.tsconfig.json", "compile:server": "tsc -p server.tsconfig.json",
"move:build" : "(cp -r ./server/i18n/ dist/server)", "move:build": "(cp -r ./server/i18n/ dist/server)",
"generate:prerender": "cd dist && node prerender", "generate:prerender": "cd dist && node prerender",
"serve:prerender": "cd dist/browser && http-server", "serve:prerender": "cd dist/browser && http-server",
"serve:ssr": "node dist/server" "serve:ssr": "node dist/server"
@ -38,6 +38,7 @@
"express": "^4.15.2", "express": "^4.15.2",
"express-static-gzip": "^1.1.3", "express-static-gzip": "^1.1.3",
"helmet": "^3.18.0", "helmet": "^3.18.0",
"jsdom": "^15.1.1",
"moment": "^2.24.0", "moment": "^2.24.0",
"morgan": "^1.9.1", "morgan": "^1.9.1",
"ngx-cookie-service": "^2.2.0", "ngx-cookie-service": "^2.2.0",

View File

@ -1,18 +1,15 @@
import "zone.js/dist/zone-node"; import "zone.js/dist/zone-node";
import "reflect-metadata"; import "reflect-metadata";
const domino = require("domino");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const template = fs const template = fs
.readFileSync(path.join(__dirname, ".", "browser", "index.html")) .readFileSync(path.join(__dirname, ".", "browser", "index.html"))
.toString(); .toString();
const win = domino.createWindow(template); const jsdom = require("jsdom");
// const styleFiles = files.filter(file => file.startsWith('styles')); const { JSDOM } = jsdom;
// const hashStyle = styleFiles[0].split('.')[1]; const { window } = new JSDOM(template);
// const style = fs.readFileSync(path.join(__dirname, '.', 'dist-server', `styles.${hashStyle}.bundle.css`)).toString(); global["window"] = window;
Object.defineProperty(window.document.body.style, "transform", {
global["window"] = win;
Object.defineProperty(win.document.body.style, "transform", {
value: () => { value: () => {
return { return {
enumerable: true, enumerable: true,
@ -20,10 +17,7 @@ Object.defineProperty(win.document.body.style, "transform", {
}; };
} }
}); });
global["document"] = win.document; global["document"] = window.document;
// global["CSS"] = win;
// global['XMLHttpRequest'] = require('xmlhttprequest').XMLHttpRequest;
// global["Prism"] = null;
import { enableProdMode } from "@angular/core"; import { enableProdMode } from "@angular/core";
@ -80,7 +74,10 @@ const controllers = [
app.use(errorMiddleware); app.use(errorMiddleware);
app.use(bodyParser.json({ limit: "50mb" })); app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({ extended: false }));
app.use("/api-docs/**", express.static(path.join(__dirname, "./server/swagger"))); app.use(
"/api-docs/**",
express.static(path.join(__dirname, "./server/swagger"))
);
app.get("/swagger.json", function(req, res) { app.get("/swagger.json", function(req, res) {
res.setHeader("Content-Type", "application/json"); res.setHeader("Content-Type", "application/json");
res.send( res.send(
@ -95,8 +92,9 @@ controllers.forEach(controller => {
app.use("/epic_explorer/v1", controller.router); app.use("/epic_explorer/v1", controller.router);
}); });
// Example Express Rest API endpoints // Example Express Rest API endpoints
app.get('/epic_explorer/v1/**', (req, res) => { res.send({'msg':'Api works.'})}); app.get("/epic_explorer/v1/**", (req, res) => {
res.send({ msg: "Api works." });
});
// * NOTE :: leave this as require() since this file is built Dynamically from webpack // * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require("./server/main"); const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require("./server/main");
@ -110,10 +108,8 @@ app.engine(
}) })
); );
app.set("view engine", "html");
app.set("views", join(DIST_FOLDER, "browser")); app.set("views", join(DIST_FOLDER, "browser"));
app.set("view engine", "html");
// Server static files from /browser // Server static files from /browser
app.get( app.get(
"*.*", "*.*",
@ -124,7 +120,7 @@ app.get(
// All regular routes use the Universal engine // All regular routes use the Universal engine
app.get("*", (req, res) => { app.get("*", (req, res) => {
res.render("index", { req }); res.sendFile(join(DIST_FOLDER, "browser") + "/index.html", { req });
}); });
// Start up the Node server // Start up the Node server

View File

@ -1,42 +1,55 @@
import { Injectable } from '@angular/core'; import { Injectable, Inject, PLATFORM_ID } from "@angular/core";
import {TranslateService} from '@ngx-translate/core'; import { TranslateService } from "@ngx-translate/core";
import { CookieService } from 'ngx-cookie-service'; import { CookieService } from "ngx-cookie-service";
import { isPlatformBrowser } from "@angular/common";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: "root"
}) })
export class TransServiceService{ export class TransServiceService {
public langLabel: any = {}; public langLabel: any = {};
constructor(public translate : TranslateService,private cookie: CookieService) { constructor(
translate.addLangs(['en', 'de']); @Inject(PLATFORM_ID) private platformId: Object,
translate.setDefaultLang('en'); public translate: TranslateService,
this.langLabel = {en: 'English', de: 'German'}; private cookie: CookieService
// console.log('this.getCookie() ifconf',this.getCookie() != 'undefined' ? 1: 2); ) {
// console.log("(this.getCookie() && this.getCookie() != null) ? this.getCookie() : (this.getCurrentLang() && this.getCurrentLang() != null) ? this.getCurrentLang() : 'en'",(this.getCookie() && this.getCookie() != 'undefined') ? this.getCookie() : (this.getCurrentLang() && this.getCurrentLang() != 'undefined') ? this.getCurrentLang() : 'en'); if (isPlatformBrowser(this.platformId)) {
translate.use((this.getCookie() && this.getCookie() != 'undefined') ? this.getCookie() : (this.getCurrentLang() && this.getCurrentLang() != 'undefined') ? this.getCurrentLang() : 'en'); translate.addLangs(["en", "de"]);
translate.setDefaultLang("en");
this.setCookie(this.getCurrentLang()); this.langLabel = { en: "English", de: "German" };
// console.log('this.getCookie() ifconf',this.getCookie() != 'undefined' ? 1: 2);
// console.log("(this.getCookie() && this.getCookie() != null) ? this.getCookie() : (this.getCurrentLang() && this.getCurrentLang() != null) ? this.getCurrentLang() : 'en'",(this.getCookie() && this.getCookie() != 'undefined') ? this.getCookie() : (this.getCurrentLang() && this.getCurrentLang() != 'undefined') ? this.getCurrentLang() : 'en');
translate.use(
this.getCookie() && this.getCookie() != "undefined"
? this.getCookie()
: this.getCurrentLang() && this.getCurrentLang() != "undefined"
? this.getCurrentLang()
: "en"
);
this.setCookie(this.getCurrentLang());
}
} }
public getLanguage(){ public getLanguage() {
return this.translate.getLangs(); return this.translate.getLangs();
} }
public getCurrentLang(){ public getCurrentLang() {
return this.translate.currentLang; return this.translate.currentLang;
} }
public changeLang(langSelect){ public changeLang(langSelect) {
this.translate.use(langSelect); this.translate.use(langSelect);
this.setCookie(langSelect); this.setCookie(langSelect);
} }
public setCookie(lang){ public setCookie(lang) {
this.cookie.set('Lang',lang); this.cookie.set("Lang", lang);
} }
public getCookie(){ public getCookie() {
return this.cookie.get('Lang'); return this.cookie.get("Lang");
} }
} }

View File

@ -55,8 +55,8 @@
/*************************************************************************************************** /***************************************************************************************************
* Zone JS is required by default for Angular itself. * Zone JS is required by default for Angular itself.
*/ */
import 'zone.js/dist/zone'; // Included with Angular CLI. import "zone.js/dist/zone"; // Included with Angular CLI.
(window as any).global = window;
/*************************************************************************************************** /***************************************************************************************************
* APPLICATION IMPORTS * APPLICATION IMPORTS