Built-in automatic self-hosting for any font file with optimal loading and zero layout shift
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Universal font loader for custom font files with automatic fallback generation and optimal loading performance. Supports all major font formats and provides comprehensive configuration options.
/**
* Load custom local font files with optimal performance
* @param options - Local font configuration
* @returns Font object with className, style, and optional variable property
*/
function localFont<T extends CssVariable | undefined = undefined>(
options: LocalFont<T>
): T extends undefined ? NextFont : NextFontWithVariable;interface LocalFont<T extends CssVariable | undefined = undefined> {
src: string | Array<{
path: string;
weight?: string;
style?: string;
}>;
display?: Display;
weight?: string;
style?: string;
adjustFontFallback?: 'Arial' | 'Times New Roman' | false;
fallback?: string[];
preload?: boolean;
variable?: T;
declarations?: Array<{ prop: string; value: string }>;
}The src property accepts either a single font file or an array of font files with different weights and styles.
src: string;Path to a single font file. All font formats are supported (woff2, woff, ttf, otf, eot).
src: Array<{
path: string;
weight?: string;
style?: string;
}>;Array of font descriptors for different weights and styles:
display?: Display;Controls CSS font-display behavior:
weight?: string;
style?: string;Default weight and style for the font family:
adjustFontFallback?: 'Arial' | 'Times New Roman' | false;
fallback?: string[];variable?: T;CSS custom property name for the font family. Enables using the font via CSS variables.
preload?: boolean;Whether to preload the font file for faster initial rendering.
declarations?: Array<{ prop: string; value: string }>;Additional CSS properties to include in the @font-face declaration:
import localFont from "@next/font/local";
const myFont = localFont({
src: './fonts/my-font.woff2',
display: 'swap',
});
export default function Page() {
return (
<div className={myFont.className}>
<h1>Custom Font Text</h1>
</div>
);
}import localFont from "@next/font/local";
const myFontFamily = localFont({
src: [
{
path: './fonts/my-font-regular.woff2',
weight: '400',
style: 'normal',
},
{
path: './fonts/my-font-italic.woff2',
weight: '400',
style: 'italic',
},
{
path: './fonts/my-font-bold.woff2',
weight: '700',
style: 'normal',
},
],
display: 'swap',
});import localFont from "@next/font/local";
const myVariableFont = localFont({
src: './fonts/my-variable-font.woff2',
variable: '--font-custom',
weight: '100 900', // Variable font weight range
});
export default function RootLayout({ children }) {
return (
<html lang="en" className={myVariableFont.variable}>
<body>{children}</body>
</html>
);
}import localFont from "@next/font/local";
const customSerif = localFont({
src: './fonts/custom-serif.woff2',
fallback: ['Georgia', 'Times New Roman', 'serif'],
adjustFontFallback: 'Times New Roman', // Use Times New Roman metrics for fallback adjustment
display: 'swap',
});import localFont from "@next/font/local";
const customFont = localFont({
src: './fonts/custom-font.woff2',
declarations: [
{ prop: 'font-feature-settings', value: '"liga" 1, "calt" 1' },
{ prop: 'font-variation-settings', value: '"wght" 400' },
],
});import localFont from "@next/font/local";
const headingFont = localFont({
src: './fonts/heading-font.woff2',
preload: true, // Preload for above-the-fold content
display: 'block', // Block rendering until font loads
});@next/font supports all major web font formats:
public/
fonts/
my-font-regular.woff2
my-font-bold.woff2
my-font-italic.woff2Font paths are relative to the public directory:
const myFont = localFont({
src: './fonts/my-font.woff2', // Resolves to public/fonts/my-font.woff2
});const optimizedFont = localFont({
src: './fonts/custom-font.woff2',
adjustFontFallback: 'Arial', // Prevents layout shift
fallback: ['system-ui', 'Arial', 'sans-serif'],
display: 'swap',
});Local fonts handle various error scenarios: