• This repository has been archived on 16/Sep/2022
  • Stars
    star
    424
  • Rank 102,329 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tiny Next.js helper for loading Google Fonts fast and asynchronously ⏩

⚠️

THIS PROJECT HAS BEEN DEPRECATED

As of Next.js 10.2, Google Fonts are automatically optimized πŸŽ‰

Thanks for all your love and support for this project,
Joe



next-google-fonts

A tiny next/head helper for loading Google Fonts fast and asynchronously ⏩

NPM Version Types Included Minizipped Size MIT License NPM Downloads Follow @joebell_ on Twitter


Using Next.js 10? See "How does this compare to Next.js font optimization?" before continuing.

Table of Contents

  1. Setup
  2. FAQs

Setup

In this example, we're going to add Inter (specifically weights 400 and 700) to a Next.js app.

See joebell.co.uk for a working example.

  1. Add the package to your Next.js project:

    npm i next-google-fonts
  2. Create a custom Head component.

    It's important to acknowledge that next-google-fonts is a small next/head component and should not be nested inside next/head. To solve this, wrap both components with a Fragment.

    // components/head.js
    import * as React from "react";
    import NextHead from "next/head";
    import { GoogleFonts } from "next-google-fonts";
    
    export const Head = ({ children, title }) => (
      <React.Fragment>
        <GoogleFonts href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" />
        <NextHead>
          <meta charSet="UTF-8" />
          <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0"
          />
          <meta httpEquiv="x-ua-compatible" content="ie=edge" />
    
          <title>{title}</title>
    
          {children}
        </NextHead>
      </React.Fragment>
    );
  3. Add the requested Google Font/s to your styles with a sensible fallback. It really doesn't matter whether you're using CSS or Sass or CSS-in-JS:

    body {
      font-family: "Inter", sans-serif;
    }
  4. Run your Next.js app to see the results in action!

    You should expect to see the fallback font first, followed by a switch to the Google Font/s without any render-blocking CSS warnings. Your font/s will continue to display until your app is re-hydrated.

    If JS is disabled, only the fallback font will display.

FAQs

How does this compare to Next.js font optimization?

Next.js 10 introduced automatic Google Font optimization, you can make a decision on which solution to use based on the following criteria:

  • "My fonts are not priority and can be loaded asynchronously" - use next-google-fonts.
  • "My fonts are priority and should not be loaded asynchronously" - use Next.js font optimization.

For further reading, see the Next.js issue discussion.

Why?

next-google-fonts aims to make the process of using Google Fonts in Next.js more consistent, faster and painless: it preconnects to font assets, preloads and asynchronously loads the CSS file.

In the current iteration of next/head, we can't make use of the familiar "media hack" method of asynchronous Google font loading:

<!-- Plain HTML -->
<link
  rel="stylesheet"
  href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap"
  media="print"
  onload="this.media='all'"
/>

If you'd like to track this issue in Next.js, you can follow it here: Next.js#12984.

More Repositories

1

cva

Class Variance Authority
TypeScript
5,556
star
2

plaiceholder

Beautiful image placeholders, without the hassle.
TypeScript
2,229
star
3

loading-disco

An alternative to the loading spinner πŸͺ©
CSS
230
star
4

raam

Beautifully boring cosmetic-free primitives for structure and layout.
TypeScript
121
star
5

blurhash-to-css

Convert a BlurHash to a CSS Object using TypeScript, Rust, and WebAssembly.
TypeScript
72
star
6

stitches-mixins

Shorthand utils for Stitches πŸ₯£
TypeScript
54
star
7

example-next-isr-with-swr

Lightning fast static pages with ISR, instantly updated with SWR ⚑️
TypeScript
53
star
8

tenprinciples.design

Dieter Rams' Ten Principles for Good Design
Astro
29
star
9

smoothie

A deliciously scalable and adaptable stylesheet methodology 🍹
JavaScript
29
star
10

cx

Concatenate your classes (with shortcuts) πŸ”—
TypeScript
24
star
11

blur-down

An experimental approach to deblurring images on load 🌫
17
star
12

marmalade

Another opinionated JAMstack static site starter (powered by Next.js) 🍊
TypeScript
11
star
13

joe-bell

πŸ§”πŸΌ
5
star
14

issue-repro-astro-vercel

Issue Reproduction: Astro with Vercel API Routes
TypeScript
3
star
15

vite-plugin-stitches

A tiny helper to configure SSR for Stitches in Vite ✨
TypeScript
2
star
16

open-sauced-goals

2
star
17

astro-bug-params-undefined

Astro
1
star
18

issue-repro-nextjs-plaiceholder

Issue Reproduction
TypeScript
1
star
19

issue-repro-stitches-ssr

TypeScript
1
star
20

isr-issue-repro

TypeScript
1
star
21

astley

Zero config. Zero dependencies. Zero runtime. ✨
1
star