Permalink
Browse files

`@use 'zappa'`: Added `/zappa/Zappa.js` combined download option.

  • Loading branch information...
shimaore committed Aug 26, 2012
1 parent 5b9539b commit e8f913bd149632fde3c311f408430cc75d551bb9
Showing with 33 additions and 6 deletions.
  1. +4 −2 Cakefile
  2. +3 −1 docs/reference.md
  3. +2 −1 package.json
  4. +12 −2 src/zappa.coffee
  5. +12 −0 tests/assets.coffee
View
@@ -16,8 +16,10 @@ task 'docs', ->
task 'vendor', ->
run 'mkdir -p vendor && cd vendor && curl -OL http://code.jquery.com/jquery-1.8.0.min.js', ->
run 'cd vendor && curl -OL https://github.com/quirkey/sammy/raw/master/lib/min/sammy-0.7.1.min.js', ->
- run 'head -n 1 vendor/jquery*', ->
- run 'head -n 3 vendor/sammy*'
+ run 'cd vendor && curl -OL https://raw.github.com/LearnBoost/socket.io-client/master/dist/socket.io.min.js', ->
+ run 'head -n 1 vendor/jquery*', ->
+ run 'head -n 3 vendor/sammy*', ->
+ run 'head -n 1 vendor/socket.io*'
task 'setup', 'build + vendor', ->
invoke 'build'
View
@@ -408,7 +408,9 @@ Same as `@express.static(root + '/public')`, where `root` is the directory of th
#### 'zappa'
-Serves `/zappa/zappa.js`, `/zappa/jquery.js` and `/zappa/sammy.js`. Automatically added by `@client` and `@shared` if not added before.
+Serves `/zappa/Zappa.js`, `/zappa/zappa.js`, `/zappa/jquery.js` and `/zappa/sammy.js`. Automatically added by `@client` and `@shared` if not added before.
+
+To minimize page download delay on the client, use `/zappa/Zappa.js`, which combines Zappa, jQuery, Sammy.js, and Socket.IO client-side scripts into a single download.
### @configure
View
@@ -34,7 +34,8 @@
"lib/zappa.js",
"lib/client.js",
"vendor/jquery-1.8.0.min.js",
- "vendor/sammy-0.7.1.min.js"
+ "vendor/sammy-0.7.1.min.js",
+ "vendor/socket.io.min.js"
],
"contributors": [
"Maurice Machado <maurice@bitbending.com>",
View
@@ -10,8 +10,11 @@ log = console.log
fs = require 'fs'
path = require 'path'
uuid = require 'node-uuid'
-jquery = fs.readFileSync(__dirname + '/../vendor/jquery-1.8.0.min.js').toString()
-sammy = fs.readFileSync(__dirname + '/../vendor/sammy-0.7.1.min.js').toString()
+vendor = (name) ->
+ fs.readFileSync(path.join(__dirname,'..','vendor',name)).toString()
+jquery = vendor 'jquery-1.8.0.min.js'
+sammy = vendor 'sammy-0.7.1.min.js'
+socketjs = vendor 'socket.io.min.js'
uglify = require 'uglify-js'
socketio_key = '__session'
@@ -118,6 +121,7 @@ zappa.app = (func,options={}) ->
# Reference to the zappa client, the value will be set later.
client = null
+ client_bundled = null
# Tracks if the zappa middleware is already mounted (`@use 'zappa'`).
zappa_used = no
@@ -231,6 +235,7 @@ zappa.app = (func,options={}) ->
if req.method.toUpperCase() isnt 'GET' then next()
else
switch req.url
+ when '/zappa/Zappa.js' then send client_bundled
when '/zappa/zappa.js' then send client
when '/zappa/jquery.js' then send jquery
when '/zappa/sammy.js' then send sammy
@@ -489,6 +494,11 @@ zappa.app = (func,options={}) ->
client = require('./client').build(zappa.version, app.settings)
client = ";#{coffeescript_helpers}(#{client})();"
client = minify(client) if app.settings['minify']
+ client_bundled =
+ if io?
+ jquery + socketjs + sammy + client
+ else
+ jquery + sammy + client
if app.settings['default layout']
context.view layout: ->
View
@@ -209,6 +209,18 @@ port = 15200
t.equal 'content-type', res.headers['content-type'], 'application/javascript'
t.ok 'snippet', res.body.indexOf('window.zappa = {};') > -1
+ Zappa: (t) ->
+ t.expect 'content-type', 'snippet'
+ t.wait 3000
+
+ zapp = zappa port++, ->
+ @use 'zappa'
+
+ c = t.client(zapp.server)
+ c.get '/zappa/Zappa.js', (err, res) ->
+ t.equal 'content-type', res.headers['content-type'], 'application/javascript'
+ t.ok 'snippet', res.body.indexOf('window.zappa = {};') > -1
+
'zappa (automatic)': (t) ->
t.expect 'content-type', 'snippet'
t.wait 3000

0 comments on commit e8f913b

Please sign in to comment.