Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mini-css-extract-plugin with SSR. #90

Closed
princesoni1989 opened this issue Apr 5, 2018 · 58 comments
Closed

mini-css-extract-plugin with SSR. #90

princesoni1989 opened this issue Apr 5, 2018 · 58 comments

Comments

@princesoni1989
Copy link

How can we use mini-css-extract-plugin with SSR and server bundle.I tried using it is gives error
document is not defined

@princesoni1989 princesoni1989 changed the title mini-css-extract-plugin wist SSR. mini-css-extract-plugin with SSR. Apr 5, 2018
@alexander-akait
Copy link
Member

@princesoni1989 looks like a bug/feature. Can you create minimum reproducible example?

@bwhitty
Copy link

bwhitty commented Apr 5, 2018

I'd recommend reading #48 (comment)

@princesoni1989
Copy link
Author

@bwhitty I tried same, Error was removed but with was producing warning related to content mismatch between server and client.

@sokra
Copy link
Member

sokra commented Apr 12, 2018

Don't use this plugin in a server bundle. Use null-loader or css-loader/locals (for CSS modules) instead.

@johnelliott
Copy link

johnelliott commented May 11, 2018

@sokra just to clarify your suggestion, how would a user import the generated asset name into a bundle to generate a <link ... > tag in a template language or server-side react?

https://github.com/webpack-contrib/mini-css-extract-plugin#using-preloaded-or-inlined-css

The 'runtime code' aspect of this module really confuses me. I don't understand why there needs to be runtime code for CSS extraction, so I must be trying to use this module in the wrong situation. It seems highly geared towards JS-heavy single-page apps that load more JS and CSS asynchronously.

My goal is to server-render a react/redux app with link tags to the built CSS. My server code needs to know the paths of the built CSS files that include content hashes for caching.

Am I on the right track by using this module? The docs for the extract-text-webpack-plugin try to discourage users from using it with webpack 4, but this module doesn't seem to suit some of the past use cases.

@DimychOcean
Copy link

My experiments with SSR have shown that you can skip the "requireEnsure" hook on the server side. And I came to this solution: override method "getCssChunkObject" in MiniCssExtractPlugin class.

class ServerMiniCssExtractPlugin extends MiniCssExtractPlugin {
  getCssChunkObject(mainChunk) {
    return {};
  }
}

And then in server webpack config used class ServerMiniCssExtractPlugin.
My rule for CSS in config for server:

      {
        test: /\.css$/,
        use: [
          {
            loader: ServerMiniCssExtractPlugin.loader
          },
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1,
              modules: true,
              localIdentName: '[name]__[local]--[hash:base64:5]',
              sourceMap: true
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: () => [
                require('postcss-flexbugs-fixes'),
                require('autoprefixer')({
                  flexbox: 'no-2009',
                  grid: true
                })
              ]
            }
          }
        ]
      }

It's work for me.

@johnelliott
Copy link

I just skipped this module entirely and I’m now using the css-loader and extract-loader.

@yisar
Copy link

yisar commented Jul 28, 2018

I also encountered this problem and thought it was a bug. Would you solve it? Or can I only solve it myself?

@alejodaraio
Copy link

To me works with the @sokra solution.

appConfig.module.rules.push({
        test: /\.(sa|sc|c)ss$/,
        use: "css-loader/locals"
});

@winterJIE
Copy link

winterJIE commented Aug 6, 2018 via email

@michael-ciniawsky
Copy link
Member

#90 (comment)

@ozguruysal
Copy link

ozguruysal commented Aug 28, 2018

@michael-ciniawsky since you closed the issue and reading the comment you pointed, does that mean there'll be no SSR support in this plugin?

@BODhaha

This comment has been minimized.

@Avcajaraville
Copy link

@michael-ciniawsky Regarding you affirming there will be no SSR for this plugin (you thumbed up @ozguruysal ), don't you guys think this is a bit harsh for people using ssr ? Do you have an alternative we can use ? I read the comments and suggestions and Im not able to get the requirements fulfilled (to extract the css from our vue components in prod. and generate appropriate CSS).

@alexander-akait
Copy link
Member

@Avcajaraville Can you create new issue with test repo and describe what you have right now and what you expected and why?

Avcajaraville added a commit to Avcajaraville/debugging-mini-css-extract-plugin that referenced this issue Oct 26, 2018
@Avcajaraville
Copy link

@evilebottnawi Alright, I created a test repo for the issue, with comments on the README.md

Here is the repo: https://github.com/Avcajaraville/debugging-mini-css-extract-plugin

Please, read the readme and let me know if something is not clear.

Thanks a lot for taking time to look into this :)

I cant wait to have this working on my project ;)

@Avcajaraville
Copy link

@evilebottnawi Did you look at my repo ? Im spending almost every day revisiting the issue and trying to come up with a solution.... no luck 😂

@yisar
Copy link

yisar commented Oct 31, 2018

Im spending almost every day revisiting this issue and trying to come up with a solution……no luck,too……

@chentianyuan
Copy link

chentianyuan commented Dec 31, 2018

@neverthanmore i rm the css in masfest.json from initial to async with a custom webpack plugin, it effective for me too,i think it just like your method did but we need not to modify the vue source code.

@neverthanmore
Copy link

neverthanmore commented Jan 8, 2019

@chentianyuan it seems new pr didn't resolve this problem completely ! so sad

@yisar
Copy link

yisar commented Jan 8, 2019

@chentianyuan it seems new pr didn't resolve this problem completely ! so sad

@tiagomatosweb
Copy link

Did anyone find a solution?

I'm facing something similar. I am using vue-cli 3 to build a npm package.
If a build without css everything works fine, but if I add css into js the SSR gets trouble with document is not defined.

Any idea?

@GianfrancoCorrea
Copy link

i have same problem, using NextJS in Lerna, its say "window is not defined", no one solved this yet?

@sebkolind
Copy link

If people still have this issues, consider using this plugin instead: https://github.com/faceyspacey/extract-css-chunks-webpack-plugin ... It just works!

@webjohnjiang
Copy link

If people still have this issues, consider using this plugin instead: https://github.com/faceyspacey/extract-css-chunks-webpack-plugin ... It just works!

it works for me. thankyou

@alexander-akait
Copy link
Member

Somebody can create minimum reproducible test repo?

@milewski
Copy link

I fixed the issue by enforcing a limit of 1 single chunk:

new webpack.optimize.LimitChunkCountPlugin({
   maxChunks: 1
})

@Avcajaraville
Copy link

Het @evilebottnawi , I did create a repo back in #90 (comment)

Thanks !

@ScriptedAlchemy
Copy link
Contributor

mini-css does not impact SSR, you shouldn't be using mini-css as part of an SSR build.
https://github.com/faceyspacey/universal-demo (look at the webpack build)
What's needed is chunk flushing: https://github.com/faceyspacey/webpack-flush-chunks

The concept is that you utilize the client build to locate assets that need to be served during the first render.

The mechanism I have designed is implemented in various chunking projects. Though i have not looked outside react at solving the problem.
https://www.smooth-code.com/open-source/loadable-components/docs/server-side-rendering/

some derivative of webpack-flush-chunks could perform the task

@natewallis
Copy link

null-loader works for me too.... it prefetches all CSS, but only adds tags for styles used on the page... which is as good as a win for me at this stage.

In server config:

    module: {
        rules: [{
            test: /\.(sa|sc|c)ss$/,
            use: [
                'null-loader',
            ]
        }]
    },

@Murphycx94
Copy link

use extract-css-chunks-webpack-plugin instead mini-css-extract-plugin

webpack.base.config.js

const ExtractCssChunksPlugin = require('extract-css-chunks-webpack-plugin')
{
  ...
  {
        test: /\.css$/,
        use: [
          {
            loader: ExtractCssChunksPlugin.loader,
            options: {
              hot: !isProd,
              reloadAll: !isProd
            }
          },
          'postcss-loader',
          'css-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          {
            loader: ExtractCssChunksPlugin.loader,
            options: {
              hot: !isProd,
              reloadAll: !isProd
            }
          },
          'css-loader',
          'postcss-loader',
          'less-loader'
        ]
      }
  ...
  plugins: [
   ...
    new ExtractCssChunksPlugin({
      filename: isProd ? 'css/[name].[contenthash:8].css' : '[name].css',
      chunkFilename: isProd ? 'css/[name].[contenthash:8].chunk.css' : '[name].chunk.css'
    })
  ]
}

webpack.server.config.js

{
  ...
  plugins: [
    new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 1
    })
  ]
}

extract-css-chunks-webpack-plugin

@IlyaSemenov
Copy link

So what's the deal with extract-css-chunks-webpack-plugin, how is it different? It tries to 'sell' HMR (even though mini-css-extract-plugin also says HMR is working?), other than that I don't see any difference?

@ScriptedAlchemy
Copy link
Contributor

I merged with mini css and assist in maintaining both. Extract is under my total control and I can make more flexible alterations for users. The projects are generally kept in sync

christoph-bessei added a commit to clubdrei/mini-css-extract-plugin that referenced this issue Nov 3, 2020
cewald added a commit to icmaa/vue-storefront that referenced this issue Feb 8, 2021
…oduction (#548)

* According to [incompatibility with server-side-requests](webpack-contrib/mini-css-extract-plugin#90) we need to use the `null-loader` for the server-build to just load the files inside the manifest but not resolve them – otherwise it will result in a `document is not defined` exception.
* See also changes in configs in icmaa/shop-workspace#22
@slorber
Copy link

slorber commented Apr 1, 2021

Don't use this plugin in a server bundle. Use null-loader or css-loader/locals (for CSS modules) instead.

If we require regular CSS (not modules) on the server, now that null-loader is deprecated, what should be used instead?

Setting resolve.alias.package to false will tell webpack@5 to ignore a module.

That does not really seem to be applicable to this usecase.

Creating my own null-loader definitively works, just wondering if it's what I should do 😅

@alexander-akait
Copy link
Member

We have emit: true | false option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests