question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SCSS modules cannot load images correctly with `next-images` package

See original GitHub issue

Update: Read my other comments below for a more pinpointed problem.

Bug report

Describe the bug

I tried to set a background image in a SCSS module with background: url("./footer-region-bg.jpg") repeat; (is in the same directory as the component). But this results in the following when fetching the image.

image

This was working with @zeit/next-sass, but it doesn’t with the native scss modules.

To Reproduce

Load an Image from the same directory as the component in an SCSS module like mentioned above. Use the native SCSS modules from NextJs.

Expected behavior

The image should be loaded correctly

System information

  • OS: Windows
  • Version of Next.js: 9.3.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
theshemcommented, Mar 23, 2020

I’m not sure if it is a next-images issue or the Next.js itself.

I faced the same issue after upgrading from 9.1.7 to 9.3.1. I was using next-sass and url-loader with the custom webpack config.

By using the built-in Sass feature, background and font face url()s inside .scss files stop working. After digging into Next.js files I realized that it handles url() imports through the file-loader now.

https://github.com/zeit/next.js/blob/v9.3.1/packages/next/build/webpack/config/blocks/css/index.ts#L278-L292

https://github.com/zeit/next.js/blob/v9.3.1/packages/next/build/webpack/config/blocks/css/index.ts#L17

I hadn’t used the built-in CSS feature before and that is why I hadn’t faced the issue.

Workaround

I ended up with excluding the url() file imports by setting a test regex in webpack issuer config as follows:

// next.config.js
module.exports = {
  // ...
webpack: (config) => {
    config.module.rules.push({
      issuer: {
        // nextjs already handles url() in css/sass/scss files
        test: /\.\w+(?<!(s?c|sa)ss)$/i,
      },
      test: /\.(jpg|gif|png|svg|ico)$/i,
      use: [
        {
          loader: 'url-loader',
          options: {
            // sample options
            limit: 8192,
            outputPath: '...',
            context: 'src',
            name: '[path][name].[hash:8].[ext]',
            publicPath: `your/public/path`,
          },
        },
      ],
    });

    return config;
  },
  // ...
};

Hope this helps.

5reactions
orxanaliyevcommented, Feb 1, 2021

Solved!

style.module.scss

.Home-Page-Header {
  border: 1px solid red;
  background-image: url("/static/images/header.jpg");
}

next.config.js

module.exports = {

  webpack: (config) => {
    config.module.rules.push({
      test: /\.(png|jpg|gif)$/i,
      use: [
        {
          loader: "url-loader",
          options: {
            limit: 8192,
          },
        },
      ],
    });

    return config;
  },

};

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to import images in nextjs using next images package
There is no need to load the image via the import if you add the images to /static/. You can use like this...
Read more >
next-images - npm
next-images package contains definitions for image modules, you need to add reference to next-images types into a type definition file, e.g. ...
Read more >
next/image - Next.js
The default loader does not optimize SVG images for a few reasons. First, SVG is a vector format meaning it can be resized...
Read more >
All you need to know about images in Next.js - DatoCMS
In other words, treating Next.js images correctly is now vital. ... But thanks to the <Image> component, you do not have to worry...
Read more >
Next.js image optimization techniques | Uploadcare Blog
Not so long ago, Next.js developers added an Image component to the framework which was aimed to replace the native HTML image and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found