'Next.js return (failed)net::ERR_UNKNOWN_URL_SCHEME for imported images
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;
}
`;
Solution 1:[1]
Instead of moving all our images to public folder you can just use
background-image: url(${Left.src});
This is what worked for me:)
Note:
Actually Left will be an object if you try to console.log it, it will be something like:
{
blurDataURL: "/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Fleft.475f00e5.png&w=8&q=70"
height: 1321
src: "/_next/static/media/left.475f00e5.png"
width: 1321
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | valerii15298 |
