1

I have a Rails 5 app that makes use of webpacker magic. It worked fine until I added a (S)CSS rule that references an image. Webpacker/webpack produces an absolute URL (with scheme and host) in the form http://packs/media/images/image.png. This obviously won't work. The stylesheet and javascript tags do have relative URLs without scheme and host, e.g. <link rel="stylesheet" media="screen" href="/packs/css/application-92fa133c.css">.

# config/webpacker.yml
default: &default
   # ...
   resolved_paths: ['app/assets']
   # ...
user@56f1d2e30dc8:/home/app/app# bin/rails c
Running via Spring preloader in process 5837
Loading development environment (Rails 5.2.4.1)
[1] pry(main)> Rails.configuration.action_controller.asset_host
=> nil

This is how I reference the image in the SCSS file:

background-image: url('~images/image.png');

Question: What can I do to make webpack translate ~images/image.png to a URL without scheme and host?

bovender
  • 1,838
  • 15
  • 31

0 Answers0