mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-05-22 05:38:46 +00:00
stringify into window.env only what we really need, save a lot of KBs
This commit is contained in:
parent
34dd4494ec
commit
e1620bad2b
6 changed files with 44 additions and 22 deletions
|
|
@ -99,10 +99,12 @@ expressres.jsonstring = function(obj, headers, status) {
|
|||
|
||||
|
||||
dataexport.avatarPage = function(req, res) {
|
||||
var env = _.defaults({user:user}, res.locals.habitrpg);
|
||||
|
||||
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: env
|
||||
});
|
||||
})
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,6 +9,33 @@ 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', '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});
|
||||
var isStaticPage = req.url.split('/')[1] === 'static'; // If url contains '/static/'
|
||||
|
|
@ -16,12 +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 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 = _.merge(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],
|
||||
|
|
@ -30,23 +53,18 @@ module.exports = function(req, res, next) {
|
|||
args.push(language.code);
|
||||
return shared.i18n.t.apply(null, args);
|
||||
},
|
||||
siteVersion: forceRefresh.siteVersion,
|
||||
Content: shared.content,
|
||||
mods: 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){}
|
||||
if(req.query.partyInvite){
|
||||
try{
|
||||
req.session.partyInvite = JSON.parse(utils.decrypt(req.query.partyInvite));
|
||||
} catch(e){}
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
|
|
@ -8,8 +8,10 @@ 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
|
||||
script(type='text/javascript').
|
||||
window.env = !{JSON.stringify(env)};
|
||||
window.env = !{JSON.stringify(env._.pick(env, env.clientVars.slice(0).push('user')))};
|
||||
|
||||
!= env.getManifestFiles("app")
|
||||
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ html(ng-app='habitrpg')
|
|||
|
||||
|
||||
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')
|
||||
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue