PhoenixHisto v1.0.0 API Reference

Modules

A plug for handling client side routing in Phoenix, a.k.a. History Api fallback. It rewrites requests for client side routes to index.html file.

Requests exempt from rewrite

The fallback algorithm is limited only to certain requests. In particular it will not rewrite if:

  • request method is not GET or HEAD.
  • it is a file request, e.g. "/path/file.ext"; note that if PhoenixHisto is plugged after Plug.Static and we get a file request than most likely the file does not exist and 404 response will be returned
  • the client does not accept text/html response MIME type
  • the requested path starts with one of the paths in :blacklist

Options

  • :index - path to index.html fallback relative to the dir passed in :static_opts (defaults to "index.html").
  • :blacklist - list of path prefixes exempt from fallback algorithm (defaults to []).
  • :static_opts - options to forward to Plug.Static (required), refer to Plug.Static docs. Note that :only and :only_matching are not respected.

Examples

The best place to mount this plug is in Phoenix.Endpoint, just after all Plug.Static entries:

defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app
  @static_opts [at: "/", from: :my_app, gzip: false]
  plug Plug.Static,
    (@static_opts ++
    [only: ~w(css fonts img js favicon.ico robots.txt manifest.json)])
  plug PhoenixHisto,
    static_opts: @static_opts,
    blacklist: ["/api"]
  ...
end