How to Load CSS asynchronous in NextJS - next.js

I want my global CSS file to load asynchronous, i.e without render-blocking. My page is waiting for FCP until it gets CSS loaded.
So, How can I achieve this in next js.
What I am getting :
<link rel="stylesheet" href="/_next/static/css/eb4e3a560474a24ae771.css" data-n-g="">
What is Expected :
<link rel="stylesheet" href="/_next/static/css/eb4e3a560474a24ae771.css" data-n-g="" media="all" onload="this.media='all'">

NextJS documentation on Font Optimization https://nextjs.org/docs/basic-features/font-optimization says that they transform provided <link> to preconnect with added style:
// Before
<link
href="https://fonts.googleapis.com/css2?family=Inter&display=optional"
rel="stylesheet"
/>
// After
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<style data-href="https://fonts.googleapis.com/css2?family=Inter&display=optional">
#font-face{font-family:'Inter';font-style:normal...
</style>
Now, you just need to adapt GET parameter display=[value] to adapt to font-display standard https://developer.mozilla.org/en-US/docs/Web/CSS/#font-face/font-display. The list is:
/* Keyword values */
font-display: auto;
font-display: block;
font-display: swap;
font-display: fallback;
font-display: optional;
Many are for mostly non-blocking (small block) display. All of them are explained in the Mozilla link, but the one I prefer, font-display: swap that gives extremely small block for font to load and if not in time, infinite swap period to change the font everywhere when it's loaded:
swap
Gives the font face an extremely small block period and an
infinite swap period.
Font block period
If the font face is not loaded, any element
attempting to use it must render an invisible fallback font face. If
the font face successfully loads during this period, it is used
normally.
Font swap period
If the font face is not loaded, any element
attempting to use it must render a fallback font face. If the font
face successfully loads during this period, it is used normally.
This way we can chose what fits better for us. If it still blocks a lot, I guess you'll have to put the link at the bottom of page.

Related

Pre-loading fonts

Do i need to use both rel="preload" and font-display="swap". Since rel="preload" prefetches the required font, is it necessary to add font-display attribute too?
<link rel="preload stylesheet" href="https://fonts.googleapis.com/css2?family=Magra&display=swap" as="font">
It depends on the intended result.
With font-display: swap, text is immediately rendered using the fallback typeface specified in your font stack while the request is made for your font. The fallback is whatever system font comes next in your font stack as listed in your font-family property.
Once the font is downloaded, it will be swapped in for the fallback.
This is just one option and you don't need to use it. For example, the immediate rendering of text when using font-display: swap creates a flash of unstyled text (FOUT) for the user and possible shifts in layout when the fonts swap, which can be undesirable.
You could opt for font-display: fallback, which attempts to account for this by hiding any text on screen for the first 100ms, optimistically hoping that your font downloads in that time frame.
If your font downloads within the first 100ms, it gets applied to the first visible text shown to the user (no FOUT).
If not, at 100ms the text gets rendered with the fallback typeface and the same pattern as font-display: swap resumes with your font getting swapped in once it eventually downloads.

Loading font-family into the web application will display the default one for the first 2 seconds

I'm trying to load the Ubuntu-Bold font-family to my web application (ASP.NET CORE API + Angular). The Ubuntu-Bold.ttf file is located on assets/fonts folder and I also added #font-face { font-family: 'Ubuntu-Bold'; src: url(assets/fonts/Ubuntu-Bold.ttf); } body { margin: 0; font-family: 'Ubuntu-Bold'; } in to the styles.scss file. The font-family its applied to my application but after 2 seconds after the application started running.
Default font-family appears first at the beginning of application:
After 2 second the desired font-family its applied:
As you can see there its a big difference between this two fonts so I'm trying to find a solution in order to apply my font Ubuntu-Bold before the application its displayed in order to not see that change of family fonts after 2 seconds after application started.
you can use preload on your link tag, it tells the browser to load this stylesheet before the main part of your web app.
it should look something like that:
<link rel="preload" href="your/path/xxx.woff" as="font" type="font/woff" crossorigin>
or if you want your entire styles to load first
<link rel="preload" href="style.css" as="style">
you can read about it here

FontAwesome icons doesnt show up after being downloaded?(works with URL, doesnt with SRC)

I've been using fontawesome for one of my web projects and it works fine when I use the CDN link as its stylesheet but when I want to have everything locally and download FontAwesome(zip file) from the website, it becomes messy and shows multiple icons as some kind of bad zoom problem over its png file of icons.
the only change is
this
<link href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
into this
<link src="assets/css/font-awesome/css/font-awesome.css" rel="stylesheet">
Again, the problem isnt that its not working, its how it shows multiple icons.
Assure you have downloaded the full package with correct version from the FontAwesome website and use a correct link, such as:
<link href="assets/css/font-awesome/css/font-awesome.css" rel="stylesheet" >
Reset your browsers cache.
Assure that the or element you use, uses the FontAwesome font family. For example:
<i class="fa-pencil" title="Edit"></i>
but
<i class="fa fa-pencil" title="Edit"></i>
It won't work if you have something as the following in your CSS:
* {
font-family: 'Josefin Sans', sans-serif !important;
}
If you are using IE, just dont...
If this doesn't work, just give it one hour or two(trust me sometimes browser plays jokes on itself), if problem was persistant, check https://github.com/FortAwesome/Font-Awesome/wiki/Troubleshooting

Google fonts and font-display

We are currently loading a couple of fonts on our site from google fonts using the following link
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons%7CRoboto:300,300i,400,400i,500,700&display=block">
As you can see, at the end of the query string, we have display=block to stop the font from rendering before it's loaded (as it adds font-display: block). However, this adds block to all declarations of every font face. Is there a way to only add this to the material icons font without having to make 2 separate requests?

How does CSS font-weight work with emojis?

I'm blocked by a question, on some Android device, I type some emoji and save them to database.Then load them and display in html page, but the emoji is invisible if set a font-weight: bold.If set font-weight: normal can display.
How does css font-weight work?
How does the browser parse CSS when it sees font-weight:bold or font-weight:normal?
System will call a bold font file (e.g., see Google Fonts which will list different weights of a font family) and if you don't have that font file on your device, it will fail to display the characters (actually for regular characters system tend to load a similar font in this case, but emoji characters are not regular).
You can include webfonts and contain the bold family in <head> section. E.g.,
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>

Resources