#Lazy Loading Local Fonts

1 messages · Page 1 of 1 (latest)

inland jay
#

How can I lazy load local fonts dynamically in Next.js based on user settings?

I want to:

  • Load a font only when the user setting requires it (lazy loading fonts)
  • Dynamically switch fonts when the user changes the setting without loading all fonts upfront

Is there a recommended approach or best practice for this?
Are there examples or existing solutions for loading local fonts lazily or on-demand in Next.js?

azure bluffBOT
#

Hi @inland jay. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the knowledge sources provided, there isn't a direct method for lazy loading local fonts dynamically based on user settings in Next.js. The next/font module is designed to optimize fonts by preloading them at build time rather than loading them on-demand.

When using next/font/local, fonts are preloaded based on where they're used:

  • If used in a unique page, the font is preloaded only on that route
  • If used in a layout, it's preloaded on all routes wrapped by that layout
  • If used in the root layout, it's preloaded on all routes

There are a few challenges with implementing dynamic font loading based on user settings:

  1. The preload option in next/font/local accepts a boolean value, but attempting to use a dynamic expression (like a ternary) can cause errors as noted in a GitHub issue next/font/local dynamic preload.

  2. Font declarations are typically made at the component level and are processed at build time, not runtime.

A potential workaround might be to:

  1. Define all your font options using next/font/local
  2. Use CSS variables approach as mentioned in the documentation for using multiple fonts
  3. Dynamically apply the appropriate font class based on user settings

For example:

// fonts.js
import localFont from 'next/font/local'

export const fontA = localFont({
  src: './fontA.woff2',
  variable: '--font-a',
})

export const fontB = localFont({
  src: './fontB.woff2',
  variable: '--font-b',
})

// In your component
import { fontA, fontB } from './fonts'
import { useState, useEffect } from 'react'
#

export default function Component() {
const [currentFont, setCurrentFont] = useState(null)

useEffect(() => {
// Load user preference from localStorage or API
const userFontPreference = localStorage.getItem('fontPreference')
setCurrentFont(userFontPreference === 'fontB' ? fontB : fontA)
}, [])

return (
<div className={currentFont?.variable}>
<p>This text will use the user's preferred font</p>
</div>
)
}


However, this approach still loads all font files upfront rather than truly lazy loading them. The Next.js font optimization is primarily designed for performance through preloading rather than for dynamic runtime loading.

For a true lazy loading approach, you might need to consider alternative methods outside of the `next/font` module, such as dynamically injecting font stylesheets based on user preferences, but this would lose the optimization benefits that Next.js provides.

Unfortunately, the knowledge sources don't provide specific examples or existing solutions for loading local fonts lazily or on-demand in Next.js based on user settings.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------