September 29, 2024

Md. Saad

Font optimization is a crucial aspect of web development that focuses on enhancing the performance and visual appeal of web fonts. By implementing various techniques, developers can ensure that fonts load quickly and render smoothly, providing a better user experience. However, these processes are not easy. Thanks to Next.js for its next/font module, which simplifies these hassles for developers.
Next.js for font optimization offers numerous benefits. It includes an automatic font optimization at build time, which improves performance metrics like First Contentful Paint (FCP) and Largest Contentful Paint (LCP). The built-in next/font package allows for self-hosting fonts, reducing latency and enhancing privacy. Next.js also supports the font-display property to prevent layout shifts, and it enables subsetting and the use of variable fonts for better performance. These features collectively enhance SEO and provide a smoother user experience, making Next.js a powerful tool for managing and optimizing web fonts.
Before setting up the GitHub Actions workflow, the first and foremost step is to create a Next.js App. Use the following command to create a next.js app.
npx create-next-app@latest app-nameAs it is not an article on creating the next.js app, we will not discuss it in detail. If you want to read it in detail follow the link.
Next.js automatically self-hosts Google Fonts. It is also served from the domain where the site is deployed. So no requests are sent to Google by the browser. To get started, import the Google font from next/font/google as a function. For using a variable font, we don't need to specify the font weight. Otherwise, we have to specify weights.
Example of a variable font:
import { Inter } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={inter.className}>
<body>{children}</body>
</html>
)
}Example of a non-variable font:
import { Roboto } from 'next/font/google'
// If loading a variable font, you don't need to specify the font weight
const roboto= Roboto({
subsets: ['latin'],
display: 'swap',
weight: '400',
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={roboto.className}>
<body>{children}</body>
</html>
)
}For a non-variable font, we can set multiple weights using an array.
import { Roboto } from 'next/font/google'
// If loading a variable font, you don't need to specify the font weight
const roboto= Roboto({
subsets: ['latin'],
display: 'swap',
weight: ['400', '700'],
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={roboto.className}>
<body>{children}</body>
</html>
)
} Note: We have to use an underscore (_) for font names with multiple words. E.g. Roboto Mono should be imported as Roboto_Mono.
We can multiple Google fonts also in our Next.js Project. There are two approaches to add multiple fonts in Next Js. Let’s discuss them.
The first method is to write a utility function that imports, exports, and applies a font's className as needed. This guarantees that the font is only loaded whenever it is generated on the site.
import { Inter, Roboto_Mono } from 'next/font/google'
export const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export const roboto_mono = Roboto_Mono({
subsets: ['latin'],
display: 'swap',
})import { inter } from './fonts'
export default function Layout({ children }) {
return (
<html lang="en" className={inter.className}>
<body>
<div>{children}</div>
</body>
</html>
)
}import { roboto_mono } from './fonts'
export default function Page() {
return (
<>
<h1 className={roboto_mono.className}>My page</h1>
</>
)
}Another Method of adding multiple fonts is using CSS Varaible. Let’s Look into an example:
import { Inter, Roboto_Mono } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
variable: '--font-inter',
display: 'swap',
})
const roboto_mono = Roboto_Mono({
subsets: ['latin'],
variable: '--font-roboto-mono',
display: 'swap',
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={`${inter.variable} ${roboto_mono.variable}`}>
<body>
<h1>My App</h1>
<div>{children}</div>
</body>
</html>
)
}In this example:
Now use them in the CSS file as like a CSS variable:
html {
font-family: var(--font-inter);
}
h1 {
font-family: var(--font-roboto-mono);
}In the example above, Inter will be applied globally, and any <h1> tags will be styled with Roboto Mono.
Instead of using Google font, we can also use Local font in our project.
import localFont from 'next/font/local'
// Font files can be colocated inside of `app`
const myFont = localFont({
src: './fonts/my-font.woff2',
display: 'swap',
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={myFont.className}>
<body>{children}</body>
</html>
)
}In this example:
When integrating numerous files for a single font family such as non-variable fonts’ wights, src can be an array:
const roboto = localFont({
src: [
{
path: './Roboto-Regular.woff2',
weight: '400',
style: 'normal',
},
{
path: './Roboto-Italic.woff2',
weight: '400',
style: 'italic',
},
{
path: './Roboto-Bold.woff2',
weight: '700',
style: 'normal',
},
{
path: './Roboto-BoldItalic.woff2',
weight: '700',
style: 'italic',
},
],
})All the steps are the same for adding fonts for Tailwind CSS. We can use any Google font from next/font/google. Moreover, We can use any font from Google or Local Fonts also. Only the changes we have to do are in the tailwind.config.js.
import { Inter, Roboto_Mono } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
variable: '--font-inter',
display: 'swap',
})
const roboto_mono = Roboto_Mono({
subsets: ['latin'],
variable: '--font-roboto-mono',
display: 'swap',
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={`${inter.variable} ${roboto_mono.variable}`}>
<body>
<h1>My App</h1>
<div>{children}</div>
</body>
</html>
)
}/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
'./app/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {
fontFamily: {
sans: ['var(--font-inter)'],
mono: ['var(--font-roboto-mono)'],
},
},
},
plugins: [],
}That's it for you. Here are some cool and amazing ways to use Google and local fonts in a Next JS project. Thus, it will help to create fast, efficient, and visually appealing websites.
I hope you got a lot out of reading this. Please do not hesitate to contact StaticMania with any questions or feedback. Happy coding!