提供没有时间戳的Javascript文件,并与application.js分开

时间:2014-05-30 03:58:45

标签: ruby-on-rails heroku asset-pipeline

我想在没有时间戳的生产(heroku)中提供特定的js文件。 我能够单独提供文件,但它提供了我不想要的时间戳。原因是我希望其他网站访问此文件。

以下是代码:

application.html.erb

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'ca4me', 'data-turbolinks-track' => true, :cache => false %>

的application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-slider
//= require bootstrap.min

application.rb中

config.assets.precompile += %w( ca4me.js )

生产环境中的HTML源代码:

<script data-turbolinks-track="true" src="/assets/application-b83af88604eb0fb1867384db77b826ae.js"></script>
<script cache="false" data-turbolinks-track="true" src="/assets/ca4me-fcff49d8b1799052a3f84c913160f6b2.js"></script>

那么我可以在没有时间戳的情况下提供ca4me.js吗?

1 个答案:

答案 0 :(得分:1)

文件

  

原因是我希望其他网站访问此文件。

我们这样做了:

/public/your_js_file.js

这是一个很难保持更新的痛苦,但这意味着您的应用始终会在/____.js处提供该文件。实际上,当您使用资产预编译时,资产会部署到public目录中的文件夹,如下所示:

- public
 |-stylesheets
 |-javascripts
 |-images

如果您希望将文件保留给其他网站(例如小部件或其他内容),我会亲自将代码库与您的Rails应用程序分开,使用Grunt或类似文件(将文件保存到直接rails_app/public


<强>摘要

我想了解这一点,我found this

config.assets.digest_exclusions << /fontawesome/

唯一的问题是这是digestion gem - 意味着它不会在Rails 4中提供。我仍然试图找到有关如何在Rails 4中完成此操作的信息,我建议现在查看我的解决方案

-

就个人而言,我会依赖资产管道作为我的资产,如果我想要一个静态的位置供人们访问,请将文件保存在public。可能是文件(库?)将与Rails应用程序一起更新,尽管是唯一的;意思是你可能希望保持代码库分开