Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NextJS - Google font is not loading or displaying on the website

Followed the documentation and added a _document.js file with the provided code:

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx)
    return { ...initialProps }
  }

  render() {
    return (
      <Html>
        <Head>
          <link href="https://fonts.googleapis.com/css2?family=Almarai:wght@300;400;700&display=swap" rel="stylesheet" />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

When I use my chrome extension font checker and verify in the inspector, it states that it using the defaults fonts. Any idea how I can get this to work? Might there be some interference with the fact that I'm using tailwindcss?

like image 646
Kristian Abenojar Avatar asked Sep 11 '25 06:09

Kristian Abenojar


2 Answers

I faced this problem a few minutes ago and fixed this by just adding one line of code.

Make sure you've added display: "swap" in your code like this -

const poppins = Poppins({
subsets: ["latin"]
weight: ["100", "200", "400", "700"],
display: "swap",
});

after adding display: "swap", it started working.

like image 87
msadikjowel Avatar answered Sep 13 '25 03:09

msadikjowel


in globals.css

/* Google Font */
@import url('https://fonts.googleapis.com/css2?family=Kurale&display=swap');

/* From Public Directory */
@font-face {
  font-family: 'Kurale';
  src: url('/fonts/Kurale.ttf');
  font-style: medium;
  font-weight: normal;
  font-display: swap;
}

like image 40
Sagar Roy Avatar answered Sep 13 '25 02:09

Sagar Roy