next.js: Automatic WebFont Optimization does not work
What version of Next.js are you using?
10.2.0
What version of Node.js are you using?
14.15.4
What browser are you using?
Chrome
What operating system are you using?
macOS BigSur
How are you deploying your application?
Vercel
Describe the Bug
The font style is not inlined even though I added a link to_document.tsx in the following format.
(I’m sure using the Head component exported from next/document)
<Html lang="ja">
<Head>
<meta charSet="utf-8" />
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+JP" rel="stylesheet" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
I forked next.js to use a local next.js, and the same thing happened.
When I run ‘next build’, .next/server/font-manifest.json will be generated, but its contents will be empty.
This probably happens only with certain fonts (maybe Japanese font?).
Also, if you run ‘yarn dev’ after ‘yarn build’, regardless of whether the font is Japanese or not, the .next/server/font-manifest. json was disappeared, is this a problem?
I found this issue(#19159), but it was already closed and did not solve the problem.
Expected Behavior
The tag is transformed as expected
- link href property is replaced with data-href of the same value
- style tag is created with the same data-href key/value
- style tag contains an inlined font definition
The .next/server/font-manifest.json contains font definitions as fetched from https://fonts.googleapis.com/css…
To Reproduce
- Build and start a server from my minimal repo:
git clone https://github.com/Co9xs/next-automatic-webfont-optimization-sample.git
cd next-automatic-webfont-optimization-sample
yarn
yarn build
yarn dev
- Open http://localhost:3000/
- Inspect the <head> of the document
- Inspect the built .next/server/font-manifest.json file
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 21
- Comments: 47 (14 by maintainers)
Commits related to this issue
- fix: 🐛 Disable font optimization temporarly https://github.com/vercel/next.js/issues/24781 — committed to baptisteArno/typebot.io by baptisteArno 2 years ago
- Update to next 12.3 to fix font issues with SSR. https://github.com/vercel/next.js/issues/24781 — committed to tourist/coinscan-front by tourist 2 years ago
Still experiencing this with
12.1.6. Font optimization breaks in pages withgetServerSideProps!Same problem here. ~
getInitialProps~getServerSidePropsseems to be causing the issue. Anywhere, not just in the_app.tsxpage. (only in production, after running ‘build/start’)To reproduce on a new project:
pages/_document.jsxpages/working.jsxThis page works as expected, it contains the html:
pages/broken.jsxThis page doesn’t load the font at all. The html contains:
I’m surprised there are not that many people experiencing this problem. I thought that a web-font in combination with
getServerSidePropswould be a pretty standard scenario 😃This issue seems to have been fixed by
"next": "12.2.0".I just test the case above with:
getServerSideProps<Head><link></Head>We’re having the same issue here (on next 12.0.8). The workaround is to disable font optimization to get any google fonts to work in production build
Okay, I found what is causing the confusion, or maybe it’s a bug? Not sure. Maybe @timneutkens can tell us more about it?
So in the documentation of the font-optimalization they tell you to create a
_documentfile. If you go to the_documentdocumentation, they give you the following function example:However, if you look closely at the font optimization documentation, you see that you should probably be using the class component version:
After updating my document to a class component, everything worked as expected. If this is the way it is, the documentation of the font optimization might benefit from a warning or explanation about this.
I am encountering exactly the same problem with using Class and
styled-componenttogether insidegetInitialProps, also unfortunately upgrading tonext@12.1.4does not fix the problemI’m also having the same issue on Next 12.1.2.
I’m also experiencing this issue. Found a minimal setup that only requires:
create-next-appgetServerSideProps_document.jsfor Font Optimization (as per the documentation at https://nextjs.org/docs/basic-features/font-optimization#usage)Steps to reproduce:
yarn create next-appstyles/global.csschangefont-familytoRubikpages/index.jsadd this to the end:_document.jsunderpages/:yarn build && yarn startUnfortunately, @twankruiswijk 's approach didn’t work for me as my Document component is already a class. For reference, it doesn’t work with this:
NEWS! It was resolved with the Next.js update
Not sure if it was specifically addressed, but upgrading to Next.js 12.1.4 seemed to fix it!
Same problem here: the font does not get loaded and the html contains:
I am facing this issue only on pages with a dynamic route:
/pages/test.tsxwill load the font as expected but/pages/[slug].tsxwill not load the font.Both pages do not have any
get***Propsexport.It seems broken for the pages with
getServerSideProps. For ISR pages withgetStaticPropsor even static pages with no data, they work perfectly.Doesn’t it make more sense to turn off the functionality?
Same issue: Next 12.1.6 and React 18.1.0. Custom
_documentis a React Class Component.The most progress I’ve made is that it seems my elements are getting filtered out at/around here. It seems that isNodeCreatingLinkElement may be causing some of the issues people are having above, although I’m not sure that it is my issue exactly. I’ll have more time to research this weekend.
I hope this helps some people track down their issues!
No, it never inlines, and there are no errors or warnings during
next build.I’m also using a Class component and
next@12.1.4. The document page itself doesn’t have agetInitialPropsbut individual pages do havegetServerSideProps.@twankruiswijk Thank you, my problem with font optimization is solved.
Thank you. Removing the
keyprop fixed it for me too.Ran into this issue as well using “next”: “11.0.1”.
I cleaned up my .babelrc and .eslintrc files according to the documentation:
.babelrc: https://nextjs.org/docs/advanced-features/customizing-babel-config .eslintrc: https://nextjs.org/docs/basic-features/eslint
Unfortunately the font still wasn’t included correctly. Turned out that for me the culprit was a key prop which I added because when running
yarn devI got the following warning:Removing the
keyprop from the<link>element added the style element to the head of the document:It appears our issue is directly related to our
.babelrcas well, so it’s safe to say that if you’re having font optimization issues: start there.In our case, we were including the following plugin, which is already a part of
babel/preset-env, which is already part of the next/babel preset.