最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Custom font in Next.js + Tailwind: no error, but wrong font - Stack Overflow

programmeradmin4浏览0评论

In my Next.js (9.4.4) / Tailwind.css (1.4.6) project, I'm using a custom font called SpaceGrotesk. To make it work, I put my fonts my public/fonts/spaceGrotesk, and then, I configured my files as follows:

// next.config.js
module.exports = {
    cssModules: true,
    webpack: (config, options) => {
        config.node = {
            fs: "empty",
        };
        config.module.rules.push({
            test: /\.(png|woff|woff2|eot|ttf|svg)$/,
            use: [
                options.defaultLoaders.babel,
                {
                    loader: "url-loader?limit=100000",
                },
                {
                    loader: "file-loader",
                },
            ],
        });
        return config;
    },
};

/** tailwind.css */
@tailwind base;

@tailwind components;

@tailwind utilities;

@font-face {
    font-family: SpaceGrotesk;
    font-weight: 400;
    font-display: auto;
    src: url(../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("woff");
}
// tailwind.config.js
module.exports = {
    purge: {
        mode: "all",
        content: [
            "./components/**/*.js",
            "./Layout/**/*.js",
            "./pages/**/*.js"
        ],
    },

    important: true,
    theme: {
        extend: {
            fontFamily: {
                paragraph: ["Crimson Text, serif"],
                spaceGrotesk: ["SpaceGrotesk, sans-serif"],
            },
        },
    },
};

I used to have a lot of trouble with import errors displayed on the console, but fixed them all. Now, however, I still don't get the right fonts. The console shows no warning, the inspector seems to say that the font is loaded correctly, but the back-up font (sans-serif) is still used instead of SpaceGrotesk.

What did I do wrong to import my font?

In my Next.js (9.4.4) / Tailwind.css (1.4.6) project, I'm using a custom font called SpaceGrotesk. To make it work, I put my fonts my public/fonts/spaceGrotesk, and then, I configured my files as follows:

// next.config.js
module.exports = {
    cssModules: true,
    webpack: (config, options) => {
        config.node = {
            fs: "empty",
        };
        config.module.rules.push({
            test: /\.(png|woff|woff2|eot|ttf|svg)$/,
            use: [
                options.defaultLoaders.babel,
                {
                    loader: "url-loader?limit=100000",
                },
                {
                    loader: "file-loader",
                },
            ],
        });
        return config;
    },
};

/** tailwind.css */
@tailwind base;

@tailwind components;

@tailwind utilities;

@font-face {
    font-family: SpaceGrotesk;
    font-weight: 400;
    font-display: auto;
    src: url(../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("woff");
}
// tailwind.config.js
module.exports = {
    purge: {
        mode: "all",
        content: [
            "./components/**/*.js",
            "./Layout/**/*.js",
            "./pages/**/*.js"
        ],
    },

    important: true,
    theme: {
        extend: {
            fontFamily: {
                paragraph: ["Crimson Text, serif"],
                spaceGrotesk: ["SpaceGrotesk, sans-serif"],
            },
        },
    },
};

I used to have a lot of trouble with import errors displayed on the console, but fixed them all. Now, however, I still don't get the right fonts. The console shows no warning, the inspector seems to say that the font is loaded correctly, but the back-up font (sans-serif) is still used instead of SpaceGrotesk.

What did I do wrong to import my font?

Share Improve this question edited Jun 23, 2020 at 9:20 Thanh-Quy Nguyen asked Jun 21, 2020 at 17:59 Thanh-Quy NguyenThanh-Quy Nguyen 3,2359 gold badges33 silver badges49 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 13

In order to integrate your own fonts into your Next project, you do not need another dependency in the form of an npm module.

To get to the font in your globals.css, you have to put the font into the public folder. Then you integrate the font in the globals.css to give it to the CSS-framework in the tailwind.config.js. Afterwards, you simply have to add it to the respective element, or you define it globally.

globals.css

@tailwind base;
@tailwind components;
@tailwind utilities;

@font-face {
  font-family: "Custom";
  src: url("/CustomFont.woff2");
}

body {
  @apply font-custom; //if u want the font globally applied
}

tailwind.config.js

module.exports = {
  purge: ["./pages/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}"],
  darkMode: false,
  theme: {
    extend: {
      fontFamily: {
        custom: ["Custom", "sans-serif"]
      }
    }
  }
}

I finally solved the issue thanks to the next-fonts package:

Step 1:

Install next-fonts:

npm install --save next-fonts

Step 2:

Import it in next.config.js:

// next.config.js

const withFonts = require("next-fonts");

module.exports = withFonts({
    webpack(config, options) {
        config.node = {
            fs: "empty",
        };
        config.module.rules.push({
            test: /\.(png|woff|woff2|eot|ttf|svg)$/,
            use: [
                options.defaultLoaders.babel,
                {
                    loader: "url-loader?limit=100000",
                },
                {
                    loader: "file-loader",
                },
            ],
        });
        return config;
    },
});

Step 3:

Put your files anywhere in the public folder. For example, I put my SpaceGrotesk font in public/fonts/spaceGrotesk.

Step 4:

Import them in your CSS using @font-face:

// tailwind.css

@font-face {
    font-family: "SpaceGrotesk";
    font-weight: 400;
    src: url(/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("woff");
}

Note that the URL does not have /public in front of it. That was the cause of an issue I had.

Step 5:

Just use your font like any other:

// tailwind.css

a {
    font-family: "SpaceGrotesk";
}

Step 6 (optional):

You can add the font to your config so you can use the class font-space-grotesk:

// tailwind.config.js

module.exports = {
    // ...The rest of your config here...
    theme: {
        extend: {
            fontFamily: {
                "space-grotesk": ["SpaceGrotesk, sans-serif"],
            },
        },
    },
};

There is no format "font-woff".

Replace

src: url(../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("font-woff");

with

src: url(../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff) format("woff");

In Next.JS version 10 and above, not sure about previous versions, you do not need to replace ../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff with /fonts/spaceGrotesk/SpaceGrotesk-Regular.woff

When serving your static assets from your public folder, you do not to specify the public folder but just the link as if the public folder is the root folder. Static File Serving

发布评论

评论列表(0)

  1. 暂无评论