Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created February 1, 2011 06:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adamloving/805499 to your computer and use it in GitHub Desktop.
Save adamloving/805499 to your computer and use it in GitHub Desktop.
Rails controller action for an HTML5 cache manifest file.
class InstallerController < ApplicationController
# Rails controller action for an HTML5 cache manifest file.
# Generates a plain text list of files that changes
# when one of the listed files change...
# So the client knows when to refresh its cache.
def cache_manifest
@files = ["CACHE MANIFEST\n"]
@files << 'favicon.ico'
@files << './client/sencha/ext-touch.js'
@files << './client/sencha/resources/css/apple.css'
add_from('./client/views/','*.html')
add_from('./client/javascripts/','*.js')
add_from('./client/stylesheets/','*.css')
add_from('./client/images/','*.png')
@files << "\nNETWORK:"
@files << '*'
digest = Digest::SHA1.new
@files.each do |f|
actual_file = File.join(Rails.root,'public',f)
digest << "##{File.mtime(actual_file)}" if File.exist?(actual_file)
end
@files << "\n# Modification Digest: #{digest.hexdigest}"
render :text => @files.join("\n"), :content_type => 'text/cache-manifest', :layout => nil
end
protected
def add_from(loc,match)
Dir.glob(File.join(Rails.root,'public',loc, match)) do |file|
@files << "#{loc}#{File.basename(file)}"
end
end
end
@overture8
Copy link

What is the add_from call?

@phaus
Copy link

phaus commented Mar 12, 2012

i ended with something like this:

def add_from(path, file_pattern)
    actual_file = File.join(Rails.root,'public',path+"/"+file_pattern)
    Dir[actual_file].each do |file|
      @files << "#{path}/#{File.basename(file)}"
    end
end

But i recently switched to a version using the assets manifest.yml

def add_from_asset_manifest
    manifest_file = File.join(Rails.root,'public','assets','manifest.yml')
    if FileTest.exist?(manifest_file)
      File.open(manifest_file).each do |l|
        if l.include? ":"
          @files << "assets/#{l.split(":").last().strip()}"
        end
      end      
    end
end  

@adamloving
Copy link
Author

Hey guys I suck for not replying. I like the yml approach as well. I added the original add_from call. @bsharpe gets credit for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment