Module: Cybertrain::Assets

Defined in:
cybertrain/assets.rb

Overview

The digested asset paths and the import map gen/assets.rb installs: read by the asset helpers and the static file server.

Defined Under Namespace

Classes: UnknownAsset

Class Method Summary collapse

Class Method Details

.build_digest ⇒ String

A digest gen writes into gen/assets.rb: of the digested assets and the import map, and in a binary cybertrain build made, of the views and components embedded in it too. It changes when a deploy changes what a page renders, so Controller#fresh_when mixes it into every ETag; "" until gen/assets.rb installs one.

Returns:

  • (String)


82
83
84
# File 'cybertrain/assets.rb', line 82

def self.build_digest
  @build_digest
end

.digest_path(logical) ⇒ String

Returns the digested path under /assets, “” for a name that is not an asset.

Examples:

Cybertrain::Assets.digest_path("logo.svg") # => "/assets/logo-1a2b3c4d.svg"

Parameters:

  • logical (String) —

    the path below an asset root

Returns:

  • (String) —

    the digested path under /assets, “” for a name that is not an asset



92
93
94
# File 'cybertrain/assets.rb', line 92

def self.digest_path(logical)
  @resolver.call(logical)
end

.importmap_json ⇒ String

Returns the import map of config/importmap.rb as compact JSON ({"imports":{...}}), </ written \u003c/ so it can sit inside a

Returns:

  • (String) —

    the import map of config/importmap.rb as compact JSON ({"imports":{...}}), </ written \u003c/ so it can sit inside a



210
211
212
# File 'cybertrain/assets.rb', line 210

def self.importmap_json
  @importmap_json
end

.install(resolver, importmap_json, preload_paths, build_digest = "") ⇒ nil

Installs gen/assets.rb’s tables; gen/assets.rb calls it when the application loads.

Parameters:

  • resolver (Proc) —

    logical path => digested path (“” when unknown), Gen::Assets.resolver; pass it positionally (spikes/NOTES.md rule 42)

  • importmap_json (String) —

    the import map, compact JSON

  • preload_paths (Array<String>) —

    the paths and URLs to preload

  • build_digest (String) (defaults to: "") —

    a digest of this build’s assets (and, in a cybertrain build binary, its embedded views), build_digest

Returns:

  • (nil)


67
68
69
70
71
72
73
# File 'cybertrain/assets.rb', line 67

def self.install(resolver, importmap_json, preload_paths, build_digest = "")
  @resolver = resolver
  @importmap_json = importmap_json
  @preload_paths = preload_paths
  @build_digest = build_digest
  nil
end

.kept_file?(file_name) ⇒ Boolean

Whether file_name (the path below /assets) carries a digest gen wrote: the current one of gen/assets.rb, or one .manifest keeps. Only such a file is served immutable (Static).

Examples:

Cybertrain::Assets.kept_file?("logo-1a2b3c4d.svg") # => true

Parameters:

  • file_name (String)

Returns:

  • (Boolean)


196
197
198
199
200
201
202
203
204
# File 'cybertrain/assets.rb', line 196

def self.kept_file?(file_name)
  name = file_name.to_s
  return true if @kept.key?(name)

  logical = AssetManifest.logical_of(name)
  return false if logical.empty?

  @resolver.call(logical) == "/assets/#{name}"
end

.load_kept(public_root) ⇒ nil

Reads public_root/assets/.manifest (written by spin run gen): the digests it keeps for each logical path, so that a page served before a deploy still fetches its files as immutable. Application#boot calls it once; a missing file keeps none. A malformed line is skipped whole, as gen skips it (AssetManifest.read_kept).

Parameters:

  • public_root (String)

Returns:

  • (nil)


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'cybertrain/assets.rb', line 158

def self.load_kept(public_root)
  kept = { "" => true }
  kept.delete("")
  file = "#{public_root}/assets/#{AssetManifest::FILE}"
  if File.file?(file)
    lines = AssetManifest.read_kept(File.read(file))
    lines.each_key do |logical|
      lines[logical].each { |digest| kept[AssetManifest.digested_name(logical, digest)] = true }
    end
  end
  @kept = kept
  nil
end

.path(logical) ⇒ String

The digested path of a logical one, what asset_path and the tag helpers write.

Examples:

Cybertrain::Assets.path("logo.svg") # => "/assets/logo-1a2b3c4d.svg"

Parameters:

  • logical (String) —

    the path below an asset root

Returns:

  • (String) —

    the digested path; for an unknown name in production /assets/<logical> (logged once per name)

Raises:

  • (UnknownAsset) —

    for an unknown name in development and test



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'cybertrain/assets.rb', line 105

def self.path(logical)
  name = logical.to_s
  digested = @resolver.call(name)
  return digested unless digested.empty?

  if @raise_on_unknown
    message = "asset \"#{name}\" is not in app/assets, app/javascript or vendor/javascript (run spin run gen)"
    raise UnknownAsset, message
  end

  # name.inspect: a name from a request cannot write a line of its own.
  log_unknown(name, "asset #{name.inspect} is not in app/assets, app/javascript or vendor/javascript (run spin run gen)")
  "/assets/#{name}"
end

.preload_paths ⇒ Array<String>

Returns the paths (and URLs) of the pins to preload, in config/importmap.rb’s order.

Returns:

  • (Array<String>) —

    the paths (and URLs) of the pins to preload, in config/importmap.rb’s order



217
218
219
# File 'cybertrain/assets.rb', line 217

def self.preload_paths
  @preload_paths
end

.raise_on_unknown ⇒ Boolean

Returns whether an unknown name raises.

Returns:

  • (Boolean) —

    whether an unknown name raises



146
147
148
# File 'cybertrain/assets.rb', line 146

def self.raise_on_unknown
  @raise_on_unknown
end

.raise_on_unknown=(value) ⇒ Object

Whether an unknown name raises (true, development and test) or is logged once and written undigested (false, production).

Parameters:

  • value (Boolean)


140
141
142
# File 'cybertrain/assets.rb', line 140

def self.raise_on_unknown=(value)
  @raise_on_unknown = value
end