Verified routes - Phoenix 1.7.10

I have an umbrella app, and in an html.eex file (apps/auction_web/lib/auction_web/layouts/app.html.heex), there is this link:

     <a href="/">
        <img src={~p"/images/logo.png"} width="36" />
      </a>

That html contains the ~p sigil which asks Phoenix to verify the route, which as far as I can tell means there has to be a route in router.ex matching /images/logo.png–otherwise there will be a compile time error. When I run:

auction_umbrella/apps/auction_web$ mix compile
Compiling 2 files (.ex)

there are no errors. But, if I change the path to /books/logo.png:

 <a href="/">
    <img src={~p"/books/logo.png"} width="36" />
  </a>

I get a warning:

auction_umbrella/apps/auction_web$ mix compile
Compiling 3 files (.ex)
warning: no route path for AuctionWeb.Router matches "/books/logo.png"
  lib/auction_web/components/layouts/app.html.heex:5: AuctionWeb.Layouts.app/1

Here’s my router (apps/auction_web/lib/auction_web/router.ex):

defmodule AuctionWeb.Router do
  use AuctionWeb, :router

  pipeline :browser do
    plug(:accepts, ["html"])
    plug(:fetch_session)
    plug(:fetch_live_flash)
    plug(:put_root_layout, html: {AuctionWeb.Layouts, :root})
    plug(:protect_from_forgery)
    plug(:put_secure_browser_headers)
  end

  pipeline :api do
    plug(:accepts, ["json"])
  end

  scope "/", AuctionWeb do
    pipe_through(:browser)

    get("/", PageController, :home)
    resources("/items", ItemController, only: [:index, :show])
  end

  # Other scopes may use custom stacks.
  # scope "/api", AuctionWeb do
  #   pipe_through :api
  # end

  # Enable LiveDashboard in development
  if Application.compile_env(:auction_web, :dev_routes) do
    # If you want to use the LiveDashboard in production, you should put
    # it behind authentication and allow only admins to access it.
    # If your application does not have an admins-only section yet,
    # you can use Plug.BasicAuth to set up some basic authentication
    # as long as you are also using SSL (which you should anyway).
    import Phoenix.LiveDashboard.Router

    scope "/dev" do
      pipe_through(:browser)

      live_dashboard("/dashboard", metrics: AuctionWeb.Telemetry)
    end
  end
end

Where is the route for /images/logo.png? If I comment out the lines:

    get("/", PageController, :home)
    resources("/items", ItemController, only: [:index, :show])

I still don’t get a compile time warning.

1 Like

I’m not super familiar with the layout of umbrella apps, but /images is a static path. Those are defined in your Endpoint. In a non-umbrella app they are incapsulated in a function at MyAppWeb.static_paths but if you search for def static_paths that should give your answer.

3 Likes

Thanks for the response.

I looked in apps/auction_web/lib/auction_web/endpoint.ex, and I found this comment:

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phx.digest
  # when deploying your static files in production.
  plug Plug.Static,
    at: "/",
    from: :auction_web,
    gzip: false,
    only: AuctionWeb.static_paths()

The last line calls AuctionWeb.static_paths(), so I looked in apps/auction_web/lib/auction_web.ex, and I found this:

defmodule AuctionWeb do

    def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)

    ...
    ...

Thanks!

3 Likes