Sheharyar
Sheharyar

Reputation: 75740

Use Custom Fonts in Phoenix

I'm trying to use a Custom Font in my Phoenix Application. I've placed them in the priv/static/fonts directory, and properly created and loaded the css file in web/templates/layout/app.html.eex template but they're not being served by the Phoenix Server.

/Users/Psycho/code/elixir/my_app/
▾ priv/
  ▸ repo/
  ▾ static/
    ▸ css/
    ▾ fonts/
      ▾ walsheim/
          gt-walsheim-light-web.svg
          gt-walsheim-light-web.eot
          gt-walsheim-light-web.ttf
          gt-walsheim-light-web.woff

The css file for sourcing the font:

// my_app/priv/css/fonts.css

@font-face {
    font-family: "Walsheim";
    font-style: normal;
    font-weight: 300;
    src:
        url("/fonts/walsheim/gt-walsheim-light-web.eot?#iefix") format("embedded-opentype"),
        url("/fonts/walsheim/gt-walsheim-light-web.woff") format("woff"),
        url("/fonts/walsheim/gt-walsheim-light-web.ttf") format("truetype"),
        url("/fonts/walsheim/gt-walsheim-light-web.svg#Walsheim") format("svg");
}

Upvotes: 15

Views: 4399

Answers (1)

Sheharyar
Sheharyar

Reputation: 75740

Okay, found the solution.

It looks like you have to tell phoenix which directories to serve for static files. I went in to my my_app/lib/my_app/endpoint.ex file and updated the Plug.Static plug to serve the fonts folder as well:

defmodule MyApp.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app

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

  # Other Stuff ...
end

Source: PhoenixTalk - Serving static assets in a Sub-Folder other than the defaults

Upvotes: 23

Related Questions