Css-loader: Data URL as data:image/svg+xml is unable to be rendered after updating to 6.0.0

Created on 16 Jul 2021  路  12Comments  路  Source: webpack-contrib/css-loader

After updating to 6.0.0 from 5.2.6 the default Bootstrap drop down arrow defined as an inline data:image/svg+xml URL no longer displays on screen.

  • Operating System: MacOS 11.3.1
  • Node Version: v14.16.1
  • NPM Version: 6.14.12
  • webpack Version: 5.44.0
  • css-loader Version: 6.0.0

Confirmed in Firefox 89.0.2 and Chrome 91.0.4472.114

Expected Behavior

We should be able to see the drop down arrow as given in the image below:

Screen Shot 2021-07-15 at 4 15 17 PM
URL is modified in such a way that it is not able to render

This is an out of the box/untouched Bootstrap 5.0.2 rule as seen below as an image and the copied rule from bootstrap.css

Screen Shot 2021-07-15 at 4 15 48 PM

background-color: #fff;
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23343a40' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e");
background-repeat: no-repeat;
background-position: right 0.75rem center;
background-size: 16px 12px;

Actual Behavior

After updating to 6.0.0 the arrow no longer renders as seen in the image below.

Screen Shot 2021-07-15 at 4 23 59 PM

When using devtools in Firefox to examine the styles for the dropdown, it appears that some encoding has been applied to the data URL, making it unrenderable.

Screen Shot 2021-07-15 at 4 24 09 PM

background-image: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2016%2016'%3E%3Cpath%20fill='none'%20stroke='#343a40'%20stroke-linecap='round'%20stroke-linejoin='round'%20stroke-width='2'%20d='M2%205l6%206%206-6'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: right 0.75rem center;
background-size: 16px 12px;

Code

Note: Bootstrap 5.0.2 is set up as a vendor module outside of package.json and therefore not included below.

{
  "repository": {},
  "description": " ",
  "license": "MIT",
  "scripts": {
    "webpack-dev-server": "webpack serve --mode development --progress"
  },
  "dependencies": {
    "alpinejs": "3.2.2",
    "phoenix": "file:deps/phoenix",
    "phoenix_html": "file:deps/phoenix_html",
    "phoenix_live_view": "file:deps/phoenix_live_view",
    "webpack-manifest-plugin": "3.1.1"
  },
  "devDependencies": {
    "@babel/core": "7.14.6",
    "@babel/preset-env": "7.14.7",
    "@babel/plugin-transform-runtime": "7.14.5",
    "babel-loader": "8.2.2",
    "copy-webpack-plugin": "9.0.1",
    "clean-webpack-plugin": "4.0.0-alpha.0",
    "css-loader": "6.0.0",
    "mini-css-extract-plugin": "2.1.0",
    "sass": "1.35.2",
    "sass-loader": "12.1.0",
    "webpack": "5.44.0",
    "webpack-cli": "4.7.2",
    "webpack-dev-server": "3.11.2"
  }
}
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader'
          }
        },
        {
          test: /\.(jpg|png|gif)$/,
          type: 'asset/resource',
          generator: {
            filename: 'images/[hash][ext][query]'
          }
        },
        {
          test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
          type: 'asset/resource',
          generator: {
            filename: 'fonts/[hash][ext][query]'
          }
        },
        {
          test: /\.[s]?css$/,
          use: [
            MiniCssExtractPlugin.loader,
            {
              loader: 'css-loader',
              options: {
                sourceMap: true,
                importLoaders: 2
              }
            },
            { 
              loader: "sass-loader", 
              options: { 
                sourceMap: true 
              }
            },
          ],
        }
      ]
    },
    plugins: [
      new CleanWebpackPlugin(),
      new MiniCssExtractPlugin({ 
        filename: isProductionMode ? "[name].[contenthash].css" : "css/[name].css"
      }),
      new CopyWebpackPlugin({ 
        patterns: [
          { from: 'assets/static/', to: '' }
        ]
      }),
      new WebpackManifestPlugin(),
      new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /en/),
    ].concat(isProductionMode ? [] : [

    ]),
    optimization: {
      minimizer: [

      ]
    } 

How Do We Reproduce?

5.2.6 -> dropdown is visible
6.0.0 -> dropdown is not visible

Most helpful comment

In the meantime if anyone needs an easy temporary fix, adding a filter to ignore any inline assets starting with data:image/svg+xml is working for me:

{
  loader: "css-loader",
  options: {
    url: {
      // skip any data URLs of type image/svg+xml
      filter: (url) => !(url.startsWith("data:image/svg+xml"))
    }
  }
}

All 12 comments

maybe relate with:https://github.com/webpack-contrib/css-loader/issues/1337

I had the same issue as this and can confirm making the change from: #1337 (comment) has resolved the issue

for me not and I can not understand, how /\.png$/ can match a data:image/svg+xml....-string.
the only change was, that all png's get converted to inline data uri's (as expected)

I had the same issue as this and can confirm making the change from: #1337 (comment) has resolved the issue

for me not and I can not understand, how /\.png$/ can match a data:image/svg+xml....-string.
the only change was, that all png's get converted to inline data uri's (as expected)

I've just removed that comment actually as I've found a small problem - so it's not 100% resolved, with regards to .png$, I also have more file extensions that are managed by this, svg jpg etc

Digging into this issue a little deeper and comparing url-loader with asset modules the problem seems to be with the encoding of the generated data:image, in my case there is a # which if I change to %23 then the asset loads.

Bug in decoding

Thanks for the wicked fast turn around to resolution on this all! Looking
forward assisting with confirmation when I have a moment.

In the meantime if anyone needs an easy temporary fix, adding a filter to ignore any inline assets starting with data:image/svg+xml is working for me:

{
  loader: "css-loader",
  options: {
    url: {
      // skip any data URLs of type image/svg+xml
      filter: (url) => !(url.startsWith("data:image/svg+xml"))
    }
  }
}

Fixed in webpack, please update webpack to the latest version https://github.com/webpack/webpack/releases/tag/v5.45.1 and remove workaround

confirmed fixed after updating to 5.45.1 and removing my workaround above, thanks as always @alexander-akait 馃檶

Has the issue reappeared with 5.46.0? SVGs stopped working as element background images on my css at least.

No, double check webpack version, or provide example of the problem

Was this page helpful?
0 / 5 - 0 ratings