Chapter 10. Releasing an App into the Wild

"The Internet is a wild land with its own games, languages, and gestures through which we are starting to share common feelings."

– Ai Weiwei

In this chapter we will learn how to prepare a web application for release. First we will discuss how to compress and combine JavaScript files for faster download times. Then we will look at how to use the HTML5 Application Cache interface to make your applications available offline.

In this chapter we will learn:

  • How to combine and compress JavaScript files
  • How to create a command-line script to prepare an application for release
  • How to use the HTML5 Application Cache API to make a page and its resources available offline
  • How to create a cache manifest file to determine what resources get cached
  • How to determine when an application's cache has been updated

Combining and compressing JavaScript

In the past, the conventional wisdom among JavaScript developers was that you should write all of your code in one file, because downloading multiple script files causes a lot of unnecessary network traffic and slows down the load time. While reducing the number of files to download is indeed better, writing all of your code in one file is difficult to read and maintain. We don't write code like that in other languages, so why should we do it in JavaScript?

Fortunately there is a solution to this problem: the JavaScript compressor. A compressor takes all of the JavaScript source files for an application, combines them into one file, and compresses them by renaming local variables to the smallest name possible, removing white space and comments. We get all of the benefits of using multiple source code files for development, plus all of the benefits of a single JavaScript file when releasing an application. You can think of it as compiling your source code into a compact executable package.

There are a number of JavaScript compressors available out there. You can find many of them online. The problem with those is that you have to copy your source code and paste it into a web form, then copy it back out into a file. That doesn't work too well for large applications. I suggest you use one of the compression applications that can be run from a command prompt, such as Yahoo's YUI Compressor or Google's Closure Compiler:

YUI and Closure are both easy to use and work extremely well. They both provide warnings about bad code (but not the same warnings). Both are written in Java and therefore require that you have the Java Runtime installed. I can't say one is better than the other. The only reason I would choose YUI would be if I also want to compress CSS, as Closure doesn't support it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.31.159