Sprockets 4: `url(relative.css)` issue

10+ years most with Ruby on Rails and JavaScript frontend and some Go. My first language is C for 3 years in university.
Search for a command to run...

10+ years most with Ruby on Rails and JavaScript frontend and some Go. My first language is C for 3 years in university.
No comments yet. Be the first to comment.
I recently updated from Slim template to ERB to use Herb (Thanks Marco). I had to update a test that was very strict on the format. I realized that Capybara::Node::Simple doesn’t provide a selector for regex. The page object doesn’t have a match? met...

It is as simple as: rustup completions zsh > ~/.zfunc/_rustup rustup completions zsh cargo > ~/.zfunc/_cargo With a TAB you get the auto-complete or the options available: ❯ cargo b b -- alias: build bench -- Execute all benchmarks of a local ...
Let’s not re-write a good article, go to: https://joyofrails.com/articles/mastering-custom-configuration-in-rails I mostly used config.x configuration that loads a yaml file with config_for that do the multi-environment by default: module YourApp c...
No need to re-write the wheel, let me redirect to this great article from Lily Reile. Obviously still up-to-date and really useful as the official Rails doc is mostly showing the value before and after. https://lilyreile.medium.com/rails-6-1-new-fram...
I came around to update Appsignal gem to the new major version of Appsignal released at the end of August 2024: https://rubygems.org/gems/appsignal/versions/4.0.0 The upgrade was smooth. The real change for my application was to remove the parameter ...
Disclaimer: sprockets in general is phased out in the Rails community, still some people need the fix ... I needed a fix today.
Issue: I have an old gem that reference an relative image in the CSS background-image: url("icon.png")) which once compile it become:
Sprockets 3: background-image: url("icon.png")) (it's a relative URL)
Sprockets 4: background-image: url("/icon.png")) (the / means root URL, eg http://example.org/icon.png)
With the fix below: background-image: url("./icon.png")) (./ is also a relative URL)
Notes about relative URL:
If the CSS file is http://example.com/application.css
icon.png is also at the root: http://example.com/icon.pngIf the CSS file is in a gem (for me it was CKeditor), the CSS file is http://example.com/assets/ckeditor/skins/moono-lisa/editor.css
icon.png is resolved as http://example.com/assets/ckeditor/skins/moono-lisa/icon.png# config/initializers/sprockets.rb
# Issue exists: https://github.com/rails/sprockets-rails/issues/507
# Fix from PR: https://github.com/rails/sprockets-rails/pull/511
# It will not get merge as Sprockets is phased out.
module Sprockets
module Rails
# Resolve assets referenced in CSS `url()` calls and replace them with the digested paths
class AssetUrlProcessor
REGEX = /url\(\s*["']?(?!(?:\#|data|http))(?<relativeToCurrentDir>\.\/)?(?<path>[^"'\s)]+)\s*["']?\)/
def self.call(input)
context = input[:environment].context_class.new(input)
data = input[:data].gsub(REGEX) do |_match|
path = Regexp.last_match[:path]
# Prevent non-digest relative path from being converted to root absolute path
orig_path = path
path = context.asset_path(path)
path = (path == "/#{orig_path}") ? ".#{path}" : path
"url(#{path})"
end
context.metadata.merge(data: data)
end
end
end
end
Caveat: the original file did not change therefore the digest did not either, until the browser invalid the the cache and download the new CSS, users will have the old CSS cached by the browser
Please share and comment on how I can write it better.