-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[Bug]: [@storybook/nextjs] Using @next/font breaks storybook v7 #19711
Comments
Hey @madeleineostoja thanks for opening this issue. This is definitely something we want to get working, but it's really tricky, given how the
So this process is very Next.js dependent, and it's probably tough to detach. I don't think it's simple to mock it. This exact error happens when running components that use the font import in Jest/Vitest, by the way. @hanneslund sorry to ping you but I think you're probably the best person to give opinions on how to proceed with this issue. Any thoughts? Would be super nice to be able to use/mock this somehow in external environments. Thank you so much! |
Hi! Is there an example somewhere I can clone and take a look? Thanks!
Just to clarify, it's not required but it gives you a bunch of stuff automatically. You can still add |
Thanks for the swift reply @hanneslund! I set up an example with instructions here for you: Happy to discuss further between our teams and come up with a good solution together! |
I am hitting this issue (using main.js webpackFinal: (config) => {
// Does not seem to work
config.resolve.alias["@next/font/local"] = require.resolve("./my-font-mock.js");
return config;
}) my-font-mock.js function localFont() {
// This is never called
return { style: { fontFamily: "Arial" } };
}
export default localFont; preview.js import { theme } from "../src/theme"; theme.js import localFont from "@next/font/local";
import { createTheme } from "@mui/material";
cont myFont = localFont({ src: "../public/font.woff2" });
return createTheme({
typography: {
fontFamily: myFont.style.fontFamily,
},
}); |
I also got this to work to mock webpackFinal: async (config) => {
config.resolve.alias = {
...config.resolve.alias,
"@next/font/google": require.resolve("./nextFontGoogle"),
}
return config;
} This will set the class in // nextFontGoogle.js
module.exports = new Proxy(
{},
{
get: function getter(_, receiver) {
return () => ({
className: receiver,
})
},
}
)
And then import a CSS files that defines the font inside the story. @font-face {
font-family: "Playfair_Display";
font-style: normal;
font-weight: 400 900;
font-display: swap;
src: url(https://fonts.gstatic.com/s/playfairdisplay/v30/nuFiD-vYSZviVYUb_rj3ij__anPXDTzYgEM86xQ.woff2)
format("woff2");
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
U+FEFF, U+FFFD;
}
.Playfair_Display {
font-family: "Playfair_Display", serif;
} |
My mock is not working because of
Removing |
Hey folks, any update on this issue? I'm getting the same thing in storybook |
Hi @hobadams. I am working on a fully-fledged solution right now. Due to the upcoming holiday season, the release of |
Crikey!! I just released https://github.com/storybookjs/storybook/releases/tag/v7.0.0-beta.13 containing PR #20291 that references this issue. Upgrade today to the
Closing this issue. Please re-open if you think there's still more to do. |
By decoupling the fonts from the theme I was able to workaround the error. This might be useful if you want to use a stable version of Storybook. In Next.js import { Work_Sans } from '@next/font/google';
const workSans = Work_Sans(...); // The point is to execute next/font away from Storybook
const appTheme = createAppTheme({
fontFamily: {
heading: workSans.style.fontFamily,
},
}); Load the font in Storybook somehow, for instance in your <link href="https://fonts.googleapis.com/css2?family=Work+Sans:wght@400&display=swap" rel="stylesheet" /> Now we can inject the theme in Storybook's preview.js: const appTheme = createAppTheme({
fontFamily: {
heading: "'Work Sans', sans-serif",
},
});
export const decorators = [
Story => (
<ThemeProvider theme={appTheme}>
<Story />
</ThemeProvider>
),
]; |
Hi @salchichongallo, this workaround shouldn‘t be necessary anymore. Have you tried the latest prerelease version? Also make sure to use the |
Hi @valentinpalkovic, I'm on |
Hi @h0tw4t3r, if you still have any issues, could you please provide a reproduction? |
@valentinpalkovic Sure! https://storybook.spilnota.xyz/ Currently, I bypass this issue by hardcoding the fonts for Another issue that I see is that in |
@h0tw4t3r One thing I can identify is that you haven’t set up staticDirs correctly to make your local next/fonts working. Please take a look here: https://github.com/storybookjs/storybook/tree/next/code/frameworks/nextjs#nextfontlocal |
Second: could you please change the content of import { Inter_Tight } from '@next/font/google'
import localFont from '@next/font/local'
const inter = Inter_Tight({
weight: ['400'],
subsets: ['latin'],
fallback: ['sans-serif']
})
const neueMachina = localFont({
src: [
{
path: '../public/fonts/neue-machina/NeueMachina-Regular.woff2',
weight: '400',
style: 'normal'
},
{
path: '../public/fonts/neue-machina/NeueMachina-Bold.woff2',
weight: '700',
style: 'normal'
}
]
})
export { infer, neueMachina } Please let me know, whether the two adjustments fixes it for you |
@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 Before: // .storybook/main.ts
export default {
// ...
staticDirs: ['../public']
} After: // .storybook/main.ts
export default {
// ...
staticDirs: [
{
from: '../public',
to: 'public'
}
] So I could import the fonts like this: // lib/fonts.ts
import { Inter_Tight } from '@next/font/google'
import localFont from '@next/font/local'
export const inter = Inter_Tight({
weight: ['400'],
subsets: ['latin'],
fallback: ['sans-serif']
})
export const neueMachina = localFont({
src: [
{
path: '../public/fonts/neue-machina/NeueMachina-Regular.woff2',
weight: '400',
style: 'normal'
},
{
path: '../public/fonts/neue-machina/NeueMachina-Bold.woff2',
weight: '700',
style: 'normal'
}
]
}) Hope this helps out someone else! Absolutely love this nextjs extension, great job guys! |
Dropping the hotfix in favor of actual fix as resolved in storybookjs/storybook#19711 (comment)
## [1.5.4](v1.5.3...v1.5.4) (2023-02-10) ### Bug Fixes * **storybook:** 🩹 hotfix introduced by 84c591e => good fix ([e41670b](e41670b)), closes [/github.com/storybookjs/storybook/issues/19711#issuecomment-1425887280](https://github.com//github.com/storybookjs/storybook/issues/19711/issues/issuecomment-1425887280)
I'm having some strange issues. I feel like I am nearly there. Here is my setup:
My main.ts static dirs
I dont get any 404 errors, however it seems my class seems to get broken url reference:
it removes slash and removes the letter "f". If I manually change this style tag (that is generated in head) I get the font icon loading. (correct path should be /fonts/ssg.woff2) This font loads fine in my nextjs project.
|
@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 :) |
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. Im running on Node v18.13.0 btw. Thanks for the help! @valentinpalkovic |
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 My setup basically looks like this. apps/website/.storybook/StorybookWrapper.tsx: import { interFont, sculpinFont } from "@my-project/components/containers/Global";
import { theme as baseTheme } from "@my-project/styles/theme";
import React from "react";
import { ThemeProvider } from "styled-components";
const theme = {
...baseTheme,
fontFamily: {
headlines: sculpinFont.style.fontFamily,
body: interFont.style.fontFamily,
},
};
const StorybookWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => {
return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
};
export default StorybookWrapper; apps/website/.storybook/preview.tsx: import StorybookWrapper from "./StorybookWrapper";
export const decorators = [
(Story) => {
return <StorybookWrapper>{Story}</StorybookWrapper>;
},
]; Fonts get created in a import localFont from "@next/font/local";
export const interFont = localFont({
src: [
{
path: "../../assets/fonts/inter-v12-latin-regular.woff2",
weight: "400",
},
],
fallback: ["Arial", "sans-serif"],
display: "swap",
});
export const sculpinFont = localFont({
src: [
{
path: "../../assets/fonts/sculpin-700.woff2",
weight: "400",
},
],
fallback: ["Arial", "sans-serif"],
display: "swap",
}); My file structure looks like this:
In my staticDirs: [
{
from: '../../../libs/components/src/assets/fonts/',
to: 'libs/components/src/assets/fonts/',
},
], 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. 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. |
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. |
But it creates a conflict with |
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
Additional context
The text was updated successfully, but these errors were encountered: