Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window is not defined after update to Angular 9 #159

Closed
anishchenko-anton opened this issue Mar 10, 2020 · 1 comment
Closed

window is not defined after update to Angular 9 #159

anishchenko-anton opened this issue Mar 10, 2020 · 1 comment

Comments

@anishchenko-anton
Copy link

anishchenko-anton commented Mar 10, 2020

Hi
after update Angular 8.3.2 -> 9.0.5

i have issue whit "window is not defined"
on Angular 8 everything ok
but after update 3rd party library and angular to last versions , i have
ReferenceError: window is not defined when i start server.
domino is still in server.ts - and nothing has changed

@anishchenko-anton
Copy link
Author

import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import {enableProdMode} from '@angular/core';
import { readFileSync } from 'fs';
import { renderModuleFactory } from '@angular/platform-server';
import cookieParser from 'cookie-parser';

import * as express from 'express';
import {join} from 'path';
import { InjectionToken } from '@angular/core';

const http = require('http');
var compression = require('compression')

enableProdMode();

// Express server
const app = express();
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main');

// Express Engine
// import { ngExpressEngine } from '@nguniversal/express-engine';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
import { NgxRequest, NgxResponse } from '@gorniv/ngx-universal';
// Import module map for lazy loading
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)

// angular/universal#830
// ssr DOM
const domino = require('domino');
// const fs = require('fs');
const path = require('path');
// index from browser build!
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
// for mock global window by domino
const win = domino.createWindow(template);
// mock
global['window'] = win;
global["navigator"] = win.navigator;
global['document'] = win.document;

app.use(cookieParser());
app.use(compression())

// // ALl regular routes use the Universal engine
const cacheTime = 864000007; // 7 days
app.get('
.*', express.static(join(DIST_FOLDER, 'browser'), { maxAge: cacheTime }));

app.engine('html', (_, options, callback) => {
const host = options.req.headers.host;
const param = options.req.query;
const hl = options.req.query['hl'];
const http = options.req.headers['x-forwarded-proto'] === undefined ? 'https' : options.req.headers['x-forwarded-proto'];
const ip = options.req.headers['x-forwarded-for'] || options.req.connection.remoteAddress;
renderModuleFactory(AppServerModuleNgFactory, {
url: options.req.url,
extraProviders: [
provideModuleMap(LAZY_MODULE_MAP),
{
provide: 'REQUEST',
// useValue: options.req
useFactory: () => options.req,
deps: []
},
{
provide: 'RESPONSE',
useValue: options.req.res
},
{
provide: NgxRequest,
useValue: options.req,
},
{
provide: NgxResponse,
useValue: options.req.res,
},
{
provide:'serverUrl',
useValue: (${http}://${host})
},
{
provide: 'IP',
useValue: ip
},
{
provide: "HL",
useValue: hl
}
]
}).then(html => {
callback(null, html);
});
});

// All regular routes use the Universal engine
app.get('*', (req, res) => {
global['navigator'] = req['headers']['user-agent'];
const http = req.headers['x-forwarded-proto'] === undefined ? 'http' : req.headers['x-forwarded-proto'];
res.render(
'index',
{
req: req,
res: res,
preboot: true,
providers: [
{
provide: REQUEST, useValue: (req)
},
{
provide: RESPONSE, useValue: (res)
},
/// for cookie
{
provide: NgxRequest,
useValue: req,
},
{
provide: NgxResponse,
useValue: res,
}
]
},
);
});

app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));

app.use((err, request, response, next) => {
// console.log( 'ERROR CODE')
// response.status(500).send(‘Something broke!’)
})

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant