annad

annad

Documentation for adding fonts to Liveview Project

I am using Tailwinds and I want to use a font family I found on Google. I have a url for the font, but I think it would be safer to download and add the font to my project. I can’t find any documentation (on the Phoenix site) on how to do this. I have found some previous Elixir Forum posts, but they are old posts and don’t know if they apply to 1.5.

Could someone point me in the right direction?

Most Liked

kokolegorille

kokolegorille

I made a small script to retrieve fonts and have them locally…

#!/bin/sh

for family in $*; do
for url in $( {
for agent in \
'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0' \
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1' \
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)' ;
do
curl -A "$agent" -s "http://fonts.googleapis.com/css?family=$family" | \
grep -oE 'http://[A-Za-z0-9/._-]+'; \
done } | sort -u ) ;
do
extn=${url##*.} ;
file=$(echo "$family"| tr +[:upper:] _[:lower:]);
echo $url $file.$extn;
curl -s "$url" -o "$file.$extn";
done
done

For example.

$ ./get_font.sh Roboto
http://fonts.gstatic.com/s/roboto/v27/KFOmCnqEu92Fr1Mu4mxM.woff roboto.woff
http://fonts.gstatic.com/s/roboto/v27/KFOmCnqEu92Fr1Mu4mxPKTU1Kg.ttf roboto.ttf

Usually I put them in assets/fonts and have them processed by webpack, and file loader.

Then I just add a font stylesheet… like this.

@font-face {
  font-family: roboto;
  src: url("../fonts/roboto.ttf") format("truetype"); /* For IE */
  src: local("roboto"), url("../fonts/roboto.ttf") format("truetype"); /* For non-IE */

  src: local("roboto"), url("../fonts/roboto.woff") format("woff");
}

and the webpack rule.

        // Load fonts
        {
          test: /\.(woff|woff2|eot|ttf|otf)$/,
          use: [
            {
              loader: "file-loader",
              options: {
                name: "[name].[ext]",
                // Relative to output public_path
                outputPath: "./fonts/"
              }
            }
          ]
        },

You need to install file_loader npm package to do this…

malloryerik

malloryerik

OK, so, older versions of Phoenix used Brunch, but then switched to Webpack. So Phoenix currently uses Webpack and not Brunch.

We can divide your fonts issue into two parts:

  1. getting the fonts into your application, and
  2. using them.

For the first part, since you’re using Google Fonts, the easiest way to get the fonts into your app is probably just to use the Google Fonts cdn link from fonts.google.com, something like this:

<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Merriweather&family=Oswald&display=swap" rel="stylesheet">

You put that into the head of your root.html.leex file, so that’s in lib/myapp_web/templates/layout/root.html.leex

Just add the links in the head of that file, so somewhere <head> here </head>.

That’s what @kokolegorille meant when they said, “link the stylesheet in root.html.eex.”

Once that’s done you can get the second part right, making sure that you’re using the fonts correctly within your app.

Then when you are going to deploy or have a moment, you can work on a more performant way of getting the fonts installed in your app, like the way @kokolegorille code showed earlier, or just using the Fontsource way.

annad

annad

YAY!!! The links worked! Thank you for dividing the issue in two parts and telling me where to put those links. Sometimes it is so embarrassing the basic things I don’t know. Sigh. I’m going to work on styling and then come back to this thread to investigate all of the options for “loading” the fonts for deployment. I will definitely check out @fontsource (thank you @c4710n for those links).

While it feels like I spent a week on fonts, it was really a valuable lesson on static assets. Thank you!

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement