Javascript 从 Rails 资产管道中清除缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10004365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:26:36  来源:igfitidea点击:

Clear the cache from the Rails asset pipeline

javascriptruby-on-railscachingruby-on-rails-3.1asset-pipeline

提问by Aaron Gray

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?

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

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:

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

//= require jquery.autoresize.js 

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

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

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

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

回答by benzado

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

我假设我们正在谈论生产环境。

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.

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

It's possible that if you replaced jquery.autoresize.jswith 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:cleanfirst, forcing it to rebuild everything in the public/assetsdirectory from scratch.

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

回答by Dex

Also try rake assets:clobber. This will totally reset everything and delete all compiled assets. In addition, I often need to set the environment before pushing to production by going: RAILS_ENV=production rake assets:precompile.

也试试rake assets:clobber。这将完全重置所有内容并删除所有已编译的资产。另外,我经常需要在推送到生产之前设置环境:RAILS_ENV=production rake assets:precompile.

回答by Aaron Gray

Rails automatically clears the cache for an individual file every time that the contents are edited. To clear the cache for a single file, simply open the file, edit a line of code, and re-save it.Rails will clear the cache for that file, and the browser will load the new file the next time the page is loaded.

每次编辑内容时,Rails 都会自动清除单个文件的缓存。要清除单个文件的缓存,只需打开文件,编辑一行代码,然后重新保存即可。Rails 将清除该文件的缓存,浏览器将在下次加载页面时加载新文件。

The reason jquery.autoresize.js was using the old cached version of the file was because the old version was deleted and then the new version was copied and pasted with the same name into the same folder. Because the file itself was never edited, Rails continued to use the old file that was cached.

jquery.autoresize.js 使用文件的旧缓存版本的原因是因为旧版本被删除,然后新版本被复制并以相同的名称粘贴到同一文件夹中。由于文件本身从未被编辑过,Rails 继续使用缓存的旧文件。

This is because the asset pipeline uses fingerprinting for the cache.

这是因为资产管道对缓存使用指纹识别。

Fingerprinting is a technique that makes the name of a file dependent on the contents of the file. When the file contents change, the filename is also changed. For content that is static or infrequently changed, this provides an easy way to tell whether two versions of a file are identical, even across different servers or deployment dates.

When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere (whether at CDNs, at ISPs, in networking equipment, or in web browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change. This will cause the remote clients to request a new copy of the content. This is generally known as cache busting.

The technique that Rails uses for fingerprinting is to insert a hash of the content into the name, usually at the end. For example a CSS file global.css could be renamed with an MD5 digest of its contents:

指纹识别是一种使文件名依赖于文件内容的技术。当文件内容改变时,文件名也会改变。对于静态或很少更改的内容,这提供了一种简单的方法来判断文件的两个版本是否相同,即使跨不同的服务器或部署日期也是如此。

当文件名是唯一的并基于其内容时,可以设置 HTTP 标头以鼓励任何地方的缓存(无论是在 CDN、ISP、网络设备还是 Web 浏览器中)以保留自己的内容副本。当内容更新时,指纹会发生变化。这将导致远程客户端请求内容的新副本。这通常称为缓存破坏。

Rails 用于指纹识别的技术是将内容的哈希值插入到名称中,通常在末尾。例如,可以使用其内容的 MD5 摘要重命名 CSS 文件 global.css:

global-908e25f4bf641868d8683022a5b62f54.css

So, if you delete a file you're referencing in the manifest, and then copy in a new file with the same name, the cache busting never occurs. When you edit the file, the fingerprinting kicks in, and a new hash is generated for the file name. This busts the cache for that file.

因此,如果您删除清单中引用的文件,然后复制到同名的新文件中,则不会发生缓存破坏。当您编辑文件时,指纹识别开始,并为文件名生成一个新的散列。这会破坏该文件的缓存。

For the full story, see What is Fingerprinting and Why Should I Care?.

有关完整故事,请参阅什么是指纹识别以及我为什么要关心?.

回答by akz92

rake tmp:cleardid the trick for me, I'm using less-rails.

rake tmp:clear对我有用,我使用的是less-rails。

回答by user3670743

I use config.assets.version = '1.01019'in my application.rbto bust the entire cache. When I want to push a complete new version, I increment the version and that does the trick. This takes care of those edge cases where Rails does not recompile as asset for whatever reason.

我用config.assets.version = '1.01019'我的application.rb来破坏整个缓存。当我想推送一个完整的新版本时,我会增加版本,这就是诀窍。这处理了那些 Rails 出于某种原因没有重新编译为资产的边缘情况。