Phoenix router with put_root_layout causes mass / cyclic dependencies

I’m having an issue with put_root_layout() causing all modules in the entire project to have a compile-time dependency on the Router, which significantly slows things down for me.

Essentially, adding a line like this

  pipeline :browser do
    # 
    plug :put_root_layout, {PortalsWeb.LayoutView, :root}
    # ... <snipped>
  end

Causes a change to any file to also recompile router.ex… which in my particular project takes a long time. I’ve been using mix xref to try and determine exactly why this is the case, but it basically shows that every file (even non-web related onces) have a transitive compile-time dependency on LayoutView.

Can you try splitting out that call into a new plug? I’ve solved a nearly identical problem for TheChangelog here:

3 Likes

That fixed it for me! Thank you so much for pointing that out.

The funny part: I actually came across your solution months ago, and implemented it (for setting a root layout for LiveView plugs), and in the intervening time totally forgot about it (and lost track because I was following the Phoenix docs so closely for the 1.5 feature).

Compile times are looking good again (unless I actually change the router, of course…)

Glad it helped! In case you haven’t seen I’ll give a short plug for DepViz: https://depviz.jasonaxelson.com/
Which is a tool I wrote that helps you analyze compile-time dependencies in your projects.