Skip to content
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

CSS @import url for loading fonts doesnt work #30567

Closed
jaemil opened this issue Oct 28, 2021 · 16 comments · Fixed by #31691
Closed

CSS @import url for loading fonts doesnt work #30567

jaemil opened this issue Oct 28, 2021 · 16 comments · Fixed by #31691
Assignees
Labels
Webpack Related to Webpack with Next.js.

Comments

@jaemil
Copy link

jaemil commented Oct 28, 2021

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');

  1. next build
  2. next start
@jaemil jaemil added the bug Issue was opened via the bug report template. label Oct 28, 2021
@sveggiani
Copy link

sveggiani commented Oct 28, 2021

Hi @jaemil , I just reported the same in #30569. I'm sorry that I didn't see your issue first. I was writing mine when you posted. In our case the Google fonts import works ok but not the one from Typekit.

@sitoftonic
Copy link

sitoftonic commented Oct 28, 2021

I 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');

@hoangph271
Copy link

I had the same problem with NES.css and google fonts, but I found this workaround:
Comment out urlImports like this:

experimental: {
  concurrentFeatures: true,
  serverComponents: true,
  // urlImports: []
}

And try building again, works on both my ARM macOs and x64 Ubuntu...!

@matthoiland
Copy link

Same issue with Typekit and Tailwind, but a different workaround:

Instead of:

/* base.css */
@import "https://use.typekit.net/*******.css";

This works for me:

/* base.css */
@import "./fonts.css";
/* fonts.css */
@import "https://use.typekit.net/*******.css";

I need to take a nap thinking about why this works 🤯

@timneutkens timneutkens added kind: bug and removed bug Issue was opened via the bug report template. labels Oct 29, 2021
@timneutkens timneutkens added this to the 12.0.x milestone Oct 29, 2021
@alaadahmed
Copy link

alaadahmed commented Nov 1, 2021

Same here, I use TailwindCSS and in globals.css I do this:

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  html,
  body {
    font-family: 'Poppins', sans-serif;
    line-height: 1.8;
  }
  // ...

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.2

@gthb
Copy link

gthb commented Nov 2, 2021

@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:

Any @import rules must precede all other valid at-rules and style rules in a style sheet (ignoring @charset), or else the @import rule is invalid.

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.

@gthb
Copy link

gthb commented Nov 2, 2021

Possibly because the @import"..." statement has been so eagerly minimized as to have no space between the @import and the "'..." but css-loader expects a space?

... never mind, that was my bad Github search-fu landing me in some really old code :)

@mrodrigues95
Copy link

mrodrigues95 commented Nov 2, 2021

Adding a custom _document.js and injecting it with the desired font as @alaadahmed suggested fixes it for me as well with v12.0.2.

@gthb
Copy link

gthb commented Nov 3, 2021

Yeah, the workaround of an explicit <link> tag in <Head> in _document.js works for my case as well — but I had to also:

  • disable font optimization with optimizeFonts: false in next.config.js (else the <link> tag gets its href rewritten to data-href so the browser ignores it)
  • include 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!

@petehl
Copy link

petehl commented Nov 3, 2021

  • include 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)

Including the rel="stylesheet"was what I was missing as well.

So adding <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins...." /> as child for <Head> in _document.tsx had our fonts rendering again.

@sveggiani
Copy link

Are there any news or progress on this?

@alizahid
Copy link

@sveggiani It's to be released in the 12.0.4 milestone, which is scheduled for November 30. Shouldn't be too long now.

@timneutkens timneutkens removed this from the 12.0.5 milestone Nov 17, 2021
@timneutkens timneutkens added the Webpack Related to Webpack with Next.js. label Nov 17, 2021
@zackdotcomputer
Copy link
Contributor

Unfortunately, not fixed in Next 12.0.4 or 12.0.5 canary 3

@sokra sokra mentioned this issue Nov 22, 2021
@kodiakhq kodiakhq bot closed this as completed in #31691 Nov 22, 2021
kodiakhq bot pushed a commit that referenced this issue Nov 22, 2021
@edgechurchdev
Copy link

Importing Typekit fonts is still broken for me in 12.0.7. Should this issue still be open?

@balazsorban44
Copy link
Member

No. Please open a separate issue with a reproduction.

@vercel vercel locked as off-topic and limited conversation to collaborators Dec 21, 2021
@balazsorban44
Copy link
Member

There is a possibility that this is the same as #32645

natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Webpack Related to Webpack with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.