I would still have an index.html.eex template. It will be precompiled then you can render it as normal in a PageController.index action. This way you don’t have to hit disk on every page just to render the home page. That will be a huge performance hit.
@chrismccord: ah, I see your point
What do you think about:
defmodule MyApp.Router do
use Plug.Router
plug :match
plug :dispatch
alias Phoenix.Controller
get "/" do
conn
|> Controller.put_layout(false)
|> Controller.put_view(MyApp.PageView)
|> Controller.render("index.html")
end
end
Will it work?
I do not need it as .eex file, because I don’t use Elixir code there …
In normal case I will use Controllers, but here I have just index.html, app.css, app.js and phoenix.js files and all happens with Websocket connection.
I would still make the “static” index.html into an index.html.eex template so you get the all the precompiled goodies. What you showed w/ the Plug.Router can work, but I would honestly just keep it part of your regular phoenix router and render a SomeController.index action and template.
@chrismccord: Yup, I know, but here I do not need all of that goodies.
Of course caching (by &render/2 method as you suggested) index.html.eex file is always important, but I will keep rest: empty view and no layout. tt’s just small and simple HTML5 WebSocket client.
Thank you for your help!
Final solution:
defmodule MyApp.Router do
use Plug.Router
plug :match
plug :dispatch
alias Phoenix.Controller
get "/" do
conn
|> Controller.put_layout(false)
|> Controller.put_view(MyApp.PageView)
|> Controller.render("index.html")
end
end
Had a similar question. I’m currently doing the following in lib/app/web/router.ex:
scope "/", App.Web do
pipe_through :browser # Use the default browser stack
get "/*anything", PageController, :index
end
This has the advantage of working with my client-side router, so links like /login still route to the controller but renders the correct client-side view. Unfortunately this blocks access to /socket, which I thought would still work since it gets configured outside the router in the Endpoint.
Is there a way around this? Basically I want to let everything through except for /socket, which should be handled outside the router. Another option might be telling the router to intercept /socket before /*anything and just not handle it, letting UserSocket deal with it.