question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cant load custom fonts with nextJS

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

I have been trying to load custom fonts in MUI for my nextJS project. But it errors out. According to the docs we need a loader for it parse custom fonts but I haven’t been able to find any information related to that. How should I configure Next and MUI to load the fonts?

The only workaround I have been able to find is to define the font family through a global css file. But then the fonts don’t work inside components.

Here’s my code for theme.ts

import { createTheme } from '@mui/material';
import proximaRegular from './public/fonts/Proxima-Nova.ttf';
import proximaBlack from './public/fonts/Proxima-Nova-Black.otf';
import proximaSBold from './public/fonts/Proxima-Nova-SBold.ttf';

const theme = createTheme({
  typography: {
    fontFamily: 'ProximaRegular, ProximaBlack, ProximaSBold, Arial, Roboto',
  },
  palette: {
    background: {
      default: '#FFD600',
    },
  },
  components: {
    MuiCssBaseline: {
      styleOverrides: `
              @font-face {
          font-family: 'ProximaRegular';
          font-style: normal;
          font-display: swap;
          font-weight: 400;
          src: url(${proximaRegular}) format('ttf');
          unicodeRange: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF;
        }

        @font-face {
          font-family: 'ProximaBlack';
          font-style: normal;
          font-display: swap;
          font-weight: 400;
          src: url(${proximaBlack}) format('otf');
          unicodeRange: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF;
        }

        @font-face {
          font-family: 'ProximaSBold';
          font-style: normal;
          font-display: swap;
          font-weight: 400;
          src: url(${proximaSBold}) format('ttf');
          unicodeRange: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF;
        }
      `,
    },
  },
});

export default theme;

and here’s the error I’m getting during the process

error - ./public/fonts/Proxima-Nova-Black.otf
Module parse failed: Unexpected character '' (1:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)

Expected behavior 🤔

The custom fonts should be loaded

Steps to reproduce 🕹

Steps:

  1. import the custom fonts inside theme.ts
  2. Create the appropriate style Overrides for them
  3. Build the app.

Context 🔦

No response

Your environment 🌎

`npx @mui/envinfo`
System:
    OS: Windows 10 10.0.19042
  Binaries:
    Node: 14.17.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.11 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.13 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: Not Found
    Edge: Spartan (44.19041.1023.0), Chromium (91.0.864.67)
  npmPackages:
    @emotion/react: ^11.7.0 => 11.7.0
    @emotion/styled: ^11.6.0 => 11.6.0
    @mui/base:  5.0.0-alpha.59
    @mui/material: ^5.2.3 => 5.2.3
    @mui/private-theming:  5.2.3
    @mui/styled-engine:  5.2.0
    @mui/system:  5.2.3
    @mui/types:  7.1.0
    @mui/utils:  5.2.3
    @types/react: ^17.0.37 => 17.0.37
    react: 17.0.2 => 17.0.2
    react-dom: 17.0.2 => 17.0.2
    typescript: ^4.5.2 => 4.5.2

I'm using Chrome v96

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
Altair2169commented, Dec 13, 2021

I figured out the problem, I was defining the formats of the font incorrectly, which was why they weren’t loading. For others here’s how this’ll work

in your global css define the fonts

@font-face {
  font-family: 'ProximaRegular';
  font-style: normal;
  font-display: swap;
  font-weight: 400;
  src: url('/fonts/Proxima-Nova.ttf') format('truetype');
  unicoderange: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
    U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
    U+FEFF;
}

@font-face {
  font-family: 'ProximaBlack';
  font-style: normal;
  font-display: swap;
  font-weight: 400;
  src: url('/fonts/Proxima-Npva-Black.otf') format('opentype');
  unicoderange: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
    U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
    U+FEFF;
}

@font-face {
  font-family: 'ProximaSBold';
  font-style: normal;
  font-display: swap;
  font-weight: 400;
  src: url('/fonts/Proxima-Nova-SBold.ttf') format('truetype');
  unicoderange: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
    U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
    U+FEFF;
}

and then refer them inside your theme object as how you do for regular fonts

Thanks

1reaction
vileencommented, Feb 2, 2022

@Altair2169 one note though, there is no such thing as unicoderange, try unicode-range, also I don’t think you should every font as font-weight: 400, it could break some third party library if you’d use it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cant load custom fonts with nextJS · Issue #30185 - GitHub
I have been trying to load custom fonts in MUI for my nextJS project. But it errors out. According to the docs we...
Read more >
Basic Features: Font Optimization - Next.js
@next/font will automatically optimize your fonts (including custom fonts) and remove external network requests for improved privacy and performance. 🎥 Watch: ...
Read more >
How to load custom fonts in NextJS!? - Reddit
My solution would be: put your font files into public folder in order to serve them as static file. For ex: public/font1.ttf. Then...
Read more >
How to import custom fonts in Next JS? - Stack Overflow
I cannot seem to import a custom font family locally using CSS. If I try to add a style.css in the same repository...
Read more >
🖋Adding Fonts in Next.js (local fonts along with styled ...
Step 1 - Creating a Next.js application · Step 2 - Adding custom fonts (local) · Step 3 - Preloading these fonts in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found