From ba1ffe194cf570993af45b1ddc8e956d2c7ec1c3 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Mon, 5 Jan 2015 14:43:20 -0800 Subject: [PATCH] Deferring user creation until after the client app loads, to try and lower the massive number of anonymous users that are created. --- app/core/initialize.coffee | 13 ++++++++++--- server_setup.coffee | 29 +++++++++-------------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/app/core/initialize.coffee b/app/core/initialize.coffee index 2bceda950..3d42b664a 100644 --- a/app/core/initialize.coffee +++ b/app/core/initialize.coffee @@ -1,5 +1,5 @@ Backbone.Mediator.setValidationEnabled false -app = require 'core/application' +app = null channelSchemas = 'auth': require 'schemas/subscriptions/auth' @@ -21,6 +21,15 @@ definitionSchemas = 'misc': require 'schemas/definitions/misc' init = -> + return if app + if not window.userObject._id + $.ajax('/auth/whoami', {success: (res) -> + window.userObject = res + init() + }) + return + + app = require 'core/application' setupConsoleLogging() watchForErrors() setUpIOSLogging() @@ -34,8 +43,6 @@ init = -> handleNormalUrls() setUpMoment() # Set up i18n for moment -module.exports.init = init = _.once init - handleNormalUrls = -> # http://artsy.github.com/blog/2012/06/25/replacing-hashbang-routes-with-pushstate/ $(document).on 'click', "a[href^='/']", (event) -> diff --git a/server_setup.coffee b/server_setup.coffee index 7b99ff29d..df81d50a0 100644 --- a/server_setup.coffee +++ b/server_setup.coffee @@ -127,26 +127,15 @@ setupJavascript404s = (app) -> setupFallbackRouteToIndex = (app) -> app.all '*', (req, res) -> - if req.user - sendMain(req, res) - # Disabling for HoC -# req.user.set('lastIP', req.connection.remoteAddress) -# req.user.save() - else - user = auth.makeNewUser(req) - makeNext = (req, res) -> -> sendMain(req, res) - next = makeNext(req, res) - auth.loginUser(req, res, user, false, next) - -sendMain = (req, res) -> - fs.readFile path.join(__dirname, 'public', 'main.html'), 'utf8', (err, data) -> - log.error "Error modifying main.html: #{err}" if err - # insert the user object directly into the html so the application can have it immediately. Sanitize - data = data.replace('"userObjectTag"', JSON.stringify(UserHandler.formatEntity(req, req.user)).replace(/\//g, '\\/')) - res.header 'Cache-Control', 'no-cache, no-store, must-revalidate' - res.header 'Pragma', 'no-cache' - res.header 'Expires', 0 - res.send 200, data + fs.readFile path.join(__dirname, 'public', 'main.html'), 'utf8', (err, data) -> + log.error "Error modifying main.html: #{err}" if err + # insert the user object directly into the html so the application can have it immediately. Sanitize + user = if req.user then JSON.stringify(UserHandler.formatEntity(req, req.user)).replace(/\//g, '\\/') else '{}' + data = data.replace('"userObjectTag"', user) + res.header 'Cache-Control', 'no-cache, no-store, must-revalidate' + res.header 'Pragma', 'no-cache' + res.header 'Expires', 0 + res.send 200, data setupFacebookCrossDomainCommunicationRoute = (app) -> app.get '/channel.html', (req, res) ->