Forwarding Static Assets in an Umbrella App

Hi all,

We’re currently building a Phoenix app that has a forward to another Phoenix app’s router if no route matches in the parent app. Everything seems to work correctly except for static assets. Since the static plug is defined in the endpoint module, it won’t get called if we forward directly to the router. However, forwarding to the endpoint is not recommended per the Phoenix docs.

I could configure the static plug in the parent app and have it point to the child, but I would prefer not to. Would it make sense to create another "endpoint’ in the child app that contains only the static plug and router and then forward to that? Or is there a better approach?

Thanks in advance!

1 Like

If you really don’t want to merge the paths, then what about adding the static plug of the ‘other’ app in the pipeline that you are forwarding to instead of in the root endpoint of that section?

2 Likes

Huh, I didn’t even think of that. Makes sense though. Tried it and it works! Here’s what I settled on:

defmodule ParentAppWeb.Router do
  use ParentAppWeb, :router

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

  scope "/" do
    pipe_through :child_app
    forward "/", ChildAppWeb.Router
  end
end
2 Likes