Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

fixed - timestamp problem on changing url in css #6

@lancejpollard

Description

@lancejpollard

I use a sprite plugin that appends the timestamp to the url in css:

url(/images/logo.png?194819891)

So if you just add url = url.split("?")[0], to the following method, it works great!

def replace_css_images!(options={})
  options[:prefix] ||= ''
  # adapted from https://github.com/blakink/asset_id
  data.gsub! /url\((?:"([^"]*)"|'([^']*)'|([^)]*))\)/mi do |match|
    begin
      # $1 is the double quoted string, $2 is single quoted, $3 is no quotes
      uri = ($1 || $2 || $3).to_s.strip
      uri.gsub!(/^\.\.\//, '/')

      # if the uri appears to begin with a protocol then the asset isn't on the local filesystem
      if uri =~ /[a-z]+:\/\//i
        "url(#{uri})"
      else
        url = url.split("?")[0]
        asset = Asset.new(uri)
        # TODO: Check the referenced asset is in the asset_paths
        puts "  - Changing CSS URI #{uri} to #{options[:prefix]}#{asset.fingerprint}" if @@debug
        "url(#{options[:prefix]}#{asset.fingerprint})"
      end
    rescue Errno::ENOENT => e
      puts "  - Warning: #{uri} not found" if @@debug
      "url(#{uri})"
    end
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions