Skip to content

Instantly share code, notes, and snippets.

@DonHuskini
Created August 8, 2022 15:37
Show Gist options
  • Save DonHuskini/6e4b31eccbd6d59fe008ecab7bd21c6b to your computer and use it in GitHub Desktop.
Save DonHuskini/6e4b31eccbd6d59fe008ecab7bd21c6b to your computer and use it in GitHub Desktop.
Load custom font NextJS
1. Add the font on the /public folder. Just .woff2 extension is good enough https://caniuse.com/?search=woff2
1.1. If you have a problem regarding vertical alignment, use https://transfonter.org/ and select "fix vertical metrics"
2. Import the font with CSS @font-face like below:
```css
@font-face {
font-family: 'Bebas Neue';
src: url('/fonts/BebasNeue/BebasNeue-Regular.woff2') format('woff2');
font-weight: 400;
font-display: swap;
}
```
3. Optimize the font by preloading it. Add the snipet on _Document file:
```jsx
<link
rel="preload"
href="/fonts/BebasNeue/BebasNeue-Regular.woff2"
as="font"
crossOrigin=""
/>
```
4. Not needed to add any caching mecanism. Nextjs takes care of it for static assets. More details here: https://nextjs.org/docs/going-to-production#caching
5. Make sure to test it on different browsers (safari, chrome, firefox and edge) with https://app.lambdatest.com/console/realtime/browser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment