向 Rails 3.1 应用程序添加 HTML5 离线支持

发布于 2024-11-29 03:35:01 字数 660 浏览 3 评论 0原文

我想为我的 Rails 3.1 应用程序添加 HTML5 离线支持,并且我遇到了 rack-offline,适合此目的的宝石。但是,rack-offline 仅将 public 文件夹中的资源添加到应用程序缓存清单文件中。如何让它还添加我的 assets 文件夹中的所有已编译资源(资源管道生成的资源)?


具体来说,我的 routes.rb 文件中有以下内容:

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

与我拥有 Rails.public_path 一样,我可以获得已编译资源的路径吗?这样我就可以使用上面的代码将该路径中的文件添加到缓存清单中。

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)?


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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-12-06 03:35:01

首先,我相信这段代码应该放在初始化程序中,因为它只是配置:

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

要回答有关提供编译资产的问题,它们可以从浏览器访问,因此您需要做的就是手动提供缓存语句一切都应该正常。尝试使用这样的配置:

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

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
七分※倦醒 2024-12-06 03:35:01

我遇到了类似的问题,并编写了一个 gem 来解决资产 MD5 指纹问题。

https://rubygems.org/gems/assets_offline

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

https://rubygems.org/gems/assets_offline

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文