-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Automatic WebFont Optimization does not work #24781
Comments
Thanks for reporting this. I will have a look into it. |
Same issue here. Any update? @janicklas-ralph |
The same happens to me. Neither using it on _app or _document works here. |
still an issue with: |
Note for OP: @Co9xs The commands you mentioned don't optimize fonts: yarn
yarn build
yarn dev # <-- this starts a dev server and ignores the next build command However it is also an issue with yarn
yarn build
yarn start This is the output we see: <head>
<link rel="stylesheet" data-href="https://fonts.googleapis.com/css2?family=Poppins">
<!-- ... -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins">
</head> The link is transformed and prepended in the head but the fonts never get inlined and therefore no optimization has been done. The original Actually the DOM gets larger instead, since we now have an extra "useless" |
For us this issue happened when loading additional babel presets FAILS // Result
// <head>
// <link rel="stylesheet" data-href="https://fonts.googleapis.com/css2?family=Poppins">
// <!-- ... -->
// <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins">
// </head>
module.exports = {
presets: ["next/babel", "@babel/preset-env", "@babel/preset-typescript", "@babel/preset-react"],
plugins: [
[
"@emotion/babel-plugin",
{
sourceMap: true,
autoLabel: "dev-only",
labelFormat: "[local]",
cssPropOptimization: true,
},
],
],
}; WORKS // Result
// <head>
// <link rel="stylesheet" data-href="https://fonts.googleapis.com/css2?family=Poppins">
// <!-- ... -->
// <style data-href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap">/* ... */</style>
// </head>
module.exports = {
presets: ["next/babel", "@babel/preset-env", ],
plugins: [
[
"@emotion/babel-plugin",
{
sourceMap: true,
autoLabel: "dev-only",
labelFormat: "[local]",
cssPropOptimization: true,
},
],
],
}; |
|
@timneutkens thanks for the explanation. We carried/copied this config through several repos and versions of nextjs. Bad habits. {
"presets": [
[
"next/babel",
{
"preset-env": {},
}
]
],
"plugins": []
} |
This appears to still be an issue, with real impact on mobile lighthouse scores. Anything we can do to help support it being fixed? The symptoms are as described above, regarding the extra |
It appears our issue is directly related to our In our case, we were including the following plugin, which is already a part of
|
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 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
class MyDocument extends Document {
render(): JSX.Element {
return (
<Html>
<Head>
{/* This does not work, notice the key prop. Removing it fixed the issue for me */}
<link
key="does-not-work"
href="https://fonts.googleapis.com/css2?family=Poppins:wght@700&display=swap"
rel="stylesheet"
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument; Removing the <head>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@700&display=swap" rel="stylesheet">
<!-- ... -->
<style data-href="https://fonts.googleapis.com/css2?family=Poppins:wght@700&display=swap">@font-face{font-family:'Poppins';font-style:normal;font-weight:700;font-display:swap;src:url(https://fonts.gstatic.com/s/poppins/v15/pxiByp8kv8JHgFVrLCz7V1g.woff) format('woff')}@font-face{font-family:'Poppins';font-style:normal;font-weight:700;font-display:swap;src:url(https://fonts.gstatic.com/s/poppins/v15/pxiByp8kv8JHgFVrLCz7Z11lFd2JQEl8qw.woff2) format('woff2');unicode-range:U+0900-097F,U+1CD0-1CF6,U+1CF8-1CF9,U+200C-200D,U+20A8,U+20B9,U+25CC,U+A830-A839,U+A8E0-A8FB}@font-face{font-family:'Poppins';font-style:normal;font-weight:700;font-display:swap;src:url(https://fonts.gstatic.com/s/poppins/v15/pxiByp8kv8JHgFVrLCz7Z1JlFd2JQEl8qw.woff2) format('woff2');unicode-range:U+0100-024F,U+0259,U+1E00-1EFF,U+2020,U+20A0-20AB,U+20AD-20CF,U+2113,U+2C60-2C7F,U+A720-A7FF}@font-face{font-family:'Poppins';font-style:normal;font-weight:700;font-display:swap;src:url(https://fonts.gstatic.com/s/poppins/v15/pxiByp8kv8JHgFVrLCz7Z1xlFd2JQEk.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}</style>
</head> |
Thank you. Removing the |
Does it work sometimes, or it doesn't inline always? Do you see any errors on the console during |
Not sure if it was specifically addressed, but upgrading to Next.js 12.1.4 seemed to fix it! |
I am encountering exactly the same problem with using Class and |
No, it never inlines, and there are no errors or warnings during I'm also using a Class component and |
Thanks. Also, what version of react are you using? |
Still experiencing this with |
This seemed to start happening to me after upgrading to react 18. Ended up disabling fontOptimization to fix for now |
I'm experiencing the same issue on Typebot after upgrading packages (React 18 and Next.js 12.1.6) |
I am also having this issue. Here's the setup: // fonts.ts
export const fonts = [
{
key: "font1",
rel: "stylesheet",
href: "https://fonts.googleapis.com/css2?family=Montserrat:wght@600&family=Open+Sans:wght@400;600&display=swap"
},
// ... more fonts
];
export const fontComponents = fonts.map((font) => (React.createElement("link", { key: font.key, rel: font.rel, href: font.href, className: 'someclass' }))) // _document.tsx
...
import { fontComponents } from '../some/path/to/fonts.ts'
...
const Document = (): React.ReactElement => {
return (
<Html>
<Head>{fontComponents}</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
} I've tried:
In all scenarios I'm seeing an output of: <head>
<link rel="stylesheet" data-href="https://fonts.googleapis.com/css2?family=...">
...
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=...">
</head> The only solutions that does work is when I code the fonts directly into |
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! |
Any update in regards to this issue Im still experiencing this with React 18 and the latest version of next? |
Same issue: Next 12.1.6 and React 18.1.0. Custom |
I'm also experiencing this issue. Found a minimal setup that only requires:
Steps to reproduce:
|
^ Currently using this hotfix: adding the |
Doesn't it make more sense to turn off the functionality? |
It seems broken for the pages with |
This issue seems to have been fixed by I just test the case above with:
|
i have same issue with writing the link in this dynamic form
and just get this
work normally if write the href without any variables maybe help |
NEWS! It was resolved with the Next.js update |
my case still not working |
Same error, doesn't load on 12.3.1 |
Same here, any updates on this? |
I also ran into this issue of Next 12 never properly inlining google font resources, even when it's nothing but a I dug around for a while and was able to get it working with this patch. I won't pretend to understand the details of the AST, but apparently the nodes of interest were neither diff --git a/node_modules/next/dist/build/webpack/plugins/font-stylesheet-gathering-plugin.js b/node_modules/next/dist/build/webpack/plugins/font-stylesheet-gathering-plugin.js
index 1ebc70a..d9050fe 100644
--- a/node_modules/next/dist/build/webpack/plugins/font-stylesheet-gathering-plugin.js
+++ b/node_modules/next/dist/build/webpack/plugins/font-stylesheet-gathering-plugin.js
@@ -76,7 +76,7 @@ function isNodeCreatingLinkElement(node) {
}
// React has pragma: _jsx.
// Next has pragma: __jsx.
- return (callee.name === "_jsx" || callee.name === "__jsx") && componentNode.value === "link";
+ return /^_*jsx$/.test(callee.name) && componentNode.value === "link";
}
class FontStylesheetGatheringPlugin {
gatheredStylesheets = [];
@@ -109,7 +109,7 @@ class FontStylesheetGatheringPlugin {
return;
}
let result;
- if (node.name === "_jsx" || node.name === "__jsx") {
+ if (/^_*jsx$/.test(node.name)) {
result = new _webpack.BasicEvaluatedExpression();
// @ts-ignore
result.setRange(node.range);
@@ -153,6 +153,7 @@ class FontStylesheetGatheringPlugin {
}
};
// React JSX transform:
+ parser.hooks.call.for("jsx").tap(this.constructor.name, jsxNodeHandler);
parser.hooks.call.for("_jsx").tap(this.constructor.name, jsxNodeHandler);
// Next.js JSX transform:
parser.hooks.call.for("__jsx").tap(this.constructor.name, jsxNodeHandler); |
Going to close this issue as automatic font optimization was superseded by |
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. |
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 fromnext/document
)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
The .next/server/font-manifest.json contains font definitions as fetched from https://fonts.googleapis.com/css...
To Reproduce
The text was updated successfully, but these errors were encountered: