I have Angular 6 application with SSR in place. I noticed that it takes wrong environmanet variable file (environment.ts) once on SSR in server.js (not happening without SSR)
this is peace of compiled server.js
var environment_1 = __webpack_require__(/*! ../../../../environments/environment */ "./src/environments/environment.ts");
and it is natural because when compiling browser angular.json swaps files
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
however once webpack compiles server it just takes enironment.ts which is dev configuration
/***/ "./src/environments/environment.ts":
/*!*****************************************!*\
!*** ./src/environments/environment.ts ***!
\*****************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.
Object.defineProperty(exports, "__esModule", { value: true });
exports.environment = {
production: false,
apiUrl: 'dev url',
googleMapsApiKey: 'dev key'
};
/***/ }),
also you can see angulars outdated suggestion to use ng build --env=prod
but i use ng build --configuration=prod
also tried with just ng build --prod
.
Any ideas how to solve this?
Adding multiple environments:Open your angular project in a preferred IDE. Navigate to src>environments, there you can see two files environment. ts and environment. prod.
Angular Interview Q & A series Server side Rendering (SSR) is a modern technique to convert a Single Page Application (SPA) running in the browser into a server based application. Usually, in SPA, the server returns a simple index. html file with the reference to the JavaScript based SPA app.
There are a few reasons you may want to use Server-Side Rendering with your Angular application. SSR helps with Search Engine Optimization. Search engines can parse the page since it is rendered on the server.
Consuming environment variables in an Angular application is not natively supported.
I have solved this problem. You need to check your angular.json file. In the server section, you need "server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "./dist/server",
"main": "./src/client/main.server.ts",
"tsConfig": "./src/client/tsconfig.server.json"
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/client/environments/environment.ts",
"with": "src/client/environments/environment.prod.ts"
}
]
},
"cloud": {
"fileReplacements": [
{
"replace": "src/client/environments/environment.ts",
"with": "src/client/environments/environment.cloud.ts"
}
]
}
}
}
Sample Code: "server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/server",
"main": "src/main.server.ts",
"tsConfig": "src/tsconfig.server.json"
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
}
}
}
Sample code:
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
"serve:ssr": "node dist/server",
"build:client-and-server-bundles": "ng build --prod && ng run app:server -c production",
"webpack:server": "webpack --config webpack.server.config.js --progress --colors"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With