Plug.Static

A plug for serving static assets.

It requires two options on initialization:

The preferred form is to use :from with an atom or tuple, since it will make your application independent from the starting directory.

If a static asset cannot be found, Plug.Static simply forwards the connection to the rest of the pipeline.

Cache mechanisms

Plug.Static uses etags for HTTP caching. This means browsers/clients should cache assets on the first request and validate the cache on following requests, not downloading the static asset once again if it has not changed. The cache-control for etags is specified by the cache_control_for_etags option and defaults to “public”.

However, Plug.Static also supports direct cache control by using versioned query strings. If the request query string starts with “?vsn=”, Plug.Static assumes the application is versioning assets and does not set the ETag header, meaning the cache behaviour will be specified solely by the cache_control_for_vsn_requests config, which defaults to “public, max-age=31536000”.

Options

Examples

This plug can be mounted in a Plug.Builder pipeline as follows:

defmodule MyPlug do
  use Plug.Builder

  plug Plug.Static, at: "/public", from: :my_app
  plug :not_found

  def not_found(conn, _) do
    send_resp(conn, 404, "not found")
  end
end
Source

Summary

call(conn, arg2)

Callback implementation for Plug.call/2

init(opts)

Callback implementation for Plug.init/1

Functions

call(conn, arg2)

Callback implementation for Plug.call/2.

Source
init(opts)

Callback implementation for Plug.init/1.

Source