Francis.Static (Francis v0.4.0)

View Source

Utilities for working with static assets.

This module provides functions to resolve static paths and URLs, including support for digested assets generated by mix francis.digest.

The decoded manifest is cached in :persistent_term, keyed by its path, and reloaded only when the file's mtime, size or inode changes (e.g. after re-running mix francis.digest, which replaces the manifest atomically via rename, so the inode changes even when a rewrite lands in the same second with the same size).

Summary

Functions

Returns all assets from the manifest.

Returns the cache-control header value recorded by mix francis.digest --age, or nil when the manifest is missing or has none.

Loads the cache manifest from the given path.

Functions

all(manifest_path \\ "priv/static/cache_manifest.json")

Returns all assets from the manifest.

Examples

iex> Francis.Static.all()
%{
  "app.css" => %{
    "digest" => "a1b2c3d4",
    "digested_path" => "app-a1b2c3d4.css",
    "size" => 1024
  }
}

cache_control(manifest_path \\ "priv/static/cache_manifest.json")

Returns the cache-control header value recorded by mix francis.digest --age, or nil when the manifest is missing or has none.

Examples

iex> Francis.Static.cache_control()
"public, max-age=31536000, immutable"

exists?(logical_path, manifest_path \\ "priv/static/cache_manifest.json")

Checks if an asset exists in the manifest.

Examples

iex> Francis.Static.exists?("app.css")
true

iex> Francis.Static.exists?("nonexistent.css")
false

load_manifest(manifest_path \\ "priv/static/cache_manifest.json")

Loads the cache manifest from the given path.

Returns {:ok, manifest} on success, {:error, reason} on failure.

Examples

iex> Francis.Static.load_manifest("priv/static/cache_manifest.json")
{:ok, %{"version" => 1, "files" => %{...}}}

iex> Francis.Static.load_manifest("nonexistent.json")
{:error, :enoent}

static_path(logical_path, base_path \\ "/", manifest_path \\ "priv/static/cache_manifest.json")

Returns the path for the given asset.

If a digest manifest is available and the asset is found in it, returns the digested path. Otherwise, returns the original path.

Examples

iex> Francis.Static.static_path("app.css")
"/app-a1b2c3d4.css"