Search code examples
ruby-on-railshtmlruby-on-rails-4assetshtml5-appcache

Rails, assets, appcache dynamic names


When declaring what files to be cached in appcache manifest is there any way of including files that comes with dynamic names?

Such as the assets that are being compiled by rails.

assets/application-12315123213123151asd.js

In this case I would like to do something like this,

MANIFEST

CACHE
/assets/*.js

or

MANIFEST

CACHE
/assets/application-*.js

Solution

  • I made my own rake task instead of using gems such as rack-offline which doesn't seem to be updated in a long time.

    #encoding: utf-8
    desc "Create html5 appcache manifest"
    task :html5_manifest => :environment do
        File.open("public/offline.appcache", "w") do |f|
            f.write("CACHE MANIFEST\n")
            f.write("# #{Time.now.to_i}\n")
            assets = Dir.glob(File.join(Rails.root, 'public/assets/**/*'))
            assets.each do |file|
                if File.extname(file) != '.gz'
                    f.write("assets/#{File.basename(file)}\n")
                end
            end
            # f.write("NETWORK\n")
            # f.write("*\n")
            # f.write("FALLBACK:\n")
            # f.write("...")
        end
    end
    

    Put this as a task in your cap file when deploying to a server