11

In main.scss I load local fonts from assets/styles/fonts folder:

@font-face {
  font-family: 'Opensans-Bold';
  font-style: normal;
  src: local('Opensans-Bold'), url(./fonts/OpenSans-Bold.ttf) format('truetype');
}
@font-face {
  font-family: 'Opensans-Light';
  font-style: normal;
  src: local('Opensans-Light'), url(./fonts/OpenSans-Light.ttf) format('truetype');
}

then in vite.config I load main.scss:

css: {
  preprocessorOptions: {
    scss: {
      additionalData: `@import "@/assets/styles/main.scss";`
    }
  }
},

but all css from main.scss is applied except fonts, I get error:

downloadable font: download failed (font-family: "Opensans-Bold" style:normal weight:400 stretch:100 src index:1): status=2152398850 source: http://localhost:3000/fonts/OpenSans-Bold.ttf

Am I on right track or I need some other approach (similar works with Vue-CLI)?

1
  • 2
    I have the same issue. The path seems nothing to do with it. In both cases - @/assets, ../assets - fonts do not load randomly when I refresh the page. But on production, everything seems fine. I believe this issue is related to the Vite dev server.
    – vovchisko
    Dec 23, 2021 at 11:57

2 Answers 2

28

That was the right way, the solution is the relative path so:

src: local('Opensans-Bold'), url(@/assets/styles/fonts/OpenSans-Bold.ttf) format('truetype');

and setting alias in vite.config.js :

resolve: {
  alias: {
    '@': path.resolve(__dirname, 'src'),
  }
}
6
  • Did you configure an alias for this? Jan 21, 2022 at 21:02
  • hmm, how does the @ work then? I wonder if it's set by default. I have a similar problem and haven't managed to make iit work on react. Jan 24, 2022 at 19:47
  • @Juan De la Cruz ahhhh sorry mate my bad, I updated my answer take a look pls Jan 24, 2022 at 19:54
  • mmm, I though I had done a config similar and it didn't work, but yours actually did. Anyways, it's weird that the relative path doesn't work and that we have to resort to aliases. Jan 24, 2022 at 23:01
  • That alias fixed everything! Relative or absolute, in resources/ or in in public/fonts, Vite wouldn't process the URLs ¯_(ツ)_/¯ Dec 3, 2022 at 2:23
2

I was able to make this work by simply put the fonts on a public folder on root.

like:

public/.**.ttf
src/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.