Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

BrunoQuaresma
Copy link
Collaborator

This aims to solve font rendering issues in Storybook like the inconsistent snapshot below.

Inconsistent snapshot:
image

References:

@BrunoQuaresma BrunoQuaresma requested a review from a team August 20, 2025 19:18
@BrunoQuaresma BrunoQuaresma self-assigned this Aug 20, 2025
@BrunoQuaresma BrunoQuaresma requested review from jaaydenh and removed request for a team August 20, 2025 19:18
@BrunoQuaresma BrunoQuaresma requested a review from aslilac as a code owner August 20, 2025 19:18
Copy link
Contributor

blink-so bot commented Aug 21, 2025

High-level: Preloading Inter via a bundler URL + Helmet looks good for the main app, and the ambient module declaration for *.woff2?url is the right way to satisfy TS. The Navbar test changes improve isolation and avoid mounting the entire app.

Main gap: Storybook doesn’t render App, so the <Helmet><link rel="preload" ... /></Helmet> in App.tsx won’t run there. Since the PR’s stated goal is stabilizing Storybook/Chromatic snapshots, we should also preload the font in the Storybook environment.

Suggested fix (uses existing HelmetProvider in preview):

// site/.storybook/preview.tsx
import { Helmet } from "react-helmet-async";
import interWoff2 from "@fontsource-variable/inter/files/inter-latin-wght-normal.woff2?url";

const withHelmet: Decorator = (Story) => {
  return (
    <HelmetProvider>
      <Helmet>
        <link
          rel="preload"
          as="font"
          type="font/woff2"
          href={interWoff2}
          crossOrigin="anonymous"
        />
      </Helmet>
      <Story />
    </HelmetProvider>
  );
};

Alternatively, add a preview-head.html with a <link rel="preload">, but using ?url import in code avoids asset-path issues with Vite.

Nice-to-have:

  • If we rely on italics anywhere, consider also preloading the italic variable file.
  • Keep document.fonts.ready loader (already present) to ensure Chromatic waits for fonts.

If you add the Storybook preload, I’m confident this will address the snapshot variability. Happy to re-review after that change.

@jaaydenh
Copy link
Contributor

@BrunoQuaresma Should the link tag be added to .storybook/preview.tsx instead of App.tsx?

@BrunoQuaresma
Copy link
Collaborator Author

I think it would be useful to preload the Inter font in the app too. Wdyt?

@jaaydenh
Copy link
Contributor

or actually, maybe have both preloads in App and preview.tsx

@BrunoQuaresma
Copy link
Collaborator Author

Good catch! I we don't use the App in Storybook 🤦

Copy link
Member

@aslilac aslilac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this actually does anything. for react-helmet to add this <link> to the head...

  • all of the javascript needs to be loaded
  • javascript needs to be parsed and executed
  • the virtual dom needs to be constructed
  • the reconciler needs to apply that virtual dom to the document
  • by this point I think the browser has probably already started loading the styles, because other elements have been applied into the dom which require its presence.

these storybook docs seem more promising to me: https://storybook.js.org/docs/configure/integration/images-and-assets#referencing-fonts-in-stories

@aslilac
Copy link
Member

aslilac commented Aug 21, 2025

also, about the import types: vite provides those types, we just haven't ever enabled them for some reason. 🙃 pr to fix: #19477

@BrunoQuaresma
Copy link
Collaborator Author

@aslilac that makes sense. About the docs you mentioned, I don't see a clear way to make fontsource to work nicely with it. A workaround that came up to my mind was to just copy and paste the font file in the public folder and use it. Wdyt?

@BrunoQuaresma
Copy link
Collaborator Author

Thinking more about that, I don't think it would work since the file location would be in different places.

@BrunoQuaresma BrunoQuaresma force-pushed the bq/preload-inter-font branch from ee682c8 to 237aeac Compare August 22, 2025 14:10
@BrunoQuaresma
Copy link
Collaborator Author

I think I got it working. I didn't know vite was smart enough to figure out how to bundle static assets from node_modules :)

@aslilac
Copy link
Member

aslilac commented Aug 22, 2025

I think you can even leave off the /node_modules/ bit and just "import it" like you would from typescript

vite is pretty clever 😄

@BrunoQuaresma
Copy link
Collaborator Author

@aslilac I tried, but it does not 😞

Copy link
Member

@aslilac aslilac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks bruno!

one last thought before you merge this tho, would it be worth adding a preload for the monospace font we use in the terminal stories? I think flickering has been a big problem there in the past. but in terms of inter specifically, this looks good.

@BrunoQuaresma BrunoQuaresma merged commit e7591aa into main Aug 25, 2025
32 checks passed
@BrunoQuaresma BrunoQuaresma deleted the bq/preload-inter-font branch August 25, 2025 13:41
@github-actions github-actions bot locked and limited conversation to collaborators Aug 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants