Setup Phoenix Router with a Single Page App
I’ve been playing around with Phoenix for a personal project. Phoenix by default comes
setup with a basic PageController
. I wanted to use Elm’s Navigation package to handle routing
on my Elm app.
With the default setup, Phoenix will attempt to serve the route, ending in a 404. I had to change my router.ex
to the following to allow any path to defer to the PageController.index
function:
1
2
3
4
5
6
7
8
9
defmodule AppliedAt.Router do
use AppliedAt.Web, :router
scope "/", AppliedAt do
pipe_through :browser # Use the default browser stack
get "/*path", PageController, :index # <-- Allows any number of path items
end
end
This should work for any router in JavaScript, Elm, or < insert language here > 😄.