next.js: Compiler does not place css @imports in own style tags the way babel used to.
What version of Next.js are you using?
12.0.7
What version of Node.js are you using?
14.18.1
What browser are you using?
chrome
What operating system are you using?
mac0s
How are you deploying your application?
vercel
Describe the Bug
I just got bit in the upgrade.
In nextjs v10, this is considered valid:
* {
font-family: "Abril Fatface";
}
@import url('https://fonts.googleapis.com/css2?family=Abril+Fatface&display=block');
This works because each @import statement is wrapped in its own style tag.
In nextjs v12, @import statements are not wrapped in their own style tag.
Expected Behavior
I expect each @import statement is wrapped in its own style tag.
To Reproduce
In nextjs v12, add this to global.css
* {
font-family: "Abril Fatface";
}
@import url('https://fonts.googleapis.com/css2?family=Abril+Fatface&display=block');
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 4
- Comments: 23 (7 by maintainers)
I can confirm that this issue is still occurring with nextjs 12.1.0 production mode. Does anyone know any alternative way to load google fonts while using swcMinify?
Edit: The issue does not seem to be related to swc, and occurs even with swcMinify = false in nextjs 12+ versions; I worked around it in the following way:
Confirming that @scottccoates mentioned above.
I had Tailwind definitions at the top of the file and then a font Google Font import afterwards like this
Moving the imports as the top first thing on the file, solved the problem
I also confirm this working correctly on Next 11
Hope this is useful.
Same problem with
@importon the topNah, moving
@import ...around doesn’t fix anything for me.My CSS files used to have
@imports at the top as seen here: https://guildplanner.pro/_next/static/css/4b3115bd6e9f5f53cd0c.cssBut with NextJs 12, those leading
@imports are gone now.Update on previous comment:
I was importing bootstrap first in my
_app.jsfile, that was causing my issue:If i switch the import order, making the global css file first, it works, even without scss.
@balazsorban44 @zackdotcomputer Any update on a fix for the above in next 12. Im still facing the issue on production only, even with the correct import hierarchy.
In my case, I was using the
@importstatement in theglobal.cssfile, and also I was using thetailwindcssfor styling.I was able to fix this issue by defining the fonts in the
_document.jsfile.Same issue here, I am using NextJS v14 with sass and app router @import url’s position incorrect after build
For font loading, we recommend leveraging our built-in font optimization: https://nextjs.org/docs/basic-features/font-optimization
I can confirm this as an issue, but as Tim said, it’s not related to SWC. (I tried adding a
.babelrcfile, and this still reproduces)I could narrow it down to this change: https://github.com/vercel/next.js/compare/v11.1.3-canary.99...v11.1.3-canary.100
Most likely this PR #30165
Prior this, the generated CSS looked like this:
But after, it was left untouched: