Using getServerSideProps in Next.js is preventing the global css from loading

In my Next.js application, I have defined global styles in the file /styles/globals.css and imported them in _app.tsx.

// import default style
import "../styles/globals.css";

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

export default MyApp;

The page is located in the pages directory (pages/index.tsx)

export async function getServerSideProps({req, res }) {
   // Some fetch here
   return {
      props: {
         someProps: objectFromFetchThere,
      },
   };
}

const Home: NextPage<{ someProps: SomeProps[] }> = ({ someProps }) => {
   return (
      <>
         <Head>
            <title>Home page</title>
            <meta name="description" content="Generated by create next app" />
            <link rel="icon" href="/favicon.ico" />
         </Head>
         <Layout>
            // More code here
         </Layout>
      </>
   );
};

While everything works fine when running the application with next dev, including loading styles and calling getServerSideProps, issues arise in production mode (

next build && NODE_ENV=production ts-node src/server.ts
). The global styles are not loaded, and the _app file is also not utilized. Is it not possible to use global styles in pages that have getServerSideProps exported? I couldn't find any information about this in the Next.js documentation. Am I overlooking something?

Here's a snippet of my custom server:

(async () => {
   try {
      const expressServer = express();
      await app.prepare();

      // Custom routes defined (e.g., /facility) that do not interfere with styles.

      // Error middleware should be used after other custom routes
      expressServer.use(
         (err: Error, req: Request, res: Response, next: NextFunction) => {
            console.error(err.stack);
            res.status(500).send("Unexpected error occurred.");
         }
      );

      expressServer.all("*", async (req: Request, res: Response) => {
         try {
            await handle(req, res);
         } catch (e) {
            console.error("Error occurred handling", req.url, e);
            res.statusCode = 500;
            res.end("internal server error");
         }
      });

      expressServer.listen(port, (err?: any) => {
         if (err) throw err;
         console.log(
            `> Ready on localhost:${port} - env ${process.env.NODE_ENV}`
         );
      });
   } catch (e) {
      console.error(e);
      process.exit(1);
   }
})();

Answer №1

The problem arises when combining a custom server with typescript. By changing the "target": "es5" setting to "target": "es6", the issue was resolved. This solution was found on .

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

Ways to manipulate external CSS classes with styled components without direct access

Currently, I am utilizing react-table and wanting to implement CSS rules for the class rt-td, which is not accessible or adjustable through their API functionalities. In traditional CSS practice, I could easily override the CSS class within my stylesheet. ...

What is the number of steps jQuery animates in?

Exploring my creative side, I decided to create my own custom animate function. Struggling to achieve a seamless animation effect, unlike the smooth transitions produced by jQuery. I'm curious about the formula they utilize to determine the ideal num ...

Issue with Left Alignment of Tabs in Material-UI

As of now, material-ui's latest version does not provide support for aligning tabs to the left in the component. However, I came across a workaround on this GitHub page I have implemented the same workaround, and you can view it here: Unfortunately, ...

The toggle feature doesn't seem to be working properly in Bootstrap 5

I've tested it in the latest Bootstrap version and still can't get it to work. I even switched to version 5, but the toggle functionality remains a problem. I've tried various solutions from StackOverflow with no luck ** I've added all ...

The functionality of jQuery .rotate() appears to be malfunctioning

I'm struggling to get this code working properly. My jQuery version is 2.1.0. Here's the code snippet: $("a.shownav img").rotate(180); Any suggestions on how to make it function correctly without relying on a plugin? ...

When attempting to chain middleware in next-connect, an issue arises with the error handler

I have the following pair of middleware functions: function validateEmail(req, res, next) { console.log('email validation'); if (req.body.email && req.body.email.match(EMAIL_REGEX)) { console.log('email OK!'); return ...

excess white space appears in the mobile version

I recently completed a website using both materializecss and bootstrap platforms. While I know this may not be the best practice, it worked for my needs. However, I am facing an issue with the mobile view. When I reduce the viewport size, a margin appear ...

Invalid JSON cannot be defined

I'm encountering the error mentioned above in this React component. Despite my best efforts in reviewing it, everything appears to be correct, so I'm puzzled as to why the error is occurring. I am utilizing next.js 13.3.0. Uncaught SyntaxError: ...

The Scss mixin fails to style the element

The problem at hand Lately, I've been struggling to make my "px to vw" mixin function properly in Scss. Despite importing all the necessary functions, whenever I inspect an element like an h1, the mixin doesn't seem to execute and instead return ...

Is there a way to prevent elements on a web page from shifting when the page width becomes narrow enough?

Currently, as I delve into the world of web development with Svelte, I am in the process of creating a basic website to put my knowledge to the test. The main aim is to ensure that no matter how much the page is resized, the text and video content remain e ...

A Guide to Modifying Background Image Attribute using JavaScript

I am currently in the process of changing an attribute of a JavaScript variable from url("../images/video.png") (as declared in the CSS) to url("../images/pause.png") using this line of code: fullscreenPlayPauseButton.style.backgroundImage = "url("../imag ...

Is there a way for me to expand the dropdown menu?

My current dropdown looks like this. I'd like the dropdown menu to span across the screen, something like this: To create my dropdown, I am using https://github.com/angular-ui/ui-select2. AngularJS JQuery Select2 I'm not sure if there is an ...

Error occurred during Next.js build: SyntaxError was thrown due to an unexpected token "'<'"

Encountering multiple console errors during Next.js build preview using Netlify GitHub integration, rendering the site unusable: webpack-211c438de79acdca.js:1 Uncaught SyntaxError: Unexpected token '<' framework-715a76d8b0695da7.js:1 Uncaught ...

Enhance the appearance of 6 image URLs by wrapping them in dynamic divs with the

Can someone assist me in displaying 6 images retrieved from a MySQL database on an MVC Razor view? Images 1 and 6 should be placed in separate divs labeled "item", while images 2, 3, 4, and 5 should be grouped together in a div called "item-small". Below i ...

Finding the "img" id using jQuery

Here is the code snippet I am working with: <div id="popupContactIMG_4179" class="xxxx"> <a id="popupContactCloseIMG_4179">x</a> <img alt="" src="../IMG_4179.jpg" id="id1&q ...

css the vertical column is unable to reach 100% vertical

Check out my website at I need help getting the black column with my thumbnails to extend all the way down on the site. Here is the CSS code for the thumbnails: #thumbnails { position:absolute; top: 110px; width: 190px; height: 100%; ...

What is the method for switching the pre font family back to the default Bootstrap 5 style?

The default font for <pre> tags in Bootstrap 5 is typically a monospaced style, which suits most coding and programming needs. However, I'm looking to utilize a <pre> tag for formatting a poem where line breaks are crucial. In this case, I ...

Tips for ensuring Accordion menu closes upon clicking a second time

I created a basic Accordion menu using just HTML and CSS. The functionality works well, but I'm facing an issue where clicking on an open accordion doesn't close it. Can this be achieved with CSS alone or would I need to use JavaScript? .midd ...

Moving a DIV below a fixed-positioned element

I have a website with a scrollable div. It works well, but I also need an absolutely positioned div on top of it - and it still needs to scroll smoothly without any hindrance. You can check out a basic JSFiddle demonstration here: http://jsfiddle.net/41ra ...

Avoid cascading of the 'display' property in JavaScript styling to prevent inheritance

Is there a way in Javascript to toggle the visibility of a larger portion of HTML without affecting inner display properties with display: <value>? Despite setting an outer display property using Javascript, the inner display properties are also alt ...