Skip to content

Instantly share code, notes, and snippets.

@slightlyoff
Created February 10, 2013 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slightlyoff/4750136 to your computer and use it in GitHub Desktop.
Save slightlyoff/4750136 to your computer and use it in GitHub Desktop.
Upgrading
// ctrl.js
// origin: http://example.com
// scope: "/*"
"use strict";
// Called when this controller is installed as the newest version
// for a scope. Perhaps a clearer name for what is now sketched
// as "onupdate". Note that this is called BEFORE "onupgrade" is
// sent to a previous version's controller.
this.oninstall = function(e) {
// We start with empty caches for each version of each controller.
assert(!this.caches.get("main"));
var main = new Cache([
// A resource used in v1. As an optimization, the browser
//can keep the copy it downloaded from v1 and avoid the network
// hit, assuming it hasn't expired in the HTTP cache sense.
"http://cdn.com/js/lib.js",
// A resource that's new to v2. This will be fetched now.
"/app/2/app.js",
// ...
]);
this.caches.set("main", main);
};
// Called when the controller is replaced by a newer
// version, but after the new version's oninstall has returned.
this.onupgrade = function(e) {
// We could free resources here, but in most cases won't need
// to. Instead, when the last live page controlled by our
// replaced controller is unloaded, the browser can remove
// all caches "owned" by the now-removed controller. Assuming
// that v3 also defines a "main", its "oninstall" will not be
// able to see the "main" cache associated with v2. It can,
// however, define a new one and, thanks to the overlapping
// resources optimization outlined above, avoid re-fetching most
// of the resources it needs.
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment