-
Notifications
You must be signed in to change notification settings - Fork 319
next-css SSR not working with CSS files from node_modules #70
Comments
Hi @thibautRe thanks for opening this issue, I'm having the same problems :) I did some deep investigation on how the What is my scenario?
What I found outI created an isolated environment, skimmed through the code and recreated So my next suspect was On the Simply put, every dependency that appears on webpack externals do not get bundled, but instead webpack thinks you will want to require the package by yourself (which makes a lot of sense on a server context) and it puts this on your build: WorkaroundOnce I found out what was happening, it was easier to come with a workaround to fix my problem. As any workaround, this is not ideal but can solve some problems and buy time until the problem is addressed on the core of Basically, I recreated the externals configuration used by const resolve = require('resolve')
const withCSS = require('@zeit/next-css')
module.exports = withCSS({
cssModules: true,
webpack (config, options) {
const { dir, isServer } = options
config.externals = []
if (isServer) {
config.externals.push((context, request, callback) => {
resolve(request, { basedir: dir, preserveSymlinks: true }, (err, res) => {
if (err) {
return callback()
}
// Next.js by default adds every module from node_modules to
// externals on the server build. This brings some undesirable
// behaviors because we can't use modules that require CSS files like
// `former-kit-skin-pagarme`.
//
// The lines below blacklist webpack itself (that cannot be put on
// externals) and `former-kit-skin-pagarme`.
if (
res.match(/node_modules[/\\].*\.js/)
&& !res.match(/node_modules[/\\]webpack/)
&& !res.match(/node_modules[/\\]former-kit-skin-pagarme/)
) {
return callback(null, `commonjs ${request}`)
}
callback()
})
})
}
return config
},
})
Definitive solutionThe definitive solution must come from The ideal solution would be to add externals conditionally: only add if the module itself does not require any CSS. However, I don't know how one would do that with webpack. |
Very interesting investigation, thank you for sharing it here. Your solution works really nicely for my use-case, and fixed my issue 🎉. As this issue doesn't seem to be a problem with |
example does not work |
Same here, compiles then crashes for me |
Same issue, how to configure sass with react flext box grid? |
Get this problem fixed by adding this plugin: https://www.npmjs.com/package/next-plugin-transpile-modules Just make webpack to transpile the module containing css imports. Something like
|
If you are coming from Google search, especially if you see the:
this might be the solution you are looking for. 3 simple steps:
Note: Using Next v 8+ Background: |
T
This solves the problem. Thanks. |
@Vrq your posted solution does not work if |
This solves the problem! Thanks dude :) |
This solved it for me!! Thank you. |
I've been using RMWC, which requires users to load additional CSS. Importing the CSS files directly in the JSX files (eg. What worked for me (for reasons I don't really understand) was creating a local CSS file, importing it from the JSX file, and within it importing the RMWC CSS, like so: /* pages/mypage.css */
@import "~@material/textfield/dist/mdc.textfield.css";
@import "~@material/floating-label/dist/mdc.floating-label.css";
@import "~@material/notched-outline/dist/mdc.notched-outline.css";
@import "~@material/line-ripple/dist/mdc.line-ripple.css"; // pages/mypage.jsx
import "./mypage.css";
const MyPage = () => { //.... Possibly related: #498. Note that I wasn't getting errors in production that I could see -- the stylesheets were seemingly just silently dropped in prod. |
This was the only thing that worked for me. In my case Im working with SaSS and added the default config from next-sass but was having a lot of problems importing some package styles. Importing the css inside my sass file is what worked for me |
Hi guys, great work with Next!
I encountered an issue which involves
next-css
,node_modules
and Server-Side Rendering.Basically, when a NPM package requires a CSS file, SSR breaks. This is an issue when trying to use a component library that bundles CSS files in itself (in my case, fyndiq-ui).
The bug happens with or without CSSModules, and with or without PostCSS-loader. It also happens in production build
Note that the bug doesn't happen when importing local CSS files (thanks to #50 I presume).
I made a small repo that demonstrates the issue https://github.com/thibautRe/next-css-ssr-bug. You can run the README instructions to reproduce the bug.
I'm willing to submit a PR to fix this behavior. I checked the
next-css
code but didn't see anything glaringly problematic.The text was updated successfully, but these errors were encountered: