One of the modules that makes up Google Gears is the LocalServer that allows you to capture web resources so they can be served up when a user is offline.
The ManagedResourceStore component allows you to declare which resources you want the LocalServer to capture for you. You use a simple JSON manifest file that you can maintain with any text editor.
However, if your application gets large (lots of resources) you may not want to manage it in this way, so I created an open source Ruby library that allows me to generate this file given some rules:
For example, this will generate the manifest as a json
string, and create entries using the current directory and sucking in everything apart from files starting with a ‘.’.
json = Google::Gears::LocalServer::Manifest.new do |m| m.version = 'MyNewVer' m.add_entry({ :url => 'main.html', :src => 'foo.html' }) m.add_extra_info :to => 'main.html', :redirect => 'foo_redirect.html' m.find_entries :in => '.', :ignore => Google::Gears::LocalServer::Manifest::LEADING_PERIOD end
find_entries
is the real meat here:
# Defaults to '.' find_entries :ignore => Google::Gears::LocalServer::Manifest::LEADING_PERIOD # Only capture HTML pages find_entries :include => '\.html' # Look in the resources subdirectory, but use the URL 'static'. This is useful if your directory on disk doesn't match your URLs find_entries :in => 'resources', :root => 'static'
Once you have created the entries, you may want to add some metadata to a few of them. This is where add_extra_info
comes in:
# Find the 'main.html' entry and add a redirect. You can also use a different :src or :ignore_query. add_extra_info :to => 'main.html', :redirect => 'foo_redirect.html'
You can also create a manifest object (instead of using the block version) if you need to add a few things, get the output, add a few more, etc etc.
If you find yourself not wanting to manage your Manifest file, check out the project.
June 29th, 2007 at 1:51 pm
…very cool… makes me want to wrestle away more time to get my hands dirty with Gears. Thanks for the info.