Justin Lane
Justin Lane

Reputation: 408

Serving a Single Page App using Phoenix Framework causes two get requests

I'm trying to get a phoenix app serving a React SPA, following along with this advice: When accessing front-end SPA-links from browser, back-end fires finding no route

I've set this up successfully before following the above solution so I know its worked in the past.

Heres the relevant bits from my router:

pipeline :browser do
  plug(:accepts, ["html"])
end

...
scope "/" do
  pipe_through(:browser)
  get("/*anything", TexWeb.StaticController, :index)
end

My first client side route is at /start. When I go there, I find that I get two get requests in the log:

Generated tex app
[info] Running TexWeb.Endpoint with Cowboy using http://0.0.0.0:4000
[info] GET /start
[debug] Processing with TexWeb.StaticController.index/2
  Parameters: %{"anything" => ["start"]}
  Pipelines: [:browser]
[info] Sent 200 in 141µs
[info] GET /static/js/main.8e39f2e0.js
[debug] Processing with TexWeb.StaticController.index/2
  Parameters: %{"anything" => ["static", "js", "main.8e39f2e0.js"]}
Pipelines: [:browser]
[info] Sent 200 in 183µs

I've copied the exact same SPA code to another phoenix 1.3 instance and it successfully loads the start route. Interestingly, this log looks like:

[info] GET /start
[debug] Processing with TexappWeb.StaticController.index/2
  Parameters: %{"anything" => ["start"]}
  Pipelines: [:browser]
[info] Sent 200 in 17ms
[info] GET /
[debug] Processing with TexappWeb.StaticController.index/2
  Parameters: %{"anything" => []}
  Pipelines: [:browser]
[info] Sent 200 in 129µs

I'm pretty sure that the line:

Parameters: %{"anything" => ["static", "js", "main.8e39f2e0.js"]}

is the cause of my problems.

But where do I start to look to debug this?

Upvotes: 0

Views: 366

Answers (1)

Justin Lane
Justin Lane

Reputation: 408

Forgot to change this line in endpoint.ex

From:

plug(Plug.Static, at: "/", from: :tex, gzip: false, only: ~w(css fonts images js favicon.ico robots.txt))

To:

plug(Plug.Static, at: "/", from: :tex, gzip: false)

Upvotes: 0

Related Questions