本文介绍了从 Rails 资产管道中清除缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Rails 中启动一个新项目,看起来 application.js 清单文件对我引用的 javascript 做了一些有趣的事情 - 它是否将这些文件缓存为资产管道的一部分?

I'm starting a new project in Rails, and it looks like the application.js manifest file is doing something funny with the javascripts that I reference - does it cache those files as part of the asset pipeline?

这就是发生的事情.我在 vendor/assets/javascripts 文件夹中添加了一个名为 jquery.autoresize.js 的 javascript 文件,然后在 application.js 清单中引用该文件,如下所示:

Here's what happened. I added a javascript file named jquery.autoresize.js to the vendor/assets/javascripts folder, and then referenced the file in the application.js manifest like this:

//= require jquery.autoresize.js

然后我启动了 rails 服务器.但是在我的应用程序中导航后,我意识到我不小心添加了错误版本的 jquery.autoresize.js 文件.因此,我删除了该文件,然后将正确的版本添加到 vendor/assets/javascripts 文件夹中.但是,令我震惊的是,当我重新加载页面时,它仍在加载旧的 javascript 文件.

Then I started up the rails server. But after navigating around in my app, I realized that I had accidentally added the wrong version of the jquery.autoresize.js file. So, I deleted that file and then added the correct version to the vendor/assets/javascripts folder. But, to my horror, when I reloaded the page, it is still loading the old javascript file.

我尝试清空浏览器缓存,然后退出并重新启动 Rails 服务器,但无济于事.我通过简单地重命名我的 javascript 文件并引用新名称来一起破解一个解决方案,这很好用.但是必须有一个更好的解决方案.

I tried emptying my browser cache, then exiting and restarting the Rails server, but to no avail. I hacked a solution together by simply renaming my javascript file and referencing the new name, which worked fine. But there has got to be a better solution to this.

新的资产管道是否以某种方式缓存您引用的文件?如果是这样,我该如何清除该缓存?感谢您的帮助!

Does the new asset pipeline cache the files you reference somehow? If so, how can I clear that cache? Thanks for any help!

推荐答案

我假设我们在谈论生产环境.

I'm assuming we're talking about the production environment.

当您在生产环境中更改任何 javascript 或样式表时,您需要运行 rake assets:precompile;此任务编译和压缩各种 .js 和 .css 文件,并创建由您的视图加载的 application.js 和 application.css 文件.

When you change any of your javascripts or stylesheets in the production environment, you need to run rake assets:precompile; this task compiles and compresses the various .js and .css files and creates the application.js and application.css files that is loaded by your views.

如果您将 jquery.autoresize.js 替换为带有 较旧时间戳 的版本,则预编译步骤可能会跳过它,认为编译版本已更新 -迄今为止.您可以通过首先运行 rake assets:clean 来避免这种情况,强制它从头开始重建 public/assets 目录中的所有内容.

It's possible that if you replaced jquery.autoresize.js with a version with an older timestamp, the precompile step might skip it, thinking the compiled version is up-to-date. You can avoid that by running rake assets:clean first, forcing it to rebuild everything in the public/assets directory from scratch.

这篇关于从 Rails 资产管道中清除缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:02