How to improve speed of your mobile web application?


Creating a mobile web application is fairly simple – just scale a website for a smaller screen and it works flawlessly. But there is a problem – a mobile web application still feels and acts like a website, it is just smaller version of a desktop website. It is slow when displaying some kind of simple animations – for example slide toggle.

Nowadays it is fairly easy for developers to create a responsive website (e.g website, which scales itself automatically according to the screen size). Some people just use media queries , another use ready-made boilerplates like Bootstrap, Skeleton or Bones (for creating responsive WordPress template).

To get best experience from your mobile web/html application, using CSS3 transitions is the key. When usual transitions are just changing DOM and usage of hardware acceleration is minimal, CSS3 transitions are always using hardware/GPU acceleration and this gives a very big performance improvement and your application becomes very responsive.

To start using CSS3 transitions, we have to check if browser is capable of handling CSS3 transitions (most browsers do, except IE, which supports CSS 3 transitions starting from version 10.0). This is not a problem with mobile browsers – almost always they can handle CSS3 transitions without any problem.

Now we have to setup document to use CSS3 transitions.

Simple CSS3 accordion

Easiest way to use CSS3 transitions is to create an accordion. We will start by creating basic HTML structure.

Next step is to write CSS

Notice that last declaration (.css3-support) contains transitions. This declaration creates 0.5 second transition, which is applied to all parameters that have been changed (height currently).

Last step is to write JavaScript code, which is triggered when title is clicked.

You can check out my final demo here. If your browser is displaying animations, it is supporting CSS3 transitions.

Using CSS3 transforms and transitions in jQuery

By default jQuery does not support CSS3 transitions and it still relies on DOM manipulation when making animations. This approach works good for a desktop application, but mobile applications are suffering a big performance hit.

The easiest way to solve this problem and to add CSS3 transforms and transitions support to jQuery is to use excellent jQuery Animate-Enhanced plugin. Using this plugin is easy, because it changes how default animate method is working – it transforms default DOM animations into powerful CSS3 transitions.

This plugin is working for most of the situations, but sometimes you may encounter flickering when performing more complicated animations. Usually these problems are related to CSS backface-visibility and are easy to solve. If some transition does not work at all, it is very easy to fall back on legacy DOM animations.

How to give a html/web application “mobile application” look

If an application needs more “mobile” look, it is smart to use touch optimized framework, such as jQuery Mobile framework. It includes set of templates to make applications look like iOS or Android applications and also it supports custom CSS3 transitions. It is very good framework if you want to create an application, which has look and feel like a native application (headers, footers, buttons, sliders,transitions).


Use HTML5 application cache to add offline support

HTML5 specification provides an application caching mechanism, that lets applications to run offline. The basic concept behind application cache is to create a cache manifest file, which defines all resources, which should be available offline and which not.

Manifest file should be defined in <html> element

A manifest file can have three distinct sections: CACHE, NETWORK, and FALLBACK.

Files listed under the CACHE: section header are explicitly cached after they are downloaded for the first time.

Files listed under the NETWORK: section header in the cache manifest file are white-listed resources that require a connection to the server. All requests to such resources bypass the cache, even if the user is offline.

The FALLBACK: section specifies fallback pages the browser should use if a resource is inaccessible. Each entry in this section lists two URIs—the first is the resource, the second is the fallback.

Typical cache manifest file is looking like this:

PS. Cache manifest files must be served with the text/cache-manifest MIME type, otherwise you may run into problems.