7

I'm getting ready to start on a new project and I'd like to know if there's a way to automatically minify JavaScript on the server side, providing caching once the JavaScript has been minified once already? I could simply write a build script to accomplish this, but it'd be nice if I could "fire-and-forget" so to speak, with automatic minification. What would be the recommended route in this scenario? Should I minify my JavaScript before it goes online at the cost of time or should I look for something that'll automatically do it for me on the serverside?

EDIT

I'll probably be using Django, but of course, JavaScript and media is served separately from the actual "HTML"/application output.

3
  • 1
    This really depends on what server-sided framework you're using; you should probably specify. Commented Jul 14, 2011 at 1:23
  • 1
    Something automated. Otherwise it would be madness. Commented Jul 14, 2011 at 1:26
  • 1
    Put it in the build process. code.google.com/closure/compiler Commented Jul 14, 2011 at 1:35

3 Answers 3

7

This will all depend on what server side language you're using. It will have nothing (so much) to do with Javascript or Css, but rather PHP, .NET, Ruby, etc.

(note: these are just some quick searches I've done. I don't have any experience with any of them.)

I personally recommend NOT doing ANY of the minification / combining at run time since there is a server side performance hit that happens over and over and over again.

Above is my "Answer"


Below is my "Preference"

My preference is to use some sort of a Build Time minification / combining tool that achieves the goal once and leaves the server to just "serve".

Since I'm running Visual Studio for my IDE, my preference is to use a tool called Chirpy... the end result is a single static site.min.js file and a single static site.min.css file that has everything I need. I will no longer suffer a performance hit from combining/minifying my js/css at run time.


Edit

Be sure to read the comments below, as they help add to the overall concept of what the OP is looking for.


Edit

Also just found WebPutty, looks like a pretty slick tool.

Sign up to request clarification or add additional context in comments.

8 Comments

Definitely. I didn't know that there was something available like Chirpy to roll it all into one, so that's really cool. Hopefully if I roll something like YUI and mod_deflate or mod_gzip I'll get the best performance and file size.
However, are there any frameworks that minify and cache JavaScript? (ie: first request causes the code to be minified and saved to a cache file, second request and on just pulls the cached file)
The best way to do the caching is to have a separate domain name, and have your web server do the caching. IIS and Apache both have ways of doing this. If you look at the StackOverflow source, you'll see that all static files come from http://cdn.sstatic.net/ - It used to NOT be a cdn... and they were still able to get good cache performance out of http://sstatic.net.
just remember that the static domain has to be "Cookieless" which means it canNOT simply be a subdomain off of your existing domain.
As an extra tid bit of info, be sure to SPRITE your images to increase performance there too.
|
1

YUI Compressor works amazingly for me, requires Java on the server and you passing it the right params:

Here is my build script I use (written in Ruby, but does not have to), it calls the compressor with the right params, thats all it does.

#
# Build JS Files
#
require 'rake/packagetask'

JAVA = 'java'
OUTPUT = 'public/js/sitel-core.js'
MINOUTPUT = 'public/js/sitel-core.min.js'
OUTCSS = 'public/css/styles.css'
MINCSS = 'public/css/styles.min.css'
WIZOUT = 'public/js/sitel-wizard.js'
WIZMIN = 'public/js/sitel-wizard.min.js'

desc "Build CSS Scripts"
task :buildcss do
    files = 'public/css/styles.css'
    `#{JAVA} -jar build/yuicompressor.jar #{OUTCSS} -o #{MINCSS}`
end

desc "Build JS Scripts"
task :build => [:buildcss] do
    files = 'public/js/sitel.js public/js/sitel/popup.js public/js/microtable.js public/js/microbox.js public/js/sitel/popup.page.js public/js/sitel/flexigrid.js public/js/flexigrid/flexigrid.js public/js/nyroModal-1.6.2/js/jquery.nyroModal-1.6.2.js public/js/help-bubble.js public/js/sitel/elgg.js'
    `cat #{files} > #{OUTPUT}`
    `#{JAVA} -jar build/yuicompressor.jar #{OUTPUT} -o #{MINOUTPUT}`    

    wizard = 'public/js/wizard2.js public/js/dual-select.js public/js/filterbox.js public/js/jquery-validate/jquery.validate.js public/js/smartsearch.js public/js/smartsearch-validator.js public/js/flexigrid/flexigrid_helper.js '
    `cat #{wizard} > #{WIZOUT}`
    `#{JAVA} -jar build/yuicompressor.jar #{WIZOUT} -o #{WIZMIN}`
end

desc "Build JS Scripts"
task :default => :build

1 Comment

YUI is the "standard" compressor that I think most other apps run on. I know that Chirpy uses YUI beneath the sheets.
0

If you use Apache you can use this solution that serve minified js files on the fly : https://www.unixmen.com/how-to-auto-minify-javascript-files-on-page-load-in-apache-using-uglifyjs/

1 Comment

There is also an nginx module for it as well: github.com/nginx-modules/nginx-minify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.