'fonts are not saved on deploying on vercel next js

What I see locally:

enter image description here

And what I see on deploying:

enter image description here

Even a layout doesn't save fonts. It is a dynamic page though (/profile/[id].jsx) On building I don't use next export. Ofc I also use className, .module.css files. The bug works only on this dynamic page. Any ideas?



Solution 1:[1]

Use a custom document to link the fonts. This way your fonts are available everywhere in the app. They are loaded even before the styles to avoid font jump when the styles load.

The _document.js file:

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

export default class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head>
          {/* link your fonts here */}
          <link rel="preconnect" href="https://fonts.googleapis.com" />
          <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
          <link
            href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;500;600;700;800;900&display=swap"
            rel="stylesheet"
          />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

Read more on custom document here at well explained Vercel Docs. I hope it helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amit