I think the reason is that in webpack.server.config.js the mode is set to 'none', not doing so gives an error when building the ssr, so the environment is not set to 'production'.
The solution I'm implementing for now is to set a new configuration with fileReplacements in the server build (angular.json):
"server": {
"builder": "@angular-devkit/build-angular:server",
"options": {
"outputPath": "dist/server",
"main": "src/main.server.ts",
"tsConfig": "src/tsconfig.server.json"
},
"configurations": {
"mynewconfig": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
}
Then in the package.json I add that configuration when building the ssr:
"build:client-and-server-bundles": "ng build --prod && ng run my-app:server --configuration=mynewconfig",
This is for Angular > 6, for prior versions you can use the --env flag instead of --configuration.