7

I want to do the following

if build?
    assetPath = "//cdn.domain.com/assets"
else
    assetPath = "assets"
end

trying all combinations and reading everywhere but simply stumped at the moment

ruby and middleman - still learning.

Ian Warner
  • 1,058
  • 13
  • 32

2 Answers2

8

Are you trying this within the config.rb? If not, you should do so.

There already is a setting that you might want to use ...

set :css_dir, 'assets'

... and change on build:

configure :build do
  set :css_dir, '//cdn.domain.com/assets'
end

Are you aware of the Asset helpers? You can use ...

<%= stylesheet_link_tag 'foo.css' %>

... within your (ERB) templates.

While developing that should give you ...

<link href="/assets/foo.css" media="screen" rel="stylesheet" type="text/css" />

... and within your build:

<link href="//cdn.domain.com/assets/foo.css" media="screen" rel="stylesheet" type="text/css" />
jottr
  • 3,256
  • 3
  • 29
  • 35
Volker Rose
  • 1,808
  • 15
  • 16
  • Hi I am sure I tried all of this - maybe some other issues with the setup is colliding - I will try again thanks – Ian Warner Jul 05 '13 at 06:32
  • 1
    I have been looking for a solution involving http_prefix for CDN usage, finding nothing but a lot of frustration! This solution worked great for me, thanks. – roguenet Jul 08 '13 at 17:13
  • This solution does not work when combined with the asset_hash option. You must use the asset_host option to set a cdn url as the other answer suggests. – Alex Sharp Oct 03 '15 at 20:19
1

I actually had problems with the early answer. To actually change the asset path to work with CDNs such as CloudFront, I had to do the following:

# Fingerprint assets
activate :asset_hash

# Enable Asset Hosts
activate :asset_host

set :asset_host do |asset|
  '//d23xxk856.cloudfront.net'.to_s
end
  • Do you really need that .to_s ? Seems redundant as you're turning a string into a string. – reid May 22 '15 at 21:15
  • 1
    For what it's worth, this syntax is no longer correct. https://middlemanapp.com/basics/build_and_deploy – coreyward May 26 '16 at 23:10