storybook: [Bug]: [@storybook/nextjs] Using @next/font breaks storybook v7
Describe the bug
Using Next 13βs new @next/font
font loader, which is required for loading fonts in the new /app
routes directory, breaks storybook v7 if it is referenced. Not sure if something can be done to shim it here or if it needs to be fixed upstream.
The workaround is to ensure that no code that touches storybook references the font loader (eg: global styles etc), which could be documented if nothing else.
To Reproduce
No response
System
System:
OS: macOS 13.0
CPU: (8) arm64 Apple M1
Binaries:
Node: 18.3.0 - /opt/homebrew/bin/node
Yarn: 1.22.15 - /usr/local/bin/yarn
npm: 8.11.0 - /opt/homebrew/bin/npm
Browsers:
Chrome: 107.0.5304.87
Firefox: 103.0.2
Safari: 16.1
npmPackages:
@storybook/addon-essentials: future => 7.0.0-alpha.34
@storybook/nextjs: ^7.0.0-alpha.41 => 7.0.0-alpha.41
Additional context
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 7
- Comments: 24 (10 by maintainers)
Commits related to this issue
- fix(storybook): π©Ή hotfix introduced by 84c591e => good fix Dropping the hotfix in favor of actual fix as resolved in https://github.com/storybookjs/storybook/issues/19711#issuecomment-1425887280 — committed to spilnotaxyz/website by dalechyn a year ago
- chore(release): 1.5.4 ## [1.5.4](https://github.com/spilnotaxyz/website/compare/v1.5.3...v1.5.4) (2023-02-10) ### Bug Fixes * **storybook:** π©Ή hotfix introduced by 84c591e => good fix ([e41670b](h... — committed to spilnotaxyz/website by semantic-release-bot a year ago
@valentinpalkovic Thanks for the help! Local font import is now resolved.
My project folder structure is next:
Considering this, I had to correctly set up the
staticDirs
configuration.Before:
After:
So I could import the fonts like this:
Hope this helps out someone else!
Absolutely love this nextjs extension, great job guys!
@next/font
is now mocked innext/jest
.I also got this to work to mock
@next/font/google
in a story.This will set the class in
.className
to be named the same as the function imported from@next/font/google
, e.g.Playfair_Display
.And then import a CSS files that defines the font inside the story.
I tried this for a while now and I somehow just canβt get it to work on my NX setup. I always either get this error when starting up Storybook:
Or this error on the Storybook web page:
My workaround is to serve the files at
/
and then loading them with regular@font-face
css. We are only using Storybook locally, so this setup is totally fine. You can continue reading if you want to dig into this though.My setup basically looks like this. I got a
StorybookWrapper.tsx
component for the that gets imported in thepreview.tsx
:apps/website/.storybook/StorybookWrapper.tsx:
apps/website/.storybook/preview.tsx:
Fonts get created in a
Global.tsx
. libs/components/src/lib/containers/Global.tsx:My file structure looks like this:
In my
main.ts
I therefore got this: apps/website/.storybook/main.ts:From is from the storybook directory and I guess in this case its the nested storybook directory because everything else than the value above throws an error. The to value should be from the root of my project so the one above should be fine as well (and does not throw an error).
../../../../../../../libs/components/src/assets/fonts/
is working as well, other paths throw an error.Iβm not quite sure where the problem is. Itβs either that I donβt really get how to work with the ββ¦/β¦/β in my local font, that my custom ts paths are not working correctly or that NX is in the way.
Hi @hobadams.
I am working on a fully-fledged solution right now. Due to the upcoming holiday season, the release of
@next/font
support might occur at the yearβs end or in the first weeks of January. Maybe I can already release something tomorrow that doesnβt break Storybook completely.Hey there @seanhakbb thank you for providing a repro! If you upgrade to the latest Storybook 7 release you will notice that it works correctly:
@AdrianFahrbach thanks for providing all this info, would you mind upgrading to the latest Storybook 7 and checking whether you still experience the issues?
Worked for me.
https://github.com/seanhakbb/storybookdebug I set this quick demo up that shows it working with npm run dev, but not in npm run storybook. (check in the iframe <head> after a class called something like βfont-face-font-98bc83β)
Im running on Node v18.13.0 btw. Thanks for the help! @valentinpalkovic
@seanhakbb Can you please create a reproduction and provide me a link to it? Then I will be able to debug it if you want π
Hi @valentinpalkovic, Iβm on
^7.0.0-beta.45
and can confirm that@nextjs/font
is not picked up bystorybook
.Hi @salchichongallo,
this workaround shouldnβt be necessary anymore. Have you tried the latest prerelease version?
Also make sure to use the
@storybook/next
framework, like described here: https://storybook.js.org/blog/integrate-nextjs-and-storybook-automatically/