Merge pull request #5789 from HabitRPG/paglias/loading-times

Stringify into window.env only what is used on the client
This commit is contained in:
Matteo Pagliazzi 2015-08-14 22:14:54 +02:00
commit a58c3d1e8b
10 changed files with 64 additions and 29 deletions

View file

@ -102,7 +102,7 @@ dataexport.avatarPage = function(req, res) {
User.findById(req.params.uuid).select('stats profile items achievements preferences backer contributor').exec(function(err, user){
res.render('avatar-static', {
title: user.profile.name,
env: _.defaults({user:user},res.locals.habitrpg)
env: _.defaults({user:user}, res.locals.habitrpg)
});
})
};

View file

@ -6,6 +6,35 @@ var i18n = require('../i18n.js');
var buildManifest = require('../libs/buildManifest');
var shared = require('../../../common');
var forceRefresh = require('./forceRefresh');
var tavern = require('../models/group').tavern;
var mods = require('../models/user').mods;
// To avoid stringifying more data then we need,
// items from `env` used on the client will have to be specified in this array
var clientVars = ['language', 'isStaticPage', 'avalaibleLanguages', 'translations',
'FACEBOOK_KEY', 'NODE_ENV', 'BASE_URL', 'GA_ID',
'AMAZON_PAYMENTS', 'STRIPE_PUB_KEY', 'AMPLITUDE_KEY',
'worldDmg', 'mods', 'IS_MOBILE'];
var env = {
getManifestFiles: buildManifest.getManifestFiles,
getBuildUrl: buildManifest.getBuildUrl,
_: _,
clientVars: clientVars,
tavern: tavern, // for world boss
mods: mods,
Content: shared.content,
siteVersion: forceRefresh.siteVersion,
avalaibleLanguages: i18n.avalaibleLanguages,
AMAZON_PAYMENTS: {
SELLER_ID: nconf.get('AMAZON_PAYMENTS:SELLER_ID'),
CLIENT_ID: nconf.get('AMAZON_PAYMENTS:CLIENT_ID')
}
};
'NODE_ENV BASE_URL GA_ID STRIPE_PUB_KEY FACEBOOK_KEY AMPLITUDE_KEY'.split(' ').forEach(function(key){
env[key] = nconf.get(key);
});
module.exports = function(req, res, next) {
var language = _.find(i18n.avalaibleLanguages, {code: req.language});
@ -14,13 +43,8 @@ module.exports = function(req, res, next) {
// Load moment.js language file only when not on static pages
language.momentLang = ((!isStaticPage && i18n.momentLangs[language.code]) || undefined);
var tavern = require('../models/group').tavern;
var envVars = _.pick(nconf.get(), 'NODE_ENV BASE_URL GA_ID STRIPE_PUB_KEY FACEBOOK_KEY AMPLITUDE_KEY'.split(' '));
res.locals.habitrpg = _.merge(envVars, {
res.locals.habitrpg = _.assign(env, {
IS_MOBILE: /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(req.header('User-Agent')),
getManifestFiles: buildManifest.getManifestFiles,
getBuildUrl: buildManifest.getBuildUrl,
avalaibleLanguages: i18n.avalaibleLanguages,
language: language,
isStaticPage: isStaticPage,
translations: i18n.translations[language.code],
@ -29,23 +53,19 @@ module.exports = function(req, res, next) {
args.push(language.code);
return shared.i18n.t.apply(null, args);
},
siteVersion: forceRefresh.siteVersion,
Content: shared.content,
mods: require('../models/user').mods,
tavern: tavern, // for world boss
// Defined here and not outside of the middleware because tavern might be an
// empty object until the query to fetch it finishes
worldDmg: (tavern && tavern.quest && tavern.quest.extra && tavern.quest.extra.worldDmg) || {},
_: _,
AMAZON_PAYMENTS: {
SELLER_ID: nconf.get('AMAZON_PAYMENTS:SELLER_ID'),
CLIENT_ID: nconf.get('AMAZON_PAYMENTS:CLIENT_ID')
}
});
// Put query-string party (& guild but use partyInvite for backward compatibility)
// invitations into session to be handled later
try{
req.session.partyInvite = JSON.parse(utils.decrypt(req.query.partyInvite));
} catch(e){}
// TODO once we have proper logging in place, log errors here
if(req.query.partyInvite){
try{
req.session.partyInvite = JSON.parse(utils.decrypt(req.query.partyInvite));
} catch(e){}
}
next();
};

View file

@ -236,7 +236,8 @@ module.exports.tavern = {};
var tavernQ = {_id:'habitrpg','quest.key':{$ne:null}};
process.nextTick(function(){
mongoose.model('Group').findOne(tavernQ,function(err,tavern){
module.exports.tavern = tavern;
// Using _assign so we don't lose the reference to the exported tavern
_.assign(module.exports.tavern, tavern);
});
})
GroupSchema.statics.tavernBoss = function(user,progress) {

View file

@ -577,11 +577,16 @@ UserSchema.methods.unlink = function(options, cb) {
module.exports.schema = UserSchema;
module.exports.model = mongoose.model("User", UserSchema);
// Initially export an empty object so external requires will get
// the right object by reference when it's defined later
// Otherwise it would remain undefined if requested before the query executes
module.exports.mods = [];
mongoose.model("User")
.find({'contributor.admin':true})
.sort('-contributor.level -backer.npc profile.name')
.select('profile contributor backer')
.exec(function(err,mods){
module.exports.mods = mods
// Using push to maintain the reference to mods
module.exports.mods.push.apply(module.exports.mods, mods);
});

View file

@ -8,8 +8,15 @@ html(ng-app="habitrpg")
meta(name='viewport', content='width=device-width, initial-scale=1.0')
meta(name='apple-mobile-web-app-capable', content='yes')
// .slice(0).push('user') is to clone the array,
// to be surethat `user` is never avalaible to other requests' env
// TODO does it need only `user` in clientVars, not the others?
-
clientVars = env.clientVars.slice(0);
clientVars.push('user');
script(type='text/javascript').
window.env = !{JSON.stringify(env)};
window.env = !{JSON.stringify(env._.pick(env, clientVars))};
!= env.getManifestFiles("app")

File diff suppressed because one or more lines are too long

View file

@ -144,6 +144,7 @@
include ./chat-box
.alert.alert-info.alert-sm
!= ' ' + env.t('tavernAlert1') + ' <a href="https://github.com/HabitRPG/habitrpg/issues/2760" target="_blank">' + env.t('tavernAlert2') + '</a>.<br />' + env.t('moderatorIntro1')
//- FIXME this could be rendered server-side?
span(ng-repeat='mod in env.mods')
|&nbsp; &nbsp;
span(ng-if='::mod.contributor.admin',popover=env.t('gamemaster'),popover-trigger='mouseenter',popover-placement='right')

View file

@ -26,7 +26,7 @@ html(ng-app='habitrpg', ng-controller='RootCtrl')
link(rel='stylesheet', type='text/css', href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css')
script(type='text/javascript').
window.env = !{JSON.stringify(env)};
window.env = !{JSON.stringify(env._.pick(env, env.clientVars))};
!= env.getManifestFiles("tmp_static_front")
script(type='text/javascript', src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js')

File diff suppressed because one or more lines are too long

View file

@ -7,7 +7,7 @@ html(ng-app='habitrpg')
title=env.t('newStuff')
script(type='text/javascript').
window.env = !{JSON.stringify(env)};
window.env = !{JSON.stringify(env._.pick(env, env.clientVars))};
link(rel='shortcut icon', href='#{env.getBuildUrl("favicon.ico")}?v=4')