GameServerWeb.SRI (game_server_web v1.0.938)

Copy Markdown

Computes Subresource Integrity (SRI) hashes for static assets.

Returns a sha384-<base64> string suitable for the integrity attribute on <script> and <link> tags. In environments without code reloading, hashes are cached in persistent_term for the lifetime of the BEAM node.

Returns nil when the file doesn't exist (e.g. in dev before digest), so the attribute is safely omitted from the rendered HTML.

Usage in HEEx templates

<% path = ~p"/assets/js/app.js" %>
<script src={path} integrity={SRI.integrity(path)} crossorigin="anonymous"></script>

The module is aliased as SRI in html_helpers, so it's available in all templates without an explicit alias.

Summary

Functions

Returns the SRI integrity hash ("sha384-...") for the given static path, or nil if the file cannot be found.

Returns a cache-busted version of the given static path by appending a content-derived v query parameter.

Functions

integrity(path)

@spec integrity(String.t() | nil) :: String.t() | nil

Returns the SRI integrity hash ("sha384-...") for the given static path, or nil if the file cannot be found.

The path should be the URL path as returned by the ~p sigil (e.g. "/assets/js/app.js" or "/assets/js/app-ABC123.js" after digest).

versioned_path(path)

@spec versioned_path(String.t() | nil) :: String.t() | nil

Returns a cache-busted version of the given static path by appending a content-derived v query parameter.

This avoids browsers reusing a stale /assets/... response under a newer integrity hash when assets change locally without a full browser cache clear.