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

Optimisation not working in background image #123

Open
dpkpaa opened this issue Dec 18, 2019 · 8 comments
Open

Optimisation not working in background image #123

dpkpaa opened this issue Dec 18, 2019 · 8 comments

Comments

@dpkpaa
Copy link

dpkpaa commented Dec 18, 2019

For external and inline css background image url not optimizing

@mercteil
Copy link

I use SCSS & next-sass package .... url('....jpg') does not get into my dist folder

@mercteil
Copy link

Not a pretty workaround is to inline style the background...
I only did not figure out how to serve webp and fallback to jpg if needed, so I just server jpg

import img from './somewhere.jpg';

const Component = () => {
...
 return {
   <div styles={{ backgroundImage: `url(${img})`}} />
 }
}

@judwhite
Copy link

judwhite commented Mar 6, 2020

I'm getting a link to bg-calculate.6decbc4e1de7c6b0bdbce85c6adbae41.png, for example. The contents of that file are text:

» cat bg-calculate.6decbc4e1de7c6b0bdbce85c6adbae41.png
module.exports = "/_next/static/images/bg-calculate-c0250b7962ac1c5eebe5083071273657.png";

The file referenced by the module.exports contains the actual image.

I see the same behavior with next, next build, and next export.

@stephankaag
Copy link

stephankaag commented Mar 11, 2020

I ran into this issue as well. Any idea @cyrilwanner?

@mpetricek
Copy link

mpetricek commented Jul 22, 2020

I tried this and it seems to work

<div style={{backgroundImage:`url(${require(`../assets${slide.img}`)})`}} />

@jamesryan-dev
Copy link

jamesryan-dev commented Aug 15, 2020

I've run into this error also - will attempt this solution

-> can confirm @mpetricek 's solution works :D

@edgardz
Copy link

edgardz commented Dec 15, 2021

I'm getting a link to bg-calculate.6decbc4e1de7c6b0bdbce85c6adbae41.png, for example. The contents of that file are text:

» cat bg-calculate.6decbc4e1de7c6b0bdbce85c6adbae41.png
module.exports = "/_next/static/images/bg-calculate-c0250b7962ac1c5eebe5083071273657.png";

The file referenced by the module.exports contains the actual image.

I see the same behavior with next, next build, and next export.

I'm having the exact same issue, which prevents me from using background images on pseudo-elements (before, after), for instance.

@edgardz
Copy link

edgardz commented Dec 15, 2021

Temporarily fixed on my project by adding this to next.config.js

... 

const optimizedImagesCSSFix = (nextConfig = {}) => {
  return Object.assign({}, nextConfig, {
    webpack(config, options) {
      if (config.module.rules) {
        config.module.rules.forEach((rule) => {
          if (rule.oneOf) {
            rule.oneOf.forEach((subRule) => {
              if (subRule.type && subRule.type === 'asset/resource' && subRule.exclude) {
                subRule.exclude.push(/\.(jpg|jpeg|png|svg|webp|gif|ico)$/);
              } else if (
                subRule.issuer &&
                !subRule.test &&
                !subRule.include &&
                subRule.exclude &&
                subRule.use &&
                subRule.use.options &&
                subRule.use?.options?.name
              ) {
                if (
                  String(subRule.issuer).includes('/\\.(css|scss|sass)(\\.webpack\\[javascript\\/auto\\])?$/') &&
                  subRule.use.options.name.startsWith('static/media/')
                ) {
                  subRule.exclude.push(/\.(jpg|jpeg|png|svg|webp|gif|ico)$/);
                }
              }
            });
          }
        });
      }
      if (typeof nextConfig.webpack === 'function') return nextConfig.webpack(config, options);
      return config;
    }
  });
};

const nextPlugins = [[optimizedImages, optimizedImagesConfig], optimizedImagesCSSFix];

...

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

No branches or pull requests

7 participants