question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected onLoad attribute behaviour in next/head

See original GitHub issue

Bug report

Describe the bug

In my project raam I’m tying set "Inter" as my font by linking to a stylesheet from Google Fonts.

In Head (a component that extends next/head) I’m using:

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

(note the preconnect to the actual font assets, then the display=swap which is intended to show the fallback font until the loaded font is swapped in)

When I then go to view the page (in Firefox particularly) I see a flash of unstyled/invisible text: https://raam-git-chore-inter.joebell.now.sh/

Screenshots

ezgif-2-8ea6320f12f1

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Go to https://raam-git-chore-inter.joebell.now.sh/ (in Firefox where this is more visible)
  2. Hard reload and watch the behaviour of fonts

Expected behavior

The system font is seen first, and ‘swapped’ to Inter on load.

System information

  • OS: macOS
  • Browser (if applies): Firefox 75
  • Version of Next.js: 9.3.3
  • Version of Node.js: 13.8.0

Additional context

I appreciate there’s a lot of variables here: Theme UI, Google Fonts, Now.sh, Next.js, but it would be good if we can figure out the root cause here for others using Next.js. If it’s something that could be resolved by adding some documentation I’d be more than happy to do so.

Thanks again for all of the team’s hard work on Next.js, it’s a real pleasure to work with

Prior reading

Example

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

10reactions
felipedebonicommented, Mar 30, 2021

Following seems to work

<style dangerouslySetInnerHTML={{
  __html: `</style>
    <link
      rel="stylesheet"
      href="https://...."
      media="print"
      onload="this.media = 'all';"
    />
    <style>`
}}></style>
5reactions
joe-bellcommented, May 18, 2020

As an update I’ve tested this new approach, which is a mix of font-loading performance suggestions I’ve found online:

<link
  rel="preload"
  href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap"
  as="style"
/>
<link
  href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap"
  rel="stylesheet"
  media="print"
  // // Next.js doesn't like this but it allows us to load CSS asynchronously
  // @ts-ignore
  onLoad="this.media='all'"
/>
Before After
Screenshot 2020-05-17 at 13 40 49 Screenshot 2020-05-17 at 13 58 06

However a slight flash still remains and TypeScript doesn’t like my onLoad="this.media='all'" as it doesn’t accept string values. Am I doing this wrong or do I need to raise a PR to update types?

I have a hunch that something in next/head is changing after load on the client, but I’m not experienced enough to know what’s happening under the hood to cause this FOUT.


Update #1 - I’ve tried fg-loadCSS but the same FOUT issue occurs. Whatever approach there is still a small FOUT.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js - How to add a <link> tag inside the <head> with literal ...
What I'd do is create a component called link that wraps Next/Link then set the logic up in there. so you'd have Link-><a...
Read more >
Optimize loading third-parties - Patterns.dev
There may be some code that needs to execute after a specific third-party has been loaded. This can be specified in the onload...
Read more >
Basic Features: Handling Scripts - Next.js
Event handlers can be used with the Script component to execute additional code after a certain event occurs: onLoad : Execute code after...
Read more >
Lecture Notes in Computer Science 2374 - Springer Link
tem behavior. In the AL-D/1 reflective programming system [56] an object is represented by multiple models, allowing behavior to be described at different....
Read more >
Next.jsでlink要素にonload属性を追加する方法 - Sorami Asai
以下のIssueでも議論されておりまして、そこに記載されている実装を参考にしました。 Unexpected onLoad attribute behaviour in next/head · Issue # ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found