When compiling for production in NEXT JS 13.4.1, the styles from bootstrap and global.scss are taking precedence

The issue I'm facing is only present when building for production; in development, the styles appear correctly.

Despite my efforts, the bootstrap styles are not being applied at all. Only the styles from main.scss seem to be working. Unfortunately, I haven't been able to find any examples using app router + bootstrap + scss together.

Here is the structure of my files:


**/app
--/css**
----main.min.css
----main.min.css.map
**--/scss**
----_fonts.css
----_header_footer.scss
----_mixins.scss
----_responsive.scss
----main.scss
--layout.jsx

In the main.scss file, the imports include:

@import "bootstrap/dist/css/bootstrap.css";
 @import "@fortawesome/fontawesome-free/css/all.min.css";
 @import "nouislider/dist/nouislider.min.css";
 @import "@splidejs/splide/dist/css/splide.min.css";
 @import "_mixins";
 @import "_header_footer";
 @import "_fonts";

In layout.jsx (the RootLayout):

  import './css/main.min.css'

During development, all styles apply perfectly. The problem arises after building, where the bootstrap classes in the JSX throughout the app cease to function.

I attempted adding this import to the layout.jsx file:

 import "bootstrap/dist/css/bootstrap.css";

Although this makes the bootstrap styles work on build, some of my scss styles get overridden. For instance, the font applied to the body and the text decoration none for anchor tags, among others.

I also tried:

Adding the following code to _app.js:

import 'bootstrap/dist/css/bootstrap.css';
import '../styles/globals.css';

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />;
}

export default MyApp;

I even experimented with creating a global.scss file in the /app root and importing both bootstrap.min.css and my main.min.css:

Another attempt involved moving the import of boostrap.min.css further down in my main.scss file while omitting the 'min' in the imports.

Answer №1

Add a new file named pages/_document.js

import { Html, Head, Main, NextScript } from 'next/document';

export default function Document() {
  return (
    <Html>
      <Head>
        <link
          rel='stylesheet'
          href='https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22404d4d56515650435262160c140c12">[email protected]</a>/dist/css/bootstrap.min.css'
        />
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}

Delete the import of bootstrap CSS from _app.js

https://i.stack.imgur.com/EftDu.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The `.populate()` function is malfunctioning in mongodb

As I work on developing an ecommerce application using Nextjs, one of the challenges I encountered involved populating the "reviewBy" property within an array of objects called "reviews". To tackle this issue, I attempted to populate the "reviewBy" proper ...

"Prisma vs. Supabase: A Comparison of Image Uploading

I am encountering an issue with adding image URLs to the Prisma database. I have successfully uploaded multiple images from an input file to Supabase storage, but when I try to add their URLs to the database, I receive an error regarding property compatibi ...

tips for successfully indexing dynamic routes on Google with NextJS

Although I am familiar with ReactJS, I am currently working on developing a web page using NextJS to enhance SEO. However, I have encountered a question. How can I ensure that Google recognizes the dynamic routes created with NextJS? I know about getStati ...

Tips for resolving the unmounted component issue in React hooks

Any suggestions on resolving this issue: Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect ...

Is it possible to programmatically disable and enable the autoPlay feature in SwipperJS?

Is there a way to have an autoplay feature in Swipper only if the number of items in the array is greater than 5? I have multiple arrays with varying lengths and want to display them in Swipper. ...

Is there a problem with Framer Motion exit and AnimatePresence in Next.js?

Why isn't my exit prop or AnimatePresence working as expected? This is my custom variant: const imgVariant = { initial: { opacity: 0, y: -100, }, animate: { opacity: 1, y: 0, transition: { type: " ...

Is it possible to pass a specific region to a language in nextjs i18n?

Suppose I am in favor of the languages ['en', 'de']. In this case, I would like to redirect requests with a header such as Accept-Language: de-DE to de since it is the most closely related to what I support. // Below is my i18n configur ...

React's `setState` function seems to be failing to hold onto

Recently, I've been challenged with creating an infinite scroll loader component. However, I'm facing a peculiar issue where my 'items' array is constantly resetting to [] (empty) instead of appending the new results as intended. A cou ...

Automatically launch Next.js applications upon restarting an Ubuntu server using pm2

pm2 has been incredibly effective in managing my Next.js application on my Linode cloud server with Ubuntu 20. However, I've encountered some challenges when attempting to automatically run my Next.js apps upon system reboot. Typically, I have been m ...

Encountering an issue: The API call for /api/orders was resolved but no response was sent, potentially causing requests to become stuck in Next.js

Struggling with an error while working on an ecommerce website using Next.js. The error message reads: error: API resolved without sending a response for /api/orders, this may result in stalled requests The error also indicates: API handler should not ...

Top recommendations for implementing private/authentication routes in NextJS 13

When working with routes affected by a user's authentication status in NextJS 13, what is the most effective approach? I have two specific scenarios that I'm unsure about implementing: What is the best method for redirecting an unauthenticated ...

The function 'next build' is malfunctioning when attempting to create a build for my nextJS application

While attempting to create a nextJS application, I encountered a recurring issue: next build - info Creating an optimized production build - info Compiled successfully - info Linting and checking validity of types - info Collecting page data [ ] - ...

Having trouble getting the tailwindcss Google font link tag to apply properly

When I use the @import in the tailwind css file, it works fine. However, when I try to add the font using the <link> tag in _document.jsx, it fails to work properly. In Chrome dev tools, it shows that the classes are applied but the fallback font is ...

Is it no longer possible to export static pages in Next.js 14?

I'm currently experiencing a problem where my CSS styles are not being exported when I try to convert my project into static HTML pages. Here is the configuration in my next.config.js file: ** @type {import('next').NextConfig} */ const next ...

The Next.js image feature encounters a 500 error when in a production environment

I recently deployed my Next.js app on GAE and encountered an issue where none of the images located in the public folder were properly served. Every time I tried to access them, a 500 error occurred, accompanied by this specific error message found upon re ...

Certain Next.js Links trigger a complete page refresh exclusively when hosted on the Vercel platform

Currently, I am utilizing Next.js 14 app router on Vercel along with Mantine for components. One of the functionalities that is incorporated in my project involves using Next.js Links polymorphically with Mantine components following the guidelines mention ...

How to customize the page background color in Next JS

I am currently working on a project using Next Js and have encountered an issue. I have a global.css file set up in the project, but I need to change the background color of a specific page without affecting the rest of the project. Although I have tried u ...

Utilizing an array of data to create a complex structure with nested

In my Next.JS React project using TSX files, I have set up a data file like this: const fieldMapping = { category:[ { title: "Category 1", Subtitle: ["Category 1", "Category 2"], SubSubTitle: ["Category ...

The class name remains unchanged despite the change in value

I am currently working on a webpage that features two interactive tabs. The goal is to have one tab highlighted as "active" while the other remains inactive when clicked, and then switch roles when the other tab is selected. Below is the code snippet I ha ...

Tips for integrating NextAuth.js with SSG (static site generation) on a Next.js site

When working with Next.js, you have the option to construct your website using either server-side (SSR) or static client-side (SSG) rendering. However, a common issue arises when running next build && next export as it eliminates the /api routes. ...