简体   繁体   English

Next.js + Tailwind 中的自定义字体:没有错误,但字体错误

[英]Custom font in Next.js + Tailwind: no error, but wrong font

In my Next.js (9.4.4) / Tailwind.css (1.4.6) project, I'm using a custom font called SpaceGrotesk.在我的 Next.js (9.4.4) / Tailwind.css (1.4.6) 项目中,我使用了一种名为 SpaceGrotesk 的自定义字体。 To make it work, I put my fonts my public/fonts/spaceGrotesk , and then, I configured my files as follows:为了使它工作,我把我的 fonts 我的public/fonts/spaceGrotesk ,然后,我配置我的文件如下:

// 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.但是,现在我仍然没有得到正确的 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.控制台没有显示警告,检查器似乎说字体加载正确,但仍然使用备用字体(无衬线)而不是 SpaceGrotesk。

What did I do wrong to import my font?导入字体我做错了什么?

In order to integrate your own fonts into your Next project, you do not need another dependency in the form of an npm module.为了将您自己的 fonts 集成到您的 Next 项目中,您不需要 npm 模块形式的其他依赖项。

To get to the font in your globals.css, you have to put the font into the public folder.要获取 globals.css 中的字体,您必须将字体放入公用文件夹中。 Then you integrate the font in the globals.css to give it to the CSS-framework in the tailwind.config.js.然后将字体集成到 globals.css 中,以将其提供给 tailwind.config.js 中的 CSS 框架。 Afterwards, you simply have to add it to the respective element, or you define it globally.之后,您只需将其添加到相应的元素中,或者全局定义它。

globals.css 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 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:多亏了next-fonts package,我终于解决了这个问题:

Step 1:步骤1:

Install next-fonts :安装next-fonts

npm install --save next-fonts

Step 2:第2步:

Import it in next.config.js :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:第 3 步:

Put your files anywhere in the public folder.将您的文件放在public文件夹中的任何位置。 For example, I put my SpaceGrotesk font in public/fonts/spaceGrotesk .例如,我将我的 SpaceGrotesk 字体放在public/fonts/spaceGrotesk中。

Step 4:第4步:

Import them in your CSS using @font-face:使用@font-face 将它们导入到您的 CSS 中:

// 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 .请注意, URL 前面没有/public That was the cause of an issue I had.这就是我遇到问题的原因。

Step 5:第 5 步:

Just use your font like any other:只需像使用其他字体一样使用您的字体:

// tailwind.css

a {
    font-family: "SpaceGrotesk";
}

Step 6 (optional):第 6 步(可选):

You can add the font to your config so you can use the class font-space-grotesk :您可以将字体添加到配置中,以便使用 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".没有“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在 Next.JS 10 及以上版本中,不确定以前的版本,您不需要将../public/fonts/spaceGrotesk/SpaceGrotesk-Regular.woff替换为/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 资产时,不要指定公用文件夹,而只需指定链接,就好像公用文件夹是根文件夹一样。 Static File Serving Static 文件服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM