Repository navigation
|
I would like to ask what is the optimal way to use self-hosted fonts. In the example I'm going through, I am going to use Inter (a variable font) from Google Fonts. I have taken some help from https://github.com/leerob/leerob.io My question is not about using fonts from Google's CDN (there is a good article about that), rather it is about self-hosting them: downloading and keeping them in your There is also official documentation on font optimization, but that will still use the fonts hosted on Google's servers. I would like some opinions on these steps, and what I can do to further optimize them: 1. Download fontSince I'm using Inter from Google Fonts, I decided I want the 200, 300, .. to 900 weight variants, so I open https://fonts.googleapis.com/css2?family=Inter:wght@200..900 in my browser. I scroll down to /* latin */
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 200 900;
src: url(https://fonts.gstatic.com/s/inter/v3/UcC73FwrK3iLTeHuS_fvQtMwCp50KnMa1ZL7W0Q5nw.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}I open the In my site's CSS file, I paste the /* styles/font.css */
@font-face {
font-family: "Inter";
font-style: normal;
font-weight: 200 900;
font-display: optional;
/* reference to local file */
src: url(/fonts/inter-var.woff2) format("woff2");
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}2. Preload fontWhen you run a lighthouse test, you will see: To fix this, add the following to <link
rel="preload"
href="/fonts/inter-var.woff2"
as="font"
type="font/woff2"
crossOrigin="anonymous"
/>This snippet is from https://leerob.io/blog/fonts. 3. Reference the font in your CSSI'm using Tailwind CSS, so this is what I've done: // tailwind.config.js
const defaultTheme = require("tailwindcss/defaultTheme");
module.exports = {
theme: {
extend: {
fontFamily: {
sans: ["Inter", ...defaultTheme.fontFamily.sans],
...
...4. CacheIn (I'm not entirely sure if this is the right way) // next.config.js
module.exports = {
async headers() {
return [
{
source: '/fonts/inter-var.woff2',
headers: [
{
key: 'Cache-control',
value: 'public, immutable, max-age=31536000',
},
],
},
]
},
}So is this way correct? I was wondering if there are any changes I should make to improve it. |
Replies: 8 comments 12 replies
|
I am also hosting my own fonts in /public/fonts, and have the same questions. What did you end up going with... from the actual fonts to caching? |
|
I have the same doubt. I'm currently using the same method you described. |
|
Hi Im currently using same approach but my font isnt working. Copy paste solution from leerob ;/ Anyone has idea why fonts are not loaded properly? I see them only on my local devmachine where they are installed. |
|
does anyone know if we can use wildcards for a folder in public that contains multiple woff2 files?
<Head>
<link
href='/hco_fonts/woff2/Gotham-Black_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-BlackItalic_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-Bold_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-BoldItalic_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-Book_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-BookItalic_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-Light_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-LightItalic_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-Medium_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-MediumItalic_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-Thin_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-ThinItalic_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-Ultra_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-UltraItalic_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-XLight_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
href='/hco_fonts/woff2/Gotham-XLightItalic_Web.woff2'
rel='preload'
as='font'
type='font/woff2'
crossOrigin='anonymous'
/>
<link
rel='prefetch'
type='video/mp4'
href='/videos/Cortina_Selphie.mp4'
crossOrigin='anonymous'
media='(autoplay)'
/>
<link href='/meta/favicon.ico' rel='shortcut icon' />
<link href='/meta/site.webmanifest' rel='manifest' />
<link
rel='apple-touch-icon'
sizes='180x180'
href='/meta/apple-touch-icon.png'
/>
<link
rel='icon'
type='image/png'
sizes='16x16'
href='/meta/favicon-16x16.png'
/>
<link
rel='icon'
type='image/png'
sizes='32x32'
href='/meta/favicon-32x32.png'
/>
<link
rel='mask-icon'
href='/meta/safari-pinned-tab.svg'
color='#5bbad5'
/>
<meta name='msapplication-TileColor' content='#da532c' />
<meta name='theme-color' content='#ffffff' />
<meta name='msapplication-config' content='/meta/browserconfig.xml' />
<meta
name='facebook-domain-verification'
content='7c23wx4wuh3cnsatzfmekou1yallec'
/>
<meta
name='google-site-verification'
content='zIWPD8byBUQ5GCoLDvkkwes7U1mhFIp18mnf5RxSz58'
/>
<meta name='yandex-verification' content='c2de378ff0e25eeb' />
</Head>
@layer base {
/*
Copyright (C) Hoefler & Co.
This software is the property of Hoefler & Co. (H&Co).
Your right to access and use this software is subject to the
applicable License Agreement, or Terms of Service, that exists
between you and H&Co. If no such agreement exists, you may not
access or use this software for any purpose.
This software may only be hosted at the locations specified in
the applicable License Agreement or Terms of Service, and only
for the purposes expressly set forth therein. You may not copy,
modify, convert, create derivative works from or distribute this
software in any way, or make it accessible to any third party,
without first obtaining the written permission of H&Co.
For more information, please visit us at http://typography.com.
*/
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-Thin_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-Thin_Web.woff') format('woff');
font-weight: 100;
font-style: normal;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-ThinItalic_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-ThinItalic_Web.woff') format('woff');
font-weight: 100;
font-style: italic;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-XLight_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-XLight_Web.woff') format('woff');
font-weight: 200;
font-style: normal;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-XLightItalic_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-XLightItalic_Web.woff') format('woff');
font-weight: 200;
font-style: italic;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-Light_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-Light_Web.woff') format('woff');
font-weight: 300;
font-style: normal;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-LightItalic_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-LightItalic_Web.woff') format('woff');
font-weight: 300;
font-style: italic;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-Book_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-Book_Web.woff') format('woff');
font-weight: 400;
font-style: normal;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-BookItalic_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-BookItalic_Web.woff') format('woff');
font-weight: 400;
font-style: italic;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-Medium_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-Medium_Web.woff') format('woff');
font-weight: 500;
font-style: normal;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-MediumItalic_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-MediumItalic_Web.woff') format('woff');
font-weight: 500;
font-style: italic;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-Bold_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-Bold_Web.woff') format('woff');
font-weight: 700;
font-style: normal;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-BoldItalic_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-BoldItalic_Web.woff') format('woff');
font-weight: 700;
font-style: italic;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-Black_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-Black_Web.woff') format('woff');
font-weight: 800;
font-style: normal;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-BlackItalic_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-BlackItalic_Web.woff') format('woff');
font-weight: 800;
font-style: italic;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-Ultra_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-Ultra_Web.woff') format('woff');
font-weight: 900;
font-style: normal;
}
@font-face {
font-family: 'HCo Gotham';
src: url('/hco_fonts/woff2/Gotham-UltraItalic_Web.woff2') format('woff2'),
url('/hco_fonts/woff/Gotham-UltraItalic_Web.woff') format('woff');
font-weight: 900;
font-style: italic;
}
//....
}
module.exports = {
//...
theme: {
extend: {
fontFamily: {
gothamBlack: ['Gotham-Black_Web', ...defaultTheme.fontFamily.sans],
gothamBlackItalic: [
'Gotham-BlackItalic_Web',
...defaultTheme.fontFamily.sans
],
gothamBold: ['Gotham-Bold_Web', ...defaultTheme.fontFamily.sans],
gothamBoldItalic: [
'Gotham-BoldItalic_Web',
...defaultTheme.fontFamily.sans
],
gothamBook: ['Gotham-Book_Web', ...defaultTheme.fontFamily.sans],
gothamBookItalic: [
'Gotham-BookItalic_Web',
...defaultTheme.fontFamily.sans
],
gothamLight: ['Gotham-Light_Web', ...defaultTheme.fontFamily.sans],
gothamLightItalic: [
'Gotham-LightItalic_Web',
...defaultTheme.fontFamily.sans
],
gothamMedium: ['Gotham-Medium_Web', ...defaultTheme.fontFamily.sans],
gothamMediumItalic: [
'Gotham-MediumItalic_Web',
...defaultTheme.fontFamily.sans
],
gothamThin: ['Gotham-Thin_Web', ...defaultTheme.fontFamily.sans],
gothamThinItalic: [
'Gotham-ThinItalic_Web',
...defaultTheme.fontFamily.sans
],
gothamUltra: ['Gotham-Ultra_Web', ...defaultTheme.fontFamily.sans],
gothamUltraItalic: [
'Gotham-UltraThin_Web',
...defaultTheme.fontFamily.sans
],
gothamXLight: ['Gotham-XLight_Web', ...defaultTheme.fontFamily.sans],
gothamXLightItalic: [
'Gotham-XLightItalic_Web',
...defaultTheme.fontFamily.sans
]
},
//...
}
}
}
// @ts-check
/**
* @type {import('next').NextConfig}
**/
module.exports = withBundleAnalyzer({
webpack: (config, { analyze = ANALYZE ?? '' }) => {
if (analyze) {
config.plugins.push(new WebpackBundleSizeAnalyzerPlugin('stats.txt'));
}
config.module.rules.push({
test: /\.ya?ml$/,
type: 'json',
use: 'yaml-loader'
});
return config;
},
async headers() {
return [
{
source: '/(.*)',
headers: [
{
key: 'X-Content-Type-Options',
value: 'nosniff'
},
{
key: 'X-Frame-Options',
value: 'DENY'
},
{
key: 'X-XSS-Protection',
value: '1; mode=block'
},
{
key: 'Strict-Transport-Security',
value: 'max-age=31536000; includeSubDomains; preload '
},
{
key: 'Referrer-Policy',
value: 'origin-when-cross-origin'
},
{
key: 'X-DNS-Prefetch-Control',
value: 'on'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-Black_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-BlackItalic_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-Bold_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-BoldItalic_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-Book_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-BookItalic_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-Light_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-LightItalic_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-Medium_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-MediumItalic_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-Thin_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-ThinItalic_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-Ultra_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-UltraItalic_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-XLight_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/hco_fonts/woff2/Gotham-XLightItalic_Web.woff2',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
},
{
source: '/videos/Cortina_Selphie.mp4',
headers: [
{
key: 'Cache-Control',
value: 'public, max-age=31536000, immutable'
}
]
}
];
},
experimental: { esmHeaders: true },
sourceMaps: {
productionBrowserSourceMaps: true
},
images: {
domains: [
'secure.gravatar.com',
'en.gravatar.com',
'gravatar.com',
'cortinahealth.com',
'wpengine.com',
'getcortina.com',
'www.getcortina.com',
'cortinaai.wpengine.com',
'cortinacare.com',
'avatars.githubusercontent.com',
'github.githubassets.com',
'api.github.com',
'dev-to-uploads.s3.amazonaws.com',
'repository-images.githubusercontent.com',
'i0.wp.com',
'i1.wp.com',
'i2.wp.com',
'www.facebook.com'
]
},
future: {
strictPostcssConfiguration: true
},
reactStrictMode: true,
i18n: {
locales: ['en-US'],
defaultLocale: 'en-US',
domains: [
{
domain: 'www.getcortina.com',
defaultLocale: 'en-US'
}
]
}
});
{
"cleanUrls": true,
"trailingSlash": false,
"headers": [
{
"source": "/hco_fonts/woff2/Gotham-Black_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-BlackItalic_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-Bold_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-BoldItalic_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-Book_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-BookItalic_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-Light_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-LightItalic_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-Medium_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-MediumItalic_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-Thin_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-ThinItalic_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-Ultra_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-UltraItalic_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-XLight_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/hco_fonts/woff2/Gotham-XLightItalic_Web.woff2",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/videos/Cortina_Selphie.mp4",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "/(.*)",
"headers": [
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-Frame-Options",
"value": "DENY"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=31536000; includeSubDomains; preload"
},
{
"key": "Referrer-Policy",
"value": "origin-when-cross-origin"
},
{
"key": "X-DNS-Prefetch-Control",
"value": "on"
}
]
}
]
}
|
|
This is not an answer to the question how to optimally self-host fonts with next.js. I would be very interested in knowing that too. In the meantime I will give https://fontsource.org/docs/introduction a try. Github link: https://github.com/fontsource/fontsource There is also https://github.com/varld/fontless but the project seems abandoned. |
|
New in Next.js 13:
|
|
How does this optimize self hosted custom fonts that are in the public
folder?
…On Sun, Nov 6, 2022, 4:32 PM Lee Robinson ***@***.***> wrote:
New in Next.js 13:
@next/font <https://nextjs.org/docs/api-reference/next/font> will
automatically optimize your fonts (including custom fonts) and remove
external network requests for improved privacy and performance.
https://nextjs.org/docs/basic-features/font-optimization
—
Reply to this email directly, view it on GitHub
<#25389 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABTB7SOALC7RX4XKBBWF5K3WHA5ZHANCNFSM45MYBVIA>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
|
I am using the updated NextJS font and I have made a font file and exported all my local font but how can I use them inside my sass files as I set the variable but my sass file is unable to read it for some reason its local to the component even though I have import the font into that file. ` export const ProcrastingPixel = localFont({
^ The above doesn't work and there is no simple way on the internet to explain it |

New in Next.js 13:
https://nextjs.org/docs/basic-features/font-optimization