next.js
next.js copied to clipboard
Font optimization corrupts Google Fonts link
Verify canary release
- [X] I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Home
Binaries:
Node: 14.17.4
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 12.1.6-canary.9
react: 18.1.0
react-dom: 18.1.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Font optimization function corrupts font link and does not convert <link>
fully into <style>
tag. Therefore, fonts are not loaded in production and only way to load them is to disable font optimization
Before the build
<link
href="https://fonts.googleapis.com/css2?family=Inter&display=optional"
rel="stylesheet"
/>
After the build
<link
data-href="https://fonts.googleapis.com/css2?family=Inter&display=optional"
rel="stylesheet"
/>
Expected Behavior
It should convert <link>
to <style>
tag
// Before
<link
href="https://fonts.googleapis.com/css2?family=Inter&display=optional"
rel="stylesheet"
/>
// After
<style data-href="https://fonts.googleapis.com/css2?family=Inter&display=optional">
@font-face{font-family:'Inter';font-style:normal...
</style>
To Reproduce
- Add _document.js file to pages folder. I'm using example from Font Optimization page
// pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
render() {
return (
<Html>
<Head>
<link
href="https://fonts.googleapis.com/css2?family=Inter&display=optional"
rel="stylesheet"
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
- run
yarn build
- run
yarn start
- open
localhost:3000
and inspect<head>
with corrupted<link>
tag
I confirmed this
Facing the same issue
Also existing on 12.1.6 last release
I confirm this, and it is happening in development environment(without build), actually, I opened a discussion about this.
Seems like https://github.com/vercel/next.js/blob/4fb0beb1ee372d56128db16ec02ff88e333d4b2a/packages/next/server/render.tsx#L1310
we always have generateStaticHTML as false.
Hence the processors never work for ssr:
There were some commits 3 months ago regarding this. Not sure if that is what make SSR stop working. I change the code manually to start as TRUE, and the web optimizations started working again.
Seems like
https://github.com/vercel/next.js/blob/4fb0beb1ee372d56128db16ec02ff88e333d4b2a/packages/next/server/render.tsx#L1310
we always have generateStaticHTML as false.
Hence the processors never work for ssr:
There were some commits 3 months ago regarding this. Not sure if that is what make SSR stop working. I change the code manually to start as TRUE, and the web optimizations started working again.
I guess it could be, cause it is not working for me with the last NextJS version.
same with me "next": "12.1.6"
any news or how to fix this issue?
any news or how to fix this issue?
It seems there is no solution yet.
It's been fixed, wait for 12.1.7 if you can't go canary.
Since there was never a 12.1.7 version, it probably was in v12.2.0
Any update on this?
I'm also unable to get Google Fonts to load in development or production, unless I change the display
value from optional
to swap
.
Non working:
<link href="https://fonts.googleapis.com/css2?family=Inter&display=optional" rel="stylesheet"/>
Working:
<link href="https://fonts.googleapis.com/css2?family=Inter&display=swap" rel="stylesheet"/>
Font optimization has been superseded by next/font
which doesn't have this issue: https://nextjs.org/docs/app/building-your-application/optimizing/fonts#google-fonts
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.