next.js: CSS @import url for loading fonts doesnt work
What version of Next.js are you using?
12.0.1
What version of Node.js are you using?
17.0.1
What browser are you using?
Firefox, Chrome
What operating system are you using?
Windows 10
How are you deploying your application?
next start
Describe the Bug
App.scss:
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap');
Loading the font with next dev works but not with next build (next start).
Tested with optimizeFonts: false
but with same result.
Expected Behavior
In next 11.0.1 the font was imported correctly
To Reproduce
Inside App.scss
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap');
- next build
- next start
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 16 (3 by maintainers)
Commits related to this issue
- add test case (#31691) fixes #30567 — committed to vercel/next.js by sokra 3 years ago
- add test case (#31691) fixes #30567 — committed to natew/next.js by sokra 3 years ago
@timneutkens I found the proximate cause: the
@import"..."
rule that should load the font appears way down in the middle of a CSS chunk. Per the specification:Sure enough, if I manually edit the chunk to put this
@import
rule at the top, the font loads correctly.Our source file does have the
@import
rule at the top. So the CSS is being chunked incorrectly.@sveggiani It’s to be released in the 12.0.4 milestone, which is scheduled for November 30. Shouldn’t be too long now.
Same here, I use TailwindCSS and in
globals.css
I do this:I see in chrome inspector that computed font is the one I imported, but it doesn’t render as should be
On
next 11.1.2
this works fine!UPDATE:
When I added
_document.js
file and injected it with<link href="https://fonts.googleapis.com/css2?family=Poppins...." />
as child for<Head>
It worked as expected in Next 12.0.2I have the same problem with Google Fonts, the exact same situation as described by @jaemil.
With NextJs version 11.0.1 was working fine, and after upgrading to latest version the fonts are not loading correctly.
I’m using Tailwind CSS and the import is found in globals.css and is also attached in the output file styles.css.
The import:
@import url('https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@300;400;600;700;900&display=swap');
Yeah, the workaround of an explicit
<link>
tag in<Head>
in_document.js
works for my case as well — but I had to also:optimizeFonts: false
innext.config.js
(else the<link>
tag gets itshref
rewritten todata-href
so the browser ignores it)rel="stylesheet"
in the<link>
tag, else it only worked in dev mode, not in the production build (don’t know if that’s for some reason particular to my project)Thanks to the other commenters here for the help!