0

I use Nextjs 9.3 with export to generate a static site. Material UI is set up according to this reference implementation (https://github.com/mui-org/material-ui/tree/master/examples/nextjs).

Additionally to Material UI, I have a global stylesheet style.css which is imported into _app.js according to the documentation (https://nextjs.org/docs/basic-features/built-in-css-support).

However after running export, index.html does not include any of the CSS from styles.css only that of Material UI. styles.css is only loaded as a normal stylesheet and not inlined. Any way to inject styles.css into index.html too to avoid FOUC?

EDIT Relevant parts of the folder structure as requested by @Dekel:

├── pages
│   ├── _app.js
│   ├── _document.js
│   ├── index.js
├── public
└── src
    ├── components
    └── styles.css
3
  • 1
    Can you show the folder structure that you have? Commented May 7, 2020 at 23:57
  • How are you importing styles.css? Commented May 8, 2020 at 22:31
  • nextjs.org/docs/basic-features/… exactly like this Commented May 8, 2020 at 23:03

1 Answer 1

1

if what are you looking for is to have your css files concat and minified inside the index.html in this way:

Case 0

// index.html
...
<head>
  <style>
    <!-- your css concat + minified here -->
  </style>
</head>
...

and not in this way

Case 1

// index.html
<head>
  <link rel="stylesheet" href="path/to/bundle.min.css"/>
</head>

I think you can't achieve this with NextJS out-of-the-box solution "Built-In CSS Support for Global Stylesheets".

Reading the doc from 9.2 release (https://nextjs.org/blog/next-9-2#built-in-css-support-for-global-stylesheets) it says:

In production, all CSS files will be automatically concatenated into a single minified .css file. This CSS file will be loaded via a <link> tag and automatically injected into the default HTML markup Next.js generates.

So, it doesn't seem to me that you can achieve Case 0 with that solution.


I had the same problem of FOUC and this is how I managed for my SSR solution with Next:

  1. helper function to concat & minify the files I need called prepareInPageCss.js which save a js module which returns a string with the bundled css as shown below:
const minify = require('@node-minify/core');
const cleanCSS = require('@node-minify/clean-css');
const fs = require('fs');
const bootstrapOutPutPathName = './static/css/_tempBootstrap.min.js';
const commonOutPutPathName = './static/css/_tempCommon.min.js';
const outputnameCss = './static/css/InPageCssBundle.css';

(async () => {
 try {
   await minify({
     compressor: cleanCSS,
     input: './static/css/bootstrap.min.css',
     output: bootstrapOutPutPathName,
     options: {
       keepSpecialComments: '0'
     }
   });

   await minify({
     compressor: cleanCSS,
     input: './static/css/common.css',
     output: commonOutPutPathName,
     options: {
       keepSpecialComments: '0'
     }
   });

   let bootstrapTempCss = fs.readFileSync(bootstrapOutPutPathName, "utf-8");
   let commonTempCss = fs.readFileSync(commonOutPutPathName, "utf-8");

   fs.writeFileSync(outputnameCss, bootstrapTempCss + commonTempCss);
   fs.writeFileSync(outputname, "const bundledCss=String.raw`" + bootstrapTempCss + commonTempCss + "`; export default bundledCss;");
 } catch (e) {
   console.log(e);
 }
})()
  1. In the file of your project where is the head structure I've done in this way:
   import commonCss from '../static/css/InPageCssBundle.js';
   const isDev = ENV === 'development';

   ...
    <title>{title}</title>
    <meta name='description' content={description}/>
    <meta charSet="utf-8"/>
    {isDev ? <>
       <link rel="stylesheet" href="/static/css/bootstrap.min.css"/>
       <link rel="stylesheet" href="/static/css/common.css"/>
     </> :
     <style>
       {commonCss}
     </style>
   }
   ...

Every time I create a new build I run node path/to/file/prepareInPageCss.js.

Now, I'm not 100% sure this is the only way (I've implemented this while Next was at 9.0) and I didn't try on static export yet but hope I gave you at least a fallback solution.

Cheers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.