Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit 07a860f

Browse files
simisonlirantal
authored andcommitted
feat(angular): disable Angular debug data in production (#1457)
Disable Angular debug data in production for a significant performance boost. Passes environment variable from template to app config and from there to Angular bootstrap config. https://docs.angularjs.org/guide/production#disabling-debug-data See #1294
1 parent c35713d commit 07a860f

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

config/lib/express.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports.initLocalVariables = function (app) {
3939
app.locals.livereload = config.livereload;
4040
app.locals.logo = config.logo;
4141
app.locals.favicon = config.favicon;
42+
app.locals.env = process.env.NODE_ENV;
4243

4344
// Passing the request url to environment locals
4445
app.use(function (req, res, next) {

modules/core/client/app/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var applicationModuleName = 'mean';
55

66
var service = {
7+
applicationEnvironment: window.env,
78
applicationModuleName: applicationModuleName,
89
applicationModuleVendorDependencies: ['ngResource', 'ngAnimate', 'ngMessages', 'ui.router', 'ui.bootstrap', 'ngFileUpload', 'ngImgCrop'],
910
registerModule: registerModule

modules/core/client/app/init.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
.module(app.applicationModuleName)
1111
.config(bootstrapConfig);
1212

13-
function bootstrapConfig($locationProvider, $httpProvider) {
13+
function bootstrapConfig($compileProvider, $locationProvider, $httpProvider) {
1414
$locationProvider.html5Mode(true).hashPrefix('!');
1515

1616
$httpProvider.interceptors.push('authInterceptor');
17+
18+
// Disable debug data for production environment
19+
// @link https://docs.angularjs.org/guide/production
20+
$compileProvider.debugInfoEnabled(app.applicationEnvironment !== 'production');
1721
}
1822

19-
bootstrapConfig.$inject = ['$locationProvider', '$httpProvider'];
23+
bootstrapConfig.$inject = ['$compileProvider', '$locationProvider', '$httpProvider'];
2024

2125
// Then define the init function for starting up the application
2226
angular.element(document).ready(init);

modules/core/server/views/layout.server.view.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656

5757
<!--Embedding The User Object-->
5858
<script type="text/javascript">
59-
var user = {{ user | json | safe }};
59+
var user = {{ user | json | safe }},
60+
env = "{{ env }}";
6061
</script>
6162

6263
<!--Load The Socket.io File-->

modules/core/tests/server/core.server.config.tests.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ var _ = require('lodash'),
1010
path = require('path'),
1111
fs = require('fs'),
1212
mock = require('mock-fs'),
13+
request = require('supertest'),
1314
config = require(path.resolve('./config/config')),
1415
logger = require(path.resolve('./config/lib/logger')),
15-
seed = require(path.resolve('./config/lib/seed'));
16+
seed = require(path.resolve('./config/lib/seed')),
17+
express = require(path.resolve('./config/lib/express'));
1618

1719
/**
1820
* Globals
1921
*/
20-
var user1,
22+
var app,
23+
agent,
24+
user1,
2125
admin1,
2226
userFromSeedConfig,
2327
adminFromSeedConfig,
@@ -507,4 +511,35 @@ describe('Configuration Tests:', function () {
507511
fileTransport.should.be.false();
508512
});
509513
});
514+
515+
describe('Testing exposing environment as a variable to layout', function () {
516+
517+
['development', 'production', 'test'].forEach(function(env) {
518+
it('should expose environment set to ' + env, function (done) {
519+
// Set env to development for this test
520+
process.env.NODE_ENV = env;
521+
522+
// Gget application
523+
app = express.init(mongoose);
524+
agent = request.agent(app);
525+
526+
// Get rendered layout
527+
agent.get('/')
528+
.expect('Content-Type', 'text/html; charset=utf-8')
529+
.expect(200)
530+
.end(function (err, res) {
531+
// Set env back to test
532+
process.env.NODE_ENV = 'test';
533+
// Handle errors
534+
if (err) {
535+
return done(err);
536+
}
537+
res.text.should.containEql('env = "' + env + '"');
538+
return done();
539+
});
540+
});
541+
});
542+
543+
});
544+
510545
});

0 commit comments

Comments
 (0)