开发者

Adding HTML5 offline support to a Rails 3.1 app

开发者 https://www.devze.com 2023-03-27 12:10 出处:网络
I want to add HTML5 offline support for my Rails 3.1 app, and I\'ve come across rack-offline, a gem suited for this purpose. However, rack-offline only adds the assets in the public folder to the appl

I want to add HTML5 offline support for my Rails 3.1 app, and I've come across rack-offline, a gem suited for this purpose. However, rack-offline only adds the assets in the public folder to the application cache manifest file. How do I have it also add all the compiled assets from my assets folder (the ones that the asset pipeline generates)?


开发者_JAVA技巧

Specifically, I have the following in my routes.rb file:

offline = Rack::Offline.configure do
  cache "images/masthead.png"

  public_path = Rails.public_path
  Dir[public_path.join("javascripts/*.js")].each do |file|
    cache file.relative_path_from(public_path)
  end

  network "/"
end

The same way that I have the Rails.public_path, can I get a path to the compiled assets? That way I can use the above code to add the files in that path to the cache manifest.


Well first things first, I believe this bit of code should be place inside an initializer since it is just configuration:

Rack::Offline.configure do
  cache "images/masthead.png"

  public_path = Rails.public_path
  Dir[public_path.join("javascripts/*.js")].each do |file|
    cache file.relative_path_from(public_path)
  end

  network "/"
end

To answer your question about serving your compiled assets they are accessible from the browser so all you need to do is provide a cache statement manually and things should work. Try using a configuration like this:

Rack::Offline.configure do
  cache "assets/application.js"
  cache "assets/application.css"   
  network "/"
end


I had a similar issue and wrote a gem to solve the assets MD5 fingerprint problem.

https://rubygems.org/gems/assets_offline

0

精彩评论

暂无评论...
验证码 换一张
取 消