Current section
Files
Jump to
Current section
Files
lib/combo/static.ex
defmodule Combo.Static do
@default_compressors [Combo.Static.Compressor.Gzip]
@default_compressible_extensions ~w(.js .map .css .txt .text .html .json .svg .eot .ttf)
@moduledoc """
Manages the static files.
It is responsible for:
* generating the manifest file of static files.
* managing the cache of static files.
## Configurations
* `:manifest` - a path to a JSON manifest file. This is typically set to
"priv/static/manifest.digest.json" which is the file generated by
`mix combo.static.digest`.
It can be either a string containing a file system path, or a tuple containing
the application name and the path within that application.
Default to `nil`.
* `:vsn` - when `true`, adds query string "?vsn=d" to the generated paths.
This query string is used by `Plug.Static` to set long expiry dates.
Therefore, if you are not using `Plug.Static` to serve assets (for example,
you are using a CDN), you should set this config to `false`, and you should
also consider passing `--no-vsn` to `mix combo.static.digest`.
Defaults to `true`.
* `:compressors` - the list of compressors.
Default to `#{inspect(@default_compressors)}`.
* `:compressible_extensions` - the list of compressible file extensions.
Default to `#{inspect(@default_compressible_extensions)}`.
## Note
To prevent a race condition where the socket listener is already started
but the static is not ready, this module should be started as a child in
the supervision tree of endpoint.
"""
alias __MODULE__.Cache
def child_spec(arg) do
%{
id: make_ref(),
start: {__MODULE__, :start_link, [arg]}
}
end
def start_link({endpoint, safe_config}) do
Cache.warmup(endpoint, safe_config)
# As we don't actually want to start a process, we return :ignore here.
:ignore
end
@doc """
Lists the compressors.
"""
@spec compressors :: [module()]
def compressors do
Combo.Env.get_env(
:static,
:compressors,
@default_compressors
)
end
@doc """
Lists the extensions of compressible files.
"""
@spec compressible_extensions :: [String.t()]
def compressible_extensions do
Combo.Env.get_env(
:static,
:compressible_extensions,
@default_compressible_extensions
)
end
def lookup(endpoint, path), do: Cache.lookup(endpoint, path)
end