Home > database >  Next.js return (failed)net::ERR_UNKNOWN_URL_SCHEME for imported images
Next.js return (failed)net::ERR_UNKNOWN_URL_SCHEME for imported images

Time:09-22

I am using Next.js styled-components TypeScript. I want to use background-image: url() where I import image from assets folder present inside src folder. In network tab, when I look for images, it says in status:

(failed)net::ERR_UNKNOWN_URL_SCHEME and the url is src:/_next/static/image/public/right.e158775f9c1cf296e36177bfb86a5ced.svg;height:32px;width:32px;

Which does not display image.

Assets folder is present in /src/assets/left.svg.

If I import the same image in index.tsx file and render it in Image component it works fine.

// styles.tsx

import styled from 'styled-components';
import Left from '../../assets/left.svg';
import Right from '../../assets/right.svg';

export const SwiperWrapper = styled.div`
  margin-top: 2.5rem;
  position: relative;
  .swiper-position {
    position: static !important;
  }
  .swiper-button-next:after,
  .swiper-button-prev:after {
    content: '';
  }
  .swiper-button-prev {
    background-image: url(${Left});
    background-repeat: no-repeat;
    background-size: 100% auto;
    background-position: center;
    -webkit-tap-highlight-color: transparent;
    top: 100%;
  }
  .swiper-button-next {
    background-image: url(${Right});
    background-repeat: no-repeat;
    background-size: 100% auto;
    background-position: center;
    -webkit-tap-highlight-color: transparent;
  }
`;

CodePudding user response:

That is because in Nextjs, static files are served from the 'public' folder in the root directory. I suggest you store the images in this folder and reference them with a "/"

eg background-image: url('/Right');

  • Related