Rails 3.1 Asset Pipeline-为什么我应该在SCSS文件中使用Asset Helpers? [英] Rails 3.1 Asset Pipeline - Why should I use the Asset Helpers in a SCSS file?

查看:66
本文介绍了Rails 3.1 Asset Pipeline-为什么我应该在SCSS文件中使用Asset Helpers?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚进入资产管道;我正在使用SASS/SCSS,但是我不明白为什么我应该使用资产助手.

I'm just getting into the Asset Pipeline; I'm using SASS/SCSS, but I'm not understanding why I should be using the Asset Helpers.

例如,如果我有一些CSS/SCSS而不使用Asset Helper:

For example, if I have some CSS/SCSS without using an Asset Helper:

background-image: url('rails.png');

这将很好用,因为我的.SCSS文件和图像都在资产目录中并且可以通过资产目录访问.

This will work fine because both my .SCSS file and image are in and accessible through the assets directory.

这样做有什么意义?

background-image: asset-url("rails.png", image);

我知道它将在网址中添加"/assets/",但是如果标准CSS方式可行,为什么还要使用Asset Helpers?

I understand it will add "/assets/" to the url, but why should I be using the Asset Helpers if the standard CSS way will work?

我想我缺少了一些东西.它与部署到生产有关吗?

I think I'm missing something. Does it have something to do with deploying to production?

推荐答案

使用助手可让您访问生产环境中的指纹URL.从资产管道指南:

Using the helpers gives you access to the finger printed URLs in production. From the Asset Pipeline guide:

在生产环境中,Rails使用上面概述的指纹方案.默认情况下,假定资产已经过预编译,并且将由您的Web服务器用作静态资产.

In the production environment Rails uses the fingerprinting scheme outlined above. By default it is assumed that assets have been precompiled and will be served as static assets by your web server.

在预编译阶段,将根据已编译文件的内容生成MD5,并在将其写入磁盘时将其插入文件名中.这些指纹名称由Rails助手使用,而不是清单名称.

During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Rails helpers in place of the manifest name.

因此,在生产环境中,路径中附加了MD5,您将具有以下内容:

So in production, the paths have an MD5 appended and you have things like this:

/assets/pancakes-af27b6a414e6da00003503148be9b409.png

有了校验和,Rails可以告诉浏览器永远缓存这些文件.然后,如果您执行的新版本更改了其中一项资产,则校验和将更改,并且整个路径也会更改;新路径使浏览器认为它是一个全新文件,因此它将再次获取它.没有校验和,您很容易将旧文件卡在浏览器缓存中,而这种事情并不是一个愉快的时光.

With the checksums in place, Rails can tell browsers to cache these files forever. Then, if you do a new release that changes one of your assets, the checksum changes and that changes the whole path; the new path makes the browser think it is a whole new file so it will fetch it again. Without the checksums you can easily get old files stuck in browser caches and that sort of thing isn't exactly a happy fun time.

这篇关于Rails 3.1 Asset Pipeline-为什么我应该在SCSS文件中使用Asset Helpers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆