我想将 Phoenix 设置为提供静态 index.html,无论发送到它的路由是什么,无需更改 URL,同时提供对非 html 资源(.js、.css、.jpg...)的访问,因为我的 SPA(在 Elm)会查看路线并找出要做什么。
基于this https://stackoverflow.com/questions/32407528/precompiled-single-page-apps-in-phoenix-app and this https://stackoverflow.com/questions/35100117/how-to-serve-static-page-in-phoenix-framework我尝试过以下方法,但没有成功
端点.ex
plug Plug.Static,
at: "/", from: :mosaic_api, gzip: false,
only: ~w(assets css fonts images js favicon.ico robots.txt index.html)
路由器扩展程序
scope "/", Api do
pipe_through :browser # Use the default browser stack
get "/*path", PageController, :index # :elm
end
页面控制器.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
我收到一个与 Phoenix 相关的(但不是标准主页)页面和一个控制台错误(Plug.Conn.AlreadySentError) the response was already sent
,同时与:elm
我最终到达 /index.html 并且丢失了路由信息。