How to loading custom fonts

Hi there,
I have a static site developed with Vite using custom fonts. The structure is shown below


and my netlify.toml file looks like

[build]
  base    = ""
  publish = "dist"
  command = "npm run build"
  [build.processing]
    skip_processing = false
  [build.processing.css]
    bundle = true
    minify = false
  [build.processing.js]
    bundle = true
    minify = false
  [build.processing.html]
    pretty_urls = true
  [build.processing.images]
    compress = true

Styles, JavaScript and images are transferred over cloudfront.net and correctly loaded. However the fonts are not.
In my css file they are still referred with the local path (i.e., @font-face{font-family:onest;font-weight:700; src:url(assets/fonts/onest/OnestBold1602-hint.woff) ....)

How can I load my custom fonts?

Thanks,
Sig

Hi, to add a custom font to your Netlify project, you can follow these steps:

  1. Include your font files in your project directory (e.g. assets/fonts directory)
  2. In your CSS or SASS files, use the @font-face rule to specify your custom font:
@font-face {
  font-family: '300';
  font-display: fallback;
  src: url('/assets/fonts/NotoSans-Light.woff2') format('woff2');
}
  1. Use the custom font in your CSS by referring to the font-family example:
body {
  font-family: var(--font_300);
}
  1. In your netlify.toml, you can set a caching rule for your font files:
[[headers]]
  for = "/assets/fonts/*"
  [headers.values]
    cache-control = '''
    public,
    max-age=31536000'''

A similar user in the forums mentioned this:

Thanks for the reply. However, adding the headers in my netlify.toml doesn’t seem to fix the issue.

My CSS file looks like

@font-face {
    font-family: 'onest';
    font-weight: bold;
    src: url('assets/fonts/onest/Onest-Bold.woff') format('woff');
}

@font-face {
    font-family: 'onest';
    font-weight: normal;
    src: url('assets/fonts/onest/Onest-Regular.woff') format('woff');
}

and the configuration file

...
[[headers]]
  for = "/assets/fonts/*"
  [headers.values]
    cache-control = '''
    public,
    max-age=31536000'''

Now, in the console I have the following errors

change that to true and remove everything else after that.

also remove this:

and lastly,

change that to /assets/fonts/onest/Onest-Bold.woff

1 Like

Thanks, fonts are now properly loaded.