Rails 5 + Rails Assets + Fontawesome does not load fonts - font-awesome

On installing Fontawesome via rails-asset.org, and following the default instructions, the fonts don't load correctly and all I see is boxes, indicating, the url's being generated to load the font are incorrect.

UPDATE : Save yourselves some time and switch gems. Here are the steps. I found that even with my previous answer there are path issues in production. However its easiest to get fontawesome working with:
gem 'font-awesome-rails'
#import "font-awesome"; in your scss file
Done!
Ignore everything below this! unless you absolutely want to use gem 'rails-assets-fontawesome'
So looks like this is a bug in the library and the asset pipeline does not find the path. The fix suggested in the issue does not work any more because the path seems to have changed since and there is no hyphen in font-awesome path anymore.
Here's the correct fix:
Create a new file app/assets/stylesheets/font-awesome.scss with content:
fa-font-path: "fontawesome/fonts";
#import "fontawesome";
In application.scss include:
*= font-awesome
This should fix the problem and icons should start showing up.
Remarks:
If you choose to move the font-awesome.scss inside some directory under app/assets/stylesheets/somedir/font-awesome.scss, then you need to fix the fa-font-path variable to have correct relative path like so:
fa-font-path: "../fontawesome/fonts";
Be careful with paths and names!
The file you create cannot be called fontawesome.scss as this is the name used by the packaged gem.
If you have newest version of the gem rails-assets-fontawesome (4.7.0) then the import and fa-font-path will use fontawesome and not font-awesome as in older versions of this gem. Not sure how far back in versions this behavior goes.

I have it worked with adding these lines to config/initializers/assets.rb:
Rails.application.config.assets.paths << Rails.root.join('node_modules')
# font-awesome fonts
Rails.application.config.assets.precompile << %r{font-awesome/fonts/[\w-]+\.(?:eot|svg|ttf|woff2?)$}
and having this as mentioned in other answers:
application.scss
$fa-font-path: "fontawesome/fonts";
#import "fontawesome";
Run rake assets:precompile and you should see font files in public/assets/font-awesome/fonts

For 5.5.0 version it should be look like this:
$fa-font-path: 'fontawesome/web-fonts-with-css/webfonts';
#import "fontawesome";
// Copy-paste of these files:
// #import "fontawesome/solid";
// #import "fontawesome/regular";
// But url() replaced with font-url()
#font-face {
font-family: 'Font Awesome 5 Free';
font-style: normal;
font-weight: 400;
src: font-url('#{$fa-font-path}/fa-regular-400.eot');
src: font-url('#{$fa-font-path}/fa-regular-400.eot?#iefix') format('embedded-opentype'),
font-url('#{$fa-font-path}/fa-regular-400.woff2') format('woff2'),
font-url('#{$fa-font-path}/fa-regular-400.woff') format('woff'),
font-url('#{$fa-font-path}/fa-regular-400.ttf') format('truetype'),
font-url('#{$fa-font-path}/fa-regular-400.svg#fontawesome') format('svg');
}
.far {
font-family: 'Font Awesome 5 Free';
font-weight: 400;
}
#font-face {
font-family: 'Font Awesome 5 Free';
font-style: normal;
font-weight: 900;
src: font-url('#{$fa-font-path}/fa-solid-900.eot');
src: font-url('#{$fa-font-path}/fa-solid-900.eot?#iefix') format('embedded-opentype'),
font-url('#{$fa-font-path}/fa-solid-900.woff2') format('woff2'),
font-url('#{$fa-font-path}/fa-solid-900.woff') format('woff'),
font-url('#{$fa-font-path}/fa-solid-900.ttf') format('truetype'),
font-url('#{$fa-font-path}/fa-solid-900.svg#fontawesome') format('svg');
}
.fa,
.fas {
font-family: 'Font Awesome 5 Free';
font-weight: 900;
}

Can't comment above, but shouldn't it be:
$fa-font-path: "fontawesome/fonts";
You can right above the suggested import by the gem:
#import "fontawesome";

if fontawsome 4.*...
Copy fonts from fontawsome/fonts for rails public folder: public/fonts

After about a day and a half of learning ALL about the asset pipeline, its cache and other fun topics, here's how I finally got this to work...
Rails 5.2.1
Font Awesome 6 Pro (loaded via npm using .npmrc with license key)
Using SCSS
Also using font-awesome-rails (but only for the helpers - notice there is no #import "font-awesome"; below)
# config/initializers/assets.rb
# Adde node_modules/ to the asset pipeline
Rails.application.config.assets.paths << Rails.root.join('vendor', 'assets', 'node_modules')
# Pre-compile any font files located in the asset pipeline
# This tells Rails to precompile the font files, fingerprint them, and place them in /public/assets/ (in the same subdir where they live in the pipeline)
# e.g.
# node_modules/#fortawesome/fontawesome-pro/webfonts/fa-solid-900.ttf (pipeline path)
# public/assets/#fortawesome/fontawesome-pro/webfonts/fa-solid-900-229b67ef16372975f562838a5c9f9bd2d9d9905ccc9c77f34c45f3d1f18f016f.ttf (pre-compiled asset path)
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|woff2|ttf)$/
# application.scss
#import "#fortawesome/fontawesome-pro/scss/fontawesome.scss";
// Don't use provided font imports b/c we need to use font-url to get asset paths with fingerprints!
// $fa-font-path: "../webfonts"; // Don't use this
// #import "#fortawesome/fontawesome-pro/scss/regular.scss"; // Don't use this
#import "fonts"; // Use this instead so that we can use font-url
# fonts.scss
/* Font Awesome 6 */
#font-face {
font-family: 'Font Awesome 6 Pro';
font-style: normal;
font-weight: 900;
src: font-url("#fortawesome/fontawesome-pro/webfonts/fa-solid-900.woff2") format("woff2"),
font-url("#fortawesome/fontawesome-pro/webfonts/fa-solid-900.ttf") format("truetype"); }
#font-face {
font-family: 'Font Awesome 6 Pro';
font-style: normal;
font-weight: 400;
src: font-url("#fortawesome/fontawesome-pro/webfonts/fa-regular-400.woff2") format("woff2"),
font-url("#fortawesome/fontawesome-pro/webfonts/fa-regular-400.ttf") format("truetype"); }
#font-face {
font-family: 'Font Awesome 6 Brands';
font-style: normal;
font-weight: normal;
src: font-url("#fortawesome/fontawesome-pro/webfonts/fa-brands-400.woff2") format("woff2"),
font-url("#fortawesome/fontawesome-pro/webfonts/fa-brands-400.ttf") format("truetype"); }
Also, one of the hardest things about this was realizing that my production environment wasn't working the same as my dev environment so I only realized the issue after deploying :( Here's how to avoid that...
# config/environments/development.rb
# Don't use pipeline if asset path fails
# config.assets.compile defaults to true in dev...so if the pre-compiled asset doesn't exist in public/assets/..., it will just pull from the pipeline and you won't know that you have an issue until you deploy!
config.assets.compile = false # This will raise an exception if the pre-compiled asset not found!
config.assets.debug = false # Pre-compile assets into single file
config.assets.digest = true # Use fingerprinting
Now, of course, this means you need to pre-compile your assets in development, so you need to run bundle exec rake assets:precompile from the root directory of your project. This will pre-compile your assets and place them in /public/assets/
If you run into a cache issue where the pre-compiler doesn't actually re-compile a resource, you can delete the Sprockets cache by running rm -r tmp/cache/assets
Once you have successfully migrated your asset changes to production, I would recommend reverting development.rb back to the defaults for efficient development. Also, you may want to bundle exec rake assets:clobber to delete the pre-compiled assets.
Here's some other good info I found after the fact...
https://guides.rubyonrails.org/asset_pipeline.html#in-development
Hope this helps someone save several hours of "rabbit-holing" :)

Related

"#font-face declaration doesn't follow the fontspring bulletproof syntax" error when trying to implement custom font

I'm trying to add a custom font to my website, but keep getting the same error no matter what I try, and I can't find much about how to go about fixing it.
Here's my code:
#font-face {
font-family: "Moderna";
src: url("/MODERNA.TTF")
}
Suggestions?
The fontspring bulletproof syntax is just a recommendation. It declares your #font-face must satisfy below syntax to work properly in most cases:
#font-face {
font-family: 'MyFontFamily';
src: url('myfont-webfont.eot?') format('eot'),
url('myfont-webfont.woff') format('woff'),
url('myfont-webfont.ttf') format('truetype'),
url('myfont-webfont.svg#svgFontName') format('svg')
}
Most browsers supports TTF fonts now but if you can't see the font in browser try this
#font-face {
font-family: "Moderna";
src: url("/MODERNA.TTF") format("ttf");
}
And if my memory serves me then the rule must be on top of your css file. And please check the file /MODERNA.TTF exists.
Pretty sure your path/url is causing this issue:
src: url("/MODERNA.TTF")
implies you're entering a direcory named 'MODERNA.TTF' and not a file.
As #Mik pointed out the recommended declaration should be fine – unless your file couldn't be found.
Depending on your html/css file structure it should rather be something like this:
(granted your font file resides in the same directory as your css.
#font-face {
font-family: 'Moderna';
src: url('MODERNA.eot?') format('eot'),
url('MODERNA.woff') format('woff2'),
url('MODERNA.woff') format('woff'),
url('MODERNA.ttf') format('truetype'),
url('MODERNA.svg#svgFontName') format('svg')
}
Keep also in mind, that referencing font files via #font-face is case-sensitive (there might be exceptions).
So double check if your exact font file name is really using uppercase letters (MODERNA.TTF or moderna.ttf).

I updated the theme, but now the font-face is not working? Whats going on?

Staging site: https://woocommerce-81222-860870.cloudwaysapps.com/
--
The live site shows the correct cursive font. But on the staging, it is not working. I had to update the theme, and now it does not work. I have the same paths too. Not sure whats going on.
I have tried different varieties of paths.
#font-face {
font-family: 'quentinregular';
src: url('font/quentin-webfont.woff2') format('woff2'),
url('font/quentin-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
I don't see any errors for the font face.
Image of the cursive font face
The font no longer exists at font/quentin-webfont.woff or font/quentin-webfont.woff2 you will need to locate the new path to the font files. Try searching in the file manager to find the fonts
In your head tag, you have a style tag that contains the following code:
#font-face { font-family:quentin;src:url(//81222-445514-raikfcquaxqncofqfm.stackpathdns.com/wp-content/uploads/2019/05/quentin.woff2) format('woff2'), url(//81222-445514-raikfcquaxqncofqfm.stackpathdns.com/wp-content/uploads/2019/05/quentin.woff) format('woff'), url(//81222-445514-raikfcquaxqncofqfm.stackpathdns.com/wp-content/uploads/2019/05/Quentin.ttf) format('truetype');}
So, there you have font-family:quentin;, but the CSS rules contain font-family:quentinregular;. You need to change that.

How to use #font-face from fonts that added to my project?

I have a font file in font directory of my project and use it to css file,in my system working true but not working in the client system.how to use it that correct result ?
my font-face in css is :
#font-face {
font-family: 'Yekan';
src: url('font/Yekan.eot');
src: url('Font/Yekan.woff');
src: url('Font/Yekan.eot?#iefix') format('embedded-opentype'), url('font/Yekan.ttf'), url('font/Yekan.svg');
font-weight: bold;
}
My font path is :
You can also try creating a web font
https://www.web-font-generator.com/
Try to remove all the ' ' from the css.
This should work for you.

font-face not working externally

I am trying to use a custom font which works locally for me, but i need to link it too an external website due to a specific requirement.
But as soon as i test it on my localhost when it is linked to my domain - the fonts dont show up
Im not sure if their is a cross domain issue - but any help would be appreciated cheers
#font-face {
font-family: "Wisdom";
src: url("http://transformer.tamar.com/Santa/726318360-Wisdom-Script.eot?#iefix") format("embedded-opentype"),
url("http://transformer.tamar.com/Santa/726318360-Wisdom-Script.svg#Wisdom Script AI") format("svg"),
url("http://transformer.tamar.com/Santa/726318360-Wisdom-Script.woff") format("woff"),
url("http://transformer.tamar.com/Santa/726318360-Wisdom-Script.ttf") format("truetype");
}
h3.step {
font-family: 'Wisdom';
font-size: 1em;
text-align: center;
}
#font-face URL's must be relative.
Example:
src: url("/Santa/726318360-Wisdom-Script.ttf") format("truetype");
Extra explanations about Relative URLs
Font-embedding, that is, the #font-face requires the font file to be created on a per-domain basis.
That's because the some fonts might have copyright issues

installing new Fonts in CSS

I have been attempting to install a font and use it in my CSS and have been unsuccessful at this. I would like to figure out where am I going wrong. I uploaded the .tff file in the same folder as the CSS file and added to the updated my css file in the appropriate manner.
here is a copy of what I have in my css:
#font-face
{
font-family: ImpactLabel;
src: url('ImpactLabel.tff');
}
p.change
{
font-family:ImpactLabel;
color: #A70C1E;
font-size:3em;
}
Here is my html file:
<p class="change">Text</p>
Check this ->
#font-face {
font-family: ImpactLabel;
src: url('ImpactLabel.ttf'), <-- Check if the path is correct (ttf not tff)
url('ImpactLabel.eot'); /* IE9 */
}
Then just call on your class:
.change {
font-family: 'ImpactLabel';
}
try this http://www.fontsquirrel.com/tools/webfont-generator
#font-face {
font-family: 'primelight';
src: url('prime_light-webfont.eot');
src: url('prime_light-webfont.eot?#iefix') format('embedded-opentype'),
url('prime_light-webfont.woff') format('woff'),
url('prime_light-webfont.ttf') format('truetype'),
url('prime_light-webfont.svg#primelight') format('svg');
font-weight: normal;
font-style: normal;
}
p.change{
font-family: "primelight", Verdana, Tahoma;
}
creat a repository in your website : fonts/prime/
put the prime files inside make sure you have something like this :PrimeLight.otf
read more here I need Prime font for my web site
Upload it to the FontSquirrel Webfont Generator and use the code and files it gives you. Problem solved!
FontSquirrel will generate several different formats of the font which are loaded selectively by different browsers and devices (and thus will give you the broadest compatibility).
Edit: Apparently this font is actually on FontSquirrel already, so all you need to do is click here and download the webfont kit from them.