When using Next.js, the global.css file is not loaded until a manual page refresh is performed. Additionally, the styles.js

The current situation is that styling is being applied in two places: global.css and index.js using styled-components.

Challenge

When the local server is started or restarted, and a manual page refresh is done, all styles are correctly applied. Any style changes are also reflected immediately. However, after performing another manual page refresh, only styles from global.css are applied initially. The styles from the index.js file do not load until certain actions are taken:

  1. Clicking on one of the links and then on the return button (as shown in the 2nd screenshot)
  2. Restarting the local server and refreshing the page

I have recorded the steps I took. Apologies for the background noise. 😌
https://www.youtube.com/watch?v=cp-97ZYsQhw

I have included the code snippets just to provide a better overview of the code. I am trying to create a functional example on codesandbox.

Question

Why does next.js only load the global.css initially, ignoring the index.js with additional CSS until either the local server is restarted or specific links are clicked?

import Link from "next/link";
import styled from 'styled-components';

const StyledMain = styled.main`
    width: 100vw;
    height: 100vh;
    display: grid;
    justify-content: center;
    align-content: center;
`;

const StyledLi = styled.li`
    font-size: var(--fontsize-profileButtons);
    text-align: center;
    list-style: none;
    margin: 50px 0; 
`;

export default function Home() {
    return (
        <>
            <StyledMain>
                <ul>
                    <StyledLi>
                        <Link href="/about">about</Link>
                    </StyledLi>
                    <StyledLi>
                        <Link href="/projects">projects</Link>
                    </StyledLi>
                    <StyledLi>
                        <Link href="/blog">blog</Link>
                    </StyledLi>
                </ul>
            </StyledMain>
        </>
    )
}
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

body {
  width: 100vw;
  height: 100vh;
}

a {
  color: inherit;
  text-decoration: none;
}

* {
  box-sizing: border-box;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

What index.js appears like after page refresh and how it's supposed to look https://i.sstatic.net/PZYXO.png

Steps to ensure that the CSS code inside index.js is also loaded https://i.sstatic.net/1cqFl.png

Answer â„–1

In my experience with version 13.3.4, I encountered a similar problem that was resolved by loading my global.css file into a client-side component. It's worth noting that this may not be an official fix.

Answer â„–2

To include your library's css file, simply insert the import into your _document.js file, like so:

<link rel="stylesheet" href="https://example.com/css/library.min.css" />

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

Creating a scrollable table with CSS: A step-by-step guide

My table has too many columns causing it to exceed the width of the screen. I would like to make it scrollable horizontally for a neater and more organized appearance. I believe CSS with the overflow hidden property can achieve this, but I am unsure where ...

How to eliminate the issue of horizontal scrollbar appearing in Material UI Datagrid

I'm currently utilizing the Mui datagrid component and encountering a horizontal scrollbar issue. const columns = [ { field: 'fileName', headerName: 'File Name', flex: 2, headerClassName: 'table-header' }, { field ...

What is the best way to implement buttons for updating and deleting items in an Autocomplete menu using Material-ui in React.js?

Currently, I am working on developing an autocomplete feature that allows me to dynamically add, update, and delete options in the select menu while it is open. With the help of Material-UI Autocomplete Creatable component, I was able to easily implement ...

The useEffect hook is causing a loop of API calls after successfully fetching data

I've been working on fetching data from my API and then performing some mappings to present it in the desired format. The challenge I'm facing is that the API calls are asynchronous, so I need to wait for the response before updating my dataset. ...

Is it possible to detect whether a website is being accessed through a mobile app or a browser when using ReactJS?

My intention is to present a download link for an app to the user only if they have not already downloaded the app or accessed the site through their browser. If the user accessed the website from the app, I would like to hide that button. While I was ab ...

How to reduce the text input field size in Django Bootstrap for a sleeker design

I have a form with various elements like textfields, radio buttons, and text areas. The textfield size is currently too small and does not look good: https://i.sstatic.net/ghimd.png I'm wondering how I can make the textfields slightly larger and why ...

What strategies can be utilized to raise the max-height from the bottom to the top?

I am facing the following coding challenge: <div id = "parent"> <div id = "button"></div> </div> There is also a dynamically generated <div id="list"></div> I have successfully implem ...

Difficulty in pinpointing hyperlinks within a styled list

My current challenge involves customizing the color of links within the <li> elements by applying specific classes to the <li> tag. The structure of my HTML is as follows: <div id="sidebar_tall"> <ul> <li class="active_item"> ...

The presence of <!--[if lte IE8]>< ![endif]--> on the website is causing problems with the dropdown

Check out my link: Problem #1: Why does "" appear on the site in Internet Explorer? The markup seems correct, so what is causing this to show up? Problem #2: In Internet Explorer, there is an issue with clicking on subnav items above the jQuery slider wh ...

Tips for resizing the dimensions of two div elements to fit the screen dimensions

Take a look at this template. It's interesting how the left content div and right sidebar divs adjust themselves based on the browser window size, zoom level, or viewing device such as a mobile phone. When viewed on a mobile, only the content div is d ...

Fixed positioning in child element while synchronizing the parent's horizontal scroll with the child element's left position

I have a div with relative positioning that scrolls horizontally. Within this div, I want to include a fixed element that also scrolls along with the parent div but remains at the top because the parent div is a child of a vertically scrolling div. To be ...

Issue encountered: When attempting to submit a form, an error is thrown stating "TypeError:

I encountered an issue while attempting to create a select form that toggles a boolean value in an object from true to false when a specific option is selected. Although my solution appeared promising, I kept running into errors. import { useState } from & ...

Out of the DIVs floating left and right, which one is positioned first?

Is the order of <div> elements in HTML important? If it is, what is the reason behind it? .float-left{float:left;} .float-right{float:right;} <div> <div class="float-left">Left</div> <div class="float-right">Right</ ...

Having trouble deploying my Gatsby Blog on Github pages, as the command "npm run deploy" keeps throwing an error

After attempting to deploy my Gatsby blog on github pages, I encountered an issue. I followed the instructions found in Gatsby's documentation: https://www.gatsbyjs.org/docs/deploy-gatsby/ The command I ran was: npm run deploy However, I faced the ...

"Error encountered when attempting to access collection object as undefined with async/await functionality

I am currently in the process of setting up an express application to handle server-side rendering for my react app. One of the challenges I am facing is the need to access Firebase content within a specific route that is defined as a boolean in my routes. ...

Getting rid of background color in a tooltip using Material UI

I'm currently tackling an issue with Material UI's tooltip. I can't seem to find a way to make the background of the tooltip completely transparent. By default, it displays with a grey background and white text. Changing the background color ...

Is there a way to speed up the loading time of React in the browser?

My experience with using React in the browser has been positive, but I'm facing a speed issue. The initial loading time is quite lengthy as it compiles. I am looking for a way to use React/JSX directly in the browser and have it compile instantly wit ...

`Is it more effective to define an array outside of a component or inside of a component in React?`

Exterior to a unit const somevalue = [1, 2, 3, 4, 5, 6]; const Unit = () => { return ( <div> {somevalue.map((value) => <span>{value}</span>)} </div> ); }; export default Unit; Interior to a unit const Unit ...

Guide on sending route parameters to my functional component

Route ID Handling return ( <div key={driver.id}> <Link to={"/drivers/" + driver.id}> <OurStaffList driver={driver} /> </Link&g ...

Tips for maintaining user authentication in my application (Firebase and React)

I am encountering an issue in my app where, after successfully signing in with email and password, if I reload the page, the user is automatically logged out. How can I prevent this from happening so that users remain signed in even after reloading the pag ...