css - sass缓存破坏

标签 css ruby caching sass

有没有办法在 SASS 中清除文件 rev 缓存?似乎可以根据此答案在此处使用指南针: SASS Image CSS Cache Busting (via Compass)

但我还没有找到任何方法来仅使用 SASS 来做到这一点。有没有办法让 SASS 从文件系统获取文件修改信息,并附加到图像路径?

而且我宁愿不附加查询字符串 - 相反,我认为这是一种更好的方法。

最佳答案

感谢 Dawn 这么久以来的插话!我已经弄明白了,但忘了我在这里发布了它。

我有一个自定义的 rb 文件,当我通过命令行运行 sass 时我会引用它 - 就像这样:

sass --update sass:css -r file_mod.rb

在 file_mod.rb 中,我有以下 ruby​​ 函数可以解决这个问题:

require 'sass'

module GETMODINT
def file_url(staticFilePath,staticHost,filePath)
    assert_type filePath, :String
    filePath = filePath.value #get string value of literal
    staticFilePath = staticFilePath.value 
    staticHost = staticHost.value 
    modtime = File.mtime(filePath).to_i
    #Sass::Script::Number.new(modtime)

    fileBaseName = File.basename filePath, '.*'
    fileDir = File.dirname(filePath).sub(staticFilePath,'')
    fileExt = File.extname(filePath)
    path = "url('#{staticHost}#{fileDir}/#{fileBaseName}.#{modtime}#{fileExt}')"
    return Sass::Script::String.new(path)
end
Sass::Script::Functions.declare :modInt, [:filePath]
end

module Sass::Script::Functions
    include GETMODINT
end

然后,在 sass mixin 中,我简单地引用 file_url 函数,将构建结果所需的参数传递给它:

@mixin backgroundImage($path, $position:0 0, $repeat:no-repeat) {
background: {
    image:file_url($staticFilePath,$staticHost,$path);
    position:$position;
    repeat:$repeat;
}   
} 

在我的例子中,我用它来构建一个 css 背景图像路径。应该很容易修改以适应其他目的。

关于css - sass缓存破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9336575/

相关文章:

html - 如何更改 HTML/CSS 中的文本透明度?

ruby - 初始化中的参数数量错误(给定 0,预期 1)

php - 用于单个服务器站点数据缓存的 Memcache 与 APC

silverlight - 浏览器如何处理 XAP 包的缓存?

html - 网站站点在 Dreamweaver 中看起来不错,但是

javascript - 将输入文本与字符串数组进行比较后,突出显示结果中的匹配词

ruby-on-rails - 如何使用私有(private)方法保持 DRY?

ruby-on-rails - ActiveRecord 迁移不填充 Postgres 物化 View

android - 在磁盘上使用 picasso 加载图像

html - 如何在数据库中的图像之上制作 CSS 叠加层