Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

442
Views
How can I add a stylesheet dynamically with NextJS?

In my nextJS application, I need to load a stylesheet dynamically based on the user preference received from the database.

So, in my page, I'm adding it in the Head (next/head), as follows:

<Head>
<link rel="stylesheet" href={`/fonts/${type}/stylesheet.css`}></link>
</Head>

However, this is giving me a warning in the console in development mode:

Do not add stylesheets using next/head (see <link rel="stylesheet"> tag with href="/fonts/cal/stylesheet.css"). Use Document instead. 
See more info here: https://nextjs.org/docs/messages/no-stylesheets-in-head-component

The stylesheet itself contains the font-face:

@font-face {
  font-family: "Cal Sans";
  src: url("CalSans-SemiBold.woff2") format("woff2"),
    url("CalSans-SemiBold.woff") format("woff");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

Since the user's preference is stored in the database, and I receive this value via a query, I don't know how I can add it to the Document.js file.

I'll really appreciate any help on this.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

While it might be possible to dynamically import a CSS file in your main app component (and you can check out an example in this guide), I would suggest a different solution.

Include all stylesheets in your application, but make its styles only apply when the html (or body) element has a certain class. Then all you need to do is modify that class based on user preference.

// styles.css
body.themeFoo #container {
  // some styles...
}

This is how many implementations of dark mode work, e.g. in tailwind CSS. I believe this is a better pattern to solve your problem.

about 4 years ago · Juan Pablo Isaza Report

0

next.js uses react so maybe try the useState hook or you can change the herf property

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!