【问题标题】:Font on production build different from font in development build Next.js生产版本中的字体与开发版本 Next.js 中的字体不同
【发布时间】:2021-11-14 20:25:31
【问题描述】:

从下图中可以看出,我的 Next.js 网页的生产构建和开发构建中的字体不同。在我运行 npm run buildnpm start 来测试生产版本后,蒙特塞拉特字体不再出现在我的网页上。

这就是我将字体导入globals.scss 文件的方式。如果我做错了什么,请告诉我:

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

@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;700&display=swap');

html,
body {
  padding: 0;
  margin: 0;
  font-family: 'Montserrat', sans-serif;
  background-color:  hsl(265, 3%, 53%);
  color: black;
  &:before{
    content:'';
    content: "";
    width: 100%;
    height: 100vh;
  }
}

生产构建的屏幕截图:

开发构建的屏幕截图:

【问题讨论】:

    标签: css next.js


    【解决方案1】:

    要将字体添加到生产中的整个 Next.js 应用程序,您可以使用自定义 Document。这里有link_1link_2,你可以阅读一下。

    示例代码 sn-p。 _document.js 文件。

    import Document, { Html, Head, Main, NextScript } from "next/document";
    
    class MyDocument extends Document {
      render() {
        return (
          <Html lang="en">
            <Head>
              <link rel="preconnect" href="https://fonts.gstatic.com" />
              <link
                href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;700;900&display=swap"
                rel="stylesheet"
              />
            </Head>
            <body>
              <Main />
              <NextScript />
            </body>
          </Html>
        );
      }
    }
    
    export default MyDocument;
    

    globals.css 文件中的 CSS。

    body {
      font-family: "Poppins", sans-serif;
      line-height: 1.5;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-18
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      • 2019-11-10
      • 2020-11-23
      • 2023-03-25
      相关资源
      最近更新 更多