Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSR takes wrong environment in Angular

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?

like image 538
Vytautas Pranskunas Avatar asked Aug 31 '18 09:08

Vytautas Pranskunas


People also ask

How does angular maintain different environments?

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.

How does SSR work in angular?

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.

Does angular use SSR?

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.

Can angular read environment variables?

Consuming environment variables in an Angular application is not natively supported.


2 Answers

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" } ] } } }

like image 70
Виктор Могилевский Avatar answered Sep 19 '22 01:09

Виктор Могилевский


  1. Create configuration for server build in angular.json

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" } ] } } }

  1. Edit package.json to use the above created build configuration. As per angular documentation, There must be a key named "build:client-and-server-bundles" in scripts object. Modify "ng build --prod && ng run app:server" to "ng build --prod && ng run app:server -c production".

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"

}

like image 30
Siddharth Hudda Avatar answered Sep 18 '22 01:09

Siddharth Hudda