PhoenixHisto v1.0.0 PhoenixHisto View Source
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
orHEAD
. - it is a file request, e.g.
"/path/file.ext"
; note that ifPhoenixHisto
is plugged afterPlug.Static
and we get a file request than most likely the file does not exist and404
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 toPlug.Static
(required), refer toPlug.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
Link to this section Summary
Link to this section Functions
Callback implementation for Plug.call/2
.
Callback implementation for Plug.init/1
.