Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore different font-display options with font inlining #21555

Closed
housseindjirdeh opened this issue Jan 26, 2021 · 9 comments
Closed

Explore different font-display options with font inlining #21555

housseindjirdeh opened this issue Jan 26, 2021 · 9 comments
Milestone

Comments

@housseindjirdeh
Copy link
Collaborator

Describe the feature you'd like to request

Font optimizations have landed that downloads and inlines Google Font stylesheets at build time. This doesn't give developers any control on whether they would like to load these fonts asynchronously or use different font-display values.

Describe the solution you'd like

No specific solution, this will need some experimenting with first. @rgabs is taking a look to figure out what the best option here might be.

Describe alternatives you've considered

Some similar discussion on alternative/similar approaches in #16065.

@andrewgadziksonos
Copy link

I had a PR out for typekit / adobe fonts here #18146

Not sure how useful it is now but figured I should share!

@timneutkens timneutkens added this to the backlog milestone Apr 29, 2021
@felixmosh
Copy link
Contributor

It is supported by google fonts automatically, using link

<link
  href="https://fonts.googleapis.com/css?family=Assistant:400,600,700&display=swap"
  rel="stylesheet"
/>

Will inline css with font-display: swap

@deadcoder0904
Copy link

I am reading https://nextjs.org/docs/basic-features/font-optimization & want to use it in pages/_document.js like:

pages/_document.js

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head>
          <link
            href="https://fonts.googleapis.com/css2?family=Inter"
            rel="stylesheet"
          />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

When I go to Google Fonts & select my font using 700, it shows me to add this snippet:

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Lora:wght@700&display=swap" rel="stylesheet">

Do I need to add the preconnect tags or Next.js adds it behind the scenes?

@DoctorDerek
Copy link

DoctorDerek commented Jul 13, 2021

@deadcoder0904 My understanding is that you still need to add the preconnect tags, though I haven't seen any documentation or source claiming as such.

See for example this repo, which does include one of the preconnect links:

          <link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="anonymous" />
          <link
            href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap"
            rel="stylesheet"
          />

Source: https://github.com/timlrx/tailwind-nextjs-starter-blog/blob/master/pages/_document.js

@deadcoder0904
Copy link

Yep, I went with preconnect 10 days ago :)

@housseindjirdeh
Copy link
Collaborator Author

Multiple different optimizations have landed to improve fonts in Next.js as mentioned above (thanks folks), so I'm closing this :)

@adamduncan
Copy link

Looking at this in production, I believe Next is taking care of the preconnect.

E.g. With nothing more than:

<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet" />

...in the <Head> of our _app.js, in production I'm seeing:

<head>
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link rel="stylesheet" data-href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&amp;display=swap" data-optimized-fonts="true"/>
  
  <!-- ...and the fonts.googleapis.com request inlined further on in the head -->
  <style data-href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap">@font-face{font-family:'Montserrat';font-style:normal;font-weight:400;font-display:swap;src:url(https://fonts.gstatic.com/s/montserrat/v15/...

Very cool 👏

@dohomi
Copy link

dohomi commented Dec 8, 2021

Is there a way to use this with dynamic font families based on page props? I let content editor select their fonts in a CMS, it would be great if this would be optimized by NextJS. I now back in v10 it was not possible but did something changed since version 12 got released?

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants