Rails资产在开发中加载两次;造成奇怪的行为

时间:2023-01-16 16:01:38

I'm working on a rails app and am seeing strange behavior that I believe is caused by rails assets loading multiple times in development.

我正在开发一个rails应用程序并且看到奇怪的行为,我认为是由于rails资产在开发中多次加载而导致的。

I'm working in Chrome, and had the Developer Tools -> Network -> "disable cache" checkbox marked, which made js and css assets load only once and made everything work as expected/desired.

我在Chrome中工作,并且标记了开发人员工具 - >网络 - >“禁用缓存”复选框,这使得js和css资产只加载一次,并使一切按预期/期望工作。

When I unchecked the "disable cache" box, however, assets started to load multiple times, and typeahead breaks, throwing: Uncaught TypeError: $(...).typeahead is not a function.

但是,当我取消选中“禁用缓存”框时,资产开始加载多次,并且输出一次中断,抛出:未捕获的TypeError:$(...)。typeahead不是一个函数。

Steps to reproduce:

重现步骤:

git clone https://github.com/duhaime/chalkboard.git
cd chalkboard
rake sunspot:solr:start
rake db:drop db:create db:migrate db:seed
rake sunspot:solr:reindex
rails s
# visit localhost:3000 and enable/disable cache (then refresh) in Chrome

I believe it's possible to resolve this error by disabling caching from within Rails (as opposed to the browser client), but I wanted to ask if there is a proper way to resolve this situation without disabling caching. I've read through many of the other SO threads on this question, but haven't found a workable solution. I would be very grateful for any advice others can provide on this question!

我相信可以通过禁用Rails中的缓存(而不是浏览器客户端)来解决此错误,但我想询问是否有正确的方法来解决这种情况而不禁用缓存。我已经阅读了这个问题的许多其他SO线程,但还没有找到可行的解决方案。我将非常感谢其他人可以就此问题提供的任何建议!

1 个解决方案

#1


0  

The reason typeahead was failing was because I was importing it via app/assets/javascripts/application.js file:

typeahead失败的原因是因为我通过app / assets / javascripts / application.js文件导入它:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require twitter/typeahead
//= require_tree .

and I was using <%= javascript_include_tag 'typeahead' %>. To make matters worse, the typeahead include calls was importing typeahead.bundle.js version 0.11.1, and I had gem installed twitter-bootstrap-rails version 0.10.5.

我正在使用<%= javascript_include_tag'typeahead'%>。更糟糕的是,typeahead包括调用导入typeahead.bundle.js版本0.11.1,并且我安装了gem-bootstrap-rails版本0.10.5。

I removed all of the <%= javascript_include_tag '{package}' %>, removed the typeahead.bundle.js file from my /assets directory, and the $(...).typeahead is not a function error has gone away. I'll leave this question here in case it will help others.

我删除了所有<%= javascript_include_tag'{package}'%>,从我的/ assets目录中删除了typeahead.bundle.js文件,而$(...)。typeahead不是函数错误已经消失。我会在这里留下这个问题,万一它会帮助别人。

#1


0  

The reason typeahead was failing was because I was importing it via app/assets/javascripts/application.js file:

typeahead失败的原因是因为我通过app / assets / javascripts / application.js文件导入它:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require twitter/typeahead
//= require_tree .

and I was using <%= javascript_include_tag 'typeahead' %>. To make matters worse, the typeahead include calls was importing typeahead.bundle.js version 0.11.1, and I had gem installed twitter-bootstrap-rails version 0.10.5.

我正在使用<%= javascript_include_tag'typeahead'%>。更糟糕的是,typeahead包括调用导入typeahead.bundle.js版本0.11.1,并且我安装了gem-bootstrap-rails版本0.10.5。

I removed all of the <%= javascript_include_tag '{package}' %>, removed the typeahead.bundle.js file from my /assets directory, and the $(...).typeahead is not a function error has gone away. I'll leave this question here in case it will help others.

我删除了所有<%= javascript_include_tag'{package}'%>,从我的/ assets目录中删除了typeahead.bundle.js文件,而$(...)。typeahead不是函数错误已经消失。我会在这里留下这个问题,万一它会帮助别人。