Custom local font not applying in safari or IOS device but works on Chrome/Firefox with Next.js 14

2 min read 04-10-2024
Custom local font not applying in safari or IOS device but works on Chrome/Firefox with Next.js 14


Font Frustration: Why Your Custom Font Works in Chrome but Not Safari (And How to Fix It)

Have you ever painstakingly chosen the perfect custom font for your Next.js 14 project, only to find it renders beautifully in Chrome and Firefox but looks disappointingly generic in Safari or on your iOS device?

This frustrating scenario is a common headache for developers, and it often stems from a critical difference in how Safari and iOS handle font loading.

The Scenario:

Let's say you've included your custom font file (e.g., my-custom-font.woff2) in your public folder and added the following to your styles.css file:

@font-face {
  font-family: 'MyCustomFont';
  src: url('/my-custom-font.woff2') format('woff2');
  font-weight: 400;
  font-display: swap;
}

body {
  font-family: 'MyCustomFont', sans-serif;
}

Everything seems to be in place, but on Safari or your iPhone, the text stubbornly defaults to the browser's generic sans-serif font instead of your carefully chosen "MyCustomFont".

Understanding the Issue:

Safari and iOS devices are notoriously strict about how fonts are loaded and used. The root of the problem lies in the font-display property. Safari and iOS prioritize performance and user experience, opting to display generic fonts initially and then load custom fonts asynchronously.

The default font-display: swap instructs the browser to display the generic font while waiting for the custom font to load. This is great for performance, but it means that your custom font might not be visible initially.

The Solution:

The key is to use the font-display property strategically to ensure a smooth transition for Safari and iOS users. Here's how:

  • Use font-display: optional: This tells the browser to load the custom font but only apply it if it's available immediately. If the font hasn't loaded yet, the generic font will be used until the custom font is ready. This ensures a consistent experience for all users without sacrificing performance.

  • Adjust font-display for Specific Devices: You can selectively target Safari and iOS devices using media queries in your CSS:

@media (max-width: 768px) and (-webkit-min-device-pixel-ratio: 2) {
  @font-face {
    font-family: 'MyCustomFont';
    src: url('/my-custom-font.woff2') format('woff2');
    font-weight: 400;
    font-display: optional; /* For Safari and iOS */
  }
}

/* Other media queries or general styles */

Further Optimization:

  • Preload fonts: Consider using the <link rel="preload"> tag in your HTML head to pre-fetch the font file. This can significantly improve loading times and ensure a smoother experience.

  • Optimize font file size: Compress your font files using tools like Fontmin or Font Squirrel to reduce file size and improve page load speeds.

  • Use a Font Optimization Service: Services like Google Fonts or Typekit offer optimized font delivery and handling, which can alleviate some of the challenges related to custom font loading.

Conclusion:

The journey to successfully implementing custom fonts on all devices can be a bit of a roadblock. By understanding how Safari and iOS handle font loading and utilizing the font-display property correctly, you can avoid common pitfalls and ensure your chosen fonts look their best across all platforms.

Remember, a little extra attention to font optimization can make a significant difference in the overall user experience and leave your visitors with a positive impression of your website.