Angular CLI is modifying the paths to fonts set in @font-face to point to the root directory in a production build
Bug Report or Feature Request (mark with an x)
- [ x] bug report -> found similar issue which i believe to have been closed in error: #3143
Versions.
Output from: `ng --version`.
@angular/cli: 1.1.0
node: 8.0.0
os: darwin x64
@angular/animations: 4.1.3
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/router: 4.1.3
@angular/cli: 1.1.0
@angular/compiler-cli: 4.1.3
@angular/language-service: 4.1.3
Repro steps.
ng new testapp --style scss
Then created new directory in ./src/assets/fonts/ which includes a set of custom font files like Gibson-Light-webfont.eot for example.
Then in my styles.scss file i reference those fonts like this:
@font-face {
font-family: 'gibson_package';
src: url('assets/fonts/Gibson-Light-webfont.eot');
...
Then i run ng build --prod
In my ./dist directory, the build process correctly copies my fonts to ./dist/assets/fonts/* as expected, but it also included duplicates of all of my fonts right in the root of my ./dist directory which i did not expect and the paths set in @font-face url's are changed to point to the root.
The log given by the failure.
No errors are output.
Desired functionality.
That the production build only copy my fonts to the ./dist/assets/fonts directory and not modify the paths in @font-face urls.
@ogousa great job. 🥇 Absolute path in styles.scss did the job. 👍 That's ridicolous anyway 👎
:-1: this solution doesn't work for me, since the CSS I'm using in on the other domain, in case I'm using absolute path it's trying to load the file from the main domain.
This is a problem indeed We need to have custom rewrite rules in our NGINX servers and having the fonts stay in the assets folder would simplify this quite a bit.
We cannot use absolute paths either (as mentioned, while this may work for some, this "solution" s*cks a bit)
@filipesilva , please in next update try to give us a way to change default path of linked in css/less/sass asset(not into dist/) in angular.json or other configuration file.. Because dozens or even hundreds of files in root directory it's not very cool... 💯👎 thank you in advance 👍 🥇
I am using several webfonts installed with npm. They are referenced directly in the node_modules folder in my angular.json file. Unless this is fixed in the CLI, I'd have to manually edit all these CSS files inside node_modules to use absolute paths, which is not a good idea of course, because I'd have to do it again in every npm install. Ideally, I should have a script that modifies these files automatically before they are compiled, but I have no clue how to do this. I assume it will require a custom webpack config with a plugin/loader to do it?
I had same issue got it fixed by creating fonts folder in assets and added font.css file with all fontface added in that new file and then imported font.css file into style.css file and it worked for me
font.css: @font-face { font-family: 'Tele-Bold'; src: url('../assets/fonts/Tele-Bold.ttf'); } style.css: @import ".assets/fonts/fonts.scss";
Similar issue with copied files We have a service worker that fails to cache files because they're gone from assets folder and now they're in root. Weird behavior
@QuipHop In the file : ngsw-config.json Just add the extensions of the files. Like this :
"files": [
"/favicon.ico",
"/index.html",
"/*.css",
"/*.js",
"/*.jpeg", // added
"/*.woff2", // added
"/*.svg", // added
"/*.ttf", // added
"/*.eot" // added
]
Because all the files referenced in the css or scss are imported from the source to the root. That's why you should add them manually to the swConfig file.
Re-open this issue
I had same issue got it fixed by creating fonts folder in assets and added font.css file with all fontface added in that new file and then imported font.css file into style.css file and it worked for me
font.css: @font-face { font-family: 'Tele-Bold'; src: url('../assets/fonts/Tele-Bold.ttf'); } style.css: @import ".assets/fonts/fonts.scss";
Since we are originally using .sass, I had to create _fonts.css in the fonts folder:
@font-face {
font-family: 'Tele-Bold';
src: url('/assets/fonts/Tele-Bold.ttf');
}
then import it into styles.sass by adding:
@import "src/assets/fonts/_fonts"
Using _fonts.sass will throw an error
When deploying as "Angular Elements" we have this issue where every custom angular element has their own "assets" folder with a "fonts" folder.
We have for example one element that is running on http://localhost:4004 and another "parent" that uses this custom element running on http://localhost:4200
This method works fine in the element that is running on http://localhost:4004:
styles.scss
$fontpath: "/assets/fonts/";
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url($fontpath + 'MaterialIcons-Regular.eot'); /* For IE6-8 */
src: local('Material Icons'),
local('MaterialIcons-Regular'),
url($fontpath + 'MaterialIcons-Regular.woff2') format('woff2'),
url($fontpath + 'MaterialIcons-Regular.woff') format('woff'),
url($fontpath + 'MaterialIcons-Regular.ttf') format('truetype');
}
angular.json
"assets": [
"src/assets/fonts"
],
(Thanks @johanroug for this solution
From similar issue: https://github.com/angular/angular-cli/issues/3143
But when using the custom element in the "parent" running on http://localhost:4200 i get these errors:
dialog.js:244 GET http://localhost:4200/assets/fonts/MaterialIcons-Regular.woff2 net::ERR_ABORTED 404 (Not Found)
(anonymous) @ dialog.js:244
Promise.then (async)
_savePreviouslyFocusedElement @ dialog.js:244
attachComponentPortal @ dialog.js:183
_attachDialogContent @ dialog.js:767
open @ dialog.js:645
openDialog @ contact-info.component.ts:101
(anonymous) @ contact-info.component.html:15
handleEvent @ core.js:29587
callWithDebugContext @ core.js:31148
debugHandleEvent @ core.js:30787
dispatchEvent @ core.js:25914
(anonymous) @ core.js:26581
(anonymous) @ platform-browser.js:1678
:4200/assets/fonts/MaterialIcons-Regular.woff:1 GET http://localhost:4200/assets/fonts/MaterialIcons-Regular.woff net::ERR_ABORTED 404 (Not Found)
:4200/assets/fonts/MaterialIcons-Regular.ttf:1 GET http://localhost:4200/assets/fonts/MaterialIcons-Regular.ttf net::ERR_ABORTED 404 (Not Found)
It seems like when we package angular components as custom elements (angular elements) we have this issue.
Any suggestions for nifty hacks to get custom fonts working in angular elements? 😶
This issue has been closed, but I still have problem: What if I import some scss files from third-party libraries and there are rules like background-image: url() in these files? Do I have to modify these relative path to absolute path in my node_modules folder?
@filipesilva I'm having the same issue with this for images referenced using css url, it works fine until I try to run a universal build, a flag to prevent this kind of hashing would be much appreciated, would save hours of work :\
Hi all, this is a pretty old issue that started with a simple report and then accumulated a lot of other reports. Some of these others reports are directly related to the original issue, and for others the only relation is that the problem also involves resources in CSS. I understand the distinction isn't obvious, and often it's not clear until it's investigated for a while.
But either way reporting things on an old closed issue isn't the best way of getting them addressed. It's better to open a new issue with all details necessary, like version numbers and a minimal reproduction. This way it's crystal clear what the issue is, and and anyone can try to fix it.
If you've commented on this issue and are still having problems with the way Angular CLI processes CSS resources, please open a new issue and fill in the details requested in the issue template.
If your fonts are not working
use './assets/ instead of '/assets/
Worked for me
@font-face {
font-family: 'FtMonopol';
src: url('./assets/fonts/FtMonopol-0.21-Medium.woff') format('woff'), /* Pretty Modern Browsers */
url('./assets/fonts/FtMonopol-0.21-Medium.ttf') format('truetype'); /* Safari, Android, iOS */
font-weight:700;
}
@elron I actually had to change from ./assets/ to /assets/ to not get the fonts in the root folder. I find it a strange behaviour from the CLI to copy certain files to the root folder.
Example:
@font-face {
font-family: 'Montserrat';
src: url('/assets/fonts/montserrat-regular.ttf') format('truetype'),
url('/assets/fonts/montserrat-regular.woff') format('woff'),
url('/assets/fonts/montserrat-regular.woff2') format('woff');
font-weight: 400;
}
I'm running the following versions:
Angular: 8.2.0 Angular CLI: 8.2.1
@elron I actually had to change from
./assets/to/assets/to not get the fonts in the root folder. I find it a strange behaviour from the CLI to copy certain files to the root folder.
The issue with this is that it doesn't work when dealing with i18n, because you are using different base-href for each language. Absolute paths do not work when referenced from css files.
I found a temporal workaround for my Angular/Electron app:
If I use absolute import (/assets/fonts), in production mode, Chrome in Electron request the file at file:///assets/fonts..., at the root of the OS.
So I moved @font-face in index.html, the relative import (assets/fonts) work again in dev mode (http://localhost:4200) and in production mode (file://).
To make things worse, before version 8, /assets/fonts/... worked with deploy-url and the end result after post-processing was my-deploy-url/assets/fonts/... but now it just leaves it as is
If your fonts are not working
use
'./assets/instead of'/assets/Worked for me
@font-face { font-family: 'FtMonopol'; src: url('./assets/fonts/FtMonopol-0.21-Medium.woff') format('woff'), /* Pretty Modern Browsers */ url('./assets/fonts/FtMonopol-0.21-Medium.ttf') format('truetype'); /* Safari, Android, iOS */ font-weight:700; }
@ogousa I have given absolute patch in my project
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 300;
src: url('/assets/fonts/open-sans-v15-latin-300.eot');
/* IE9 Compat Modes */
src: local('Open Sans Light'), local('OpenSans-Light'),
url('/assets/fonts/open-sans-v15-latin-300.woff2') format('woff2'),
}
It should work but it did not work now i am getting error at the time of deploy. Because lets say my production IP is 192.168.2.38 project name is AngularProject and fonts are in Assets/Fonts folder but when i build and deploy this then it is giving me error that "open-sans-v15-latin-300.woff2" is not found on this location 192.168.2.38/assets/Fonts/open-sans-v15-latin-300.woff2 but actually its on this location -
192.168.2.38/AngularProject/assets/Fonts/open-sans-v15-latin-300.woff2 so when i manually edit the prod style.css file and change the path to url('./assets/fonts/open-sans-v15-latin-300.woff2') format('woff2') then it works .
Can you please help me i am doing wrong something here?