【问题标题】:sass cache bustingsass 缓存破坏
【发布时间】:2012-03-09 08:15:40
【问题描述】:

有没有办法在 SASS 中进行文件 rev 缓存破坏?根据这个答案,这里似乎可以使用指南针: SASS Image CSS Cache Busting (via Compass)

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

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

【问题讨论】:

  • 看起来这确实可以通过定义一个自定义的 sass 函数来实现:seancolombo.com/2010/07/28/… 但我对 Ruby 一点也不熟悉。我宁愿不必通过命令行指定自定义函数,而是继续使用 SASS watch 命令。
  • 避免查询字符串是对的——它们可以被忽略 (stevesouders.com/blog/2008/08/23/…)。如果您使用的是 ruby​​ 并且可能是 rails,那么您使用的是 v3 吗? Rails 资产管道为您完成所有工作,如果没有,那么 sprockets 可能会有所帮助。当你放置一个图像引用时,你使用 image-url() 而不是 url() 所以计算路径

标签: css ruby caching sass


【解决方案1】:

感谢 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 背景图像路径。应该很容易修改以适应其他用途。

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 2010-11-01
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多