I want to set Phoenix up to serve a static index.html no matter what route is sent to it, without changing the URL and while providing access to non-html assets (.js, .css, .jpg,...), as my SPA (in Elm) will look at the route and work out what to do.
Based on this and this I have tried the following, but without success
endpoint.ex
plug Plug.Static,
at: "/", from: :mosaic_api, gzip: false,
only: ~w(assets css fonts images js favicon.ico robots.txt index.html)
router.ex
scope "/", Api do
pipe_through :browser # Use the default browser stack
get "/*path", PageController, :index # :elm
end
PageController.ex
defmodule Api.PageController do
use Api.Web, :controller
plug :action # Edit: now removed
def index(conn, _params) do
conn
|> put_layout(false)
|> render("index.html")
# Edit: replaced 3 lines above by: html(conn, File.read!("priv/static/index.html"))
end
def elm(conn, _params) do
redirect conn, to: "/index.html"
end
end
With :index
I get a Phoenix-related (but not the standard home-) page and a console error (Plug.Conn.AlreadySentError) the response was already sent
, while with :elm
I end up at /index.html and have lost the routing information.