Module: Lash::AssetsHost

Defined in:
lib/lash/assets_host.rb

Class Method Summary collapse

Class Method Details

.asset_id(file) ⇒ Object

Generates an asset id for the given file used for cache-busting



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lash/assets_host.rb', line 22

def self.asset_id( file )
  if Lash.lash_options[:use_git_asset_id]
    repo = Grit::Repo.new( Rails.root.to_s )
    [:javascripts, :stylesheets, :images] \
      .map { |folder| repo.log( 'master', "public/#{folder}", :max_count => 1 ).first } \
      .max_by { |log| log && log.committed_date } 
      .id
  elsif ::File.exist?( file )
    File.mtime( file ).to_i.to_s
  end
end

.resolve_static_asset_server_for_source(source, request) ⇒ Object

Method used to map an asset to a static asset server. This method simply generates a semi-random domain prefix based on the filename of the source. The asset server should resolve to the same server as the rails app. This is a basic browser hack to allow more than 4 connections to the server so that the browser can download multiple assets simultaneously.

Examples:

request.host        # => lvh.me
resolve_static_asset_server_for_source "smiles", request
                    # => assets1.lvh.me      

# from terminal
nslookup lvh.me           # => 127.0.0.1
nslookup assets1.lvh.me   # => 127.0.0.1


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lash/assets_host.rb', line 48

def self.resolve_static_asset_server_for_source( source, request )
  if /\/\// =~ source
    nil
  elsif request.ssl? and ! Lash.lash_options[:use_asset_servers_in_ssl]
    nil
  elsif !Lash.lash_options[:use_asset_servers]
    nil
  else

    # Change the host name to include a randomized asset name at the same domain
    # level. This is required so that HTTPS requests can use a wildcard domain
    # without using subject alt name.

    host = request.host_with_port
    parts = host.split( /\./ )
    if parts.length > 2 
      parts[0] = "#{parts[0]}-assets#{source.hash % 4}"
    else
      parts.unshift "assets#{source.hash % 4}"
    end

    "http#{'s' if request.ssl?}://#{parts.join('.')}"
  end
end

.use_git_asset_idObject

Configures Rails to use the most recent GIT repo id for public/(javascripts|stylesheets|images) for the asset id used for cache busting.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lash/assets_host.rb', line 9

def self.use_git_asset_id
  return unless Lash.lash_options[:use_git_asset_id]

  repo = Grit::Repo.new( Rails.root.to_s )

  ENV['RAILS_ASSET_ID'] = \
  [:javascripts, :stylesheets, :images] \
    .map { |folder| repo.log( 'master', "public/#{folder}", :max_count => 1 ).first } \
    .max_by { |log| log && log.committed_date } 
    .id
end