NextJS不会显示Google字体

ezykj2lf  于 2023-11-18  发布在  Go
关注(0)|答案(1)|浏览(221)

我有最困难的时间让谷歌字体与NextJS工作。
我从一个名为typography.ts的文件开始,在这里我使用nextjs字体优化器导入了一个Google字体:

  1. import { Noto_Serif_Hebrew, Roboto } from "next/font/google";
  2. export const roboto = Roboto({
  3. weight: ["300", "400", "500", "700"],
  4. subsets: ["latin"],
  5. display: "swap",
  6. });
  7. export const notoSerifHebrew = Noto_Serif_Hebrew({
  8. weight: ["400", "700"], // Add the weights you need
  9. subsets: ["latin", "latin-ext", "hebrew"],
  10. display: "swap",
  11. });

字符串
然后将这些字体导入到另一个名为commonStyles.ts的文件中

  1. import { roboto, notoSerifHebrew } from "../assets/typography";
  2. export const hebrewFamily = `${notoSerifHebrew.className}, "Times New Roman", sans-serif`;
  3. export const englishFamily = `${roboto.className}, Arial, sans-serif`;
  4. const commonStyles = {
  5. typography: {
  6. fontFamily: englishFamily, // This will be the default font family
  7. hebrewFamily: hebrewFamily,
  8. englishFamily: englishFamily,
  9. },
  10. components: {
  11. MuiCssBaseline: {
  12. styleOverrides: {
  13. a: {
  14. textDecoration: "none",
  15. color: "inherit",
  16. },
  17. },
  18. },
  19. },
  20. };
  21. export default commonStyles;


我也有一个themeExtensions文件:

  1. declare module "@mui/material/styles" {
  2. interface Typography {
  3. hebrewFamily: string;
  4. englishFamily: string;
  5. }
  6. interface TypographyOptions {
  7. hebrewFamily?: string;
  8. englishFamily?: string;
  9. }
  10. interface Theme {
  11. prime: {
  12. [key: string]: string;
  13. };
  14. accent: {
  15. [key: string]: string;
  16. };
  17. transparency: {
  18. [key: string]: string;
  19. };
  20. }
  21. // allow configuration using `createTheme`
  22. interface ThemeOptions {
  23. prime?: {
  24. [key: string]: string;
  25. };
  26. accent?: {
  27. [key: string]: string;
  28. };
  29. transparency?: {
  30. [key: string]: string;
  31. };
  32. }
  33. }


现在,我有了一个浅色和深色主题--我将themeExtensioncommonStyles都导入到这两个主题中:

  1. // lightTheme.ts
  2. import { createTheme } from "@mui/material/styles";
  3. import "../configurations/themeExtensions";
  4. import { colors } from "../assets/colors";
  5. import { amber, blue, green, red } from "../assets/tailwind-colors";
  6. import commonStyles from "../configurations/commonStyles";
  7. const {
  8. light: { prime },
  9. } = colors;
  10. const lightTheme = createTheme({
  11. ...commonStyles,
  12. ...colors.light,
  13. palette: {
  14. mode: "light",
  15. ...
  16. // darkTheme.ts


从“@mui/material/styles”中导入{主题};导入“../configurations/themeExtensions”;
从“../assets/colors”导入{ colors };从“../assets/tailwind-colors”导入{ amber,blue,绿色,red };从“../configurations/commonStyles”导入commonStyles;
const { dark:{ prime },} =颜色;
const darkTheme ={. commonStyles,. colors.dark,palette:{ mode:“dark”,.

  1. Now, at this point, I would expect to be able to use my two Google fonts. But, I can't. For starters, I would think that the default font would be Roboto. But, it is not. It is whatever the fallback is. Right now, it is Arial. If I switch it to Times New Roman, then it is Times New Roman.
  2. Furthermore, I can't even switch it directly. `sx={{fontFamily: "Roboto" }}` won't do anything.
  3. Any idea why this doesn't work?

alen0pnh

alen0pnh1#

CORS和安全标头:如果您在生产环境中遇到问题,请确保您的服务器已配置为允许Google Fonts域的CORS(跨域资源共享)。此外,请检查安全标头(例如,内容安全策略),以确保它们允许加载外部字体。
指纹识别和缓存:如果您的应用程序使用资产指纹或具有积极的缓存设置,请确保字体URL已正确更新且未缓存。
浏览器缓存:有时,字体可能会缓存在您的浏览器中。请尝试清除浏览器缓存,然后重新加载页面。
Google Fonts CDN状态:有时,Google Fonts可能会遇到临时中断或问题。您可以检查Google Fonts状态页面(https://status.google.com),以查看是否存在任何持续的问题。

相关问题