Module: Assiette::Helpers

Defined in:
lib/assiette/helpers.rb

Instance Method Summary collapse

Instance Method Details

#assiette_asset_integrity(path) ⇒ Object

Returns the SRI integrity hash for an asset, computed over the served (rewritten) content. Returns nil if the file is not found.



15
16
17
18
19
# File 'lib/assiette/helpers.rb', line 15

def assiette_asset_integrity(path)
  entry = request.env["assiette.stack"]&.last
  raise "No Assiette::Server in middleware stack" unless entry
  entry[:server].asset_integrity(path)
end

#assiette_asset_path(path) ⇒ Object

Returns the URL path to an asset served by Assiette, with a cache-busting version tag appended.



7
8
9
10
11
# File 'lib/assiette/helpers.rb', line 7

def assiette_asset_path(path)
  entry = request.env["assiette.stack"]&.last
  raise "No Assiette::Server in middleware stack" unless entry
  entry[:server].absolute_asset_url_path(path, entry[:script_name])
end

#assiette_modulepreload_tagsObject

Generates <link rel=“modulepreload”> tags for all detected ES modules under the configured asset roots. Each tag includes an SRI integrity hash computed over the served (rewritten) content.



30
31
32
33
34
35
36
37
38
# File 'lib/assiette/helpers.rb', line 30

def assiette_modulepreload_tags
  entry = request.env["assiette.stack"]&.last
  raise "No Assiette::Server in middleware stack" unless entry
  modules = entry[:server].js_modules
  safe_join(modules.map { |mod|
    tag.link(rel: "modulepreload", href: assiette_asset_path(mod[:path]),
      integrity: mod[:integrity], crossorigin: "anonymous")
  }, "\n")
end

#assiette_stylesheet_tag(path) ⇒ Object

Generates a <link rel=“stylesheet”> tag with SRI integrity.



22
23
24
25
# File 'lib/assiette/helpers.rb', line 22

def assiette_stylesheet_tag(path)
  tag.link(rel: "stylesheet", href: assiette_asset_path(path),
    integrity: assiette_asset_integrity(path), crossorigin: "anonymous")
end