When importing images in Next.js, the error message "failed to load resource: net::ERR_UNKNOWN_URL_SCHEME

Currently utilizing Next.js with styled-components and TypeScript. I am trying to implement background-image: url() by importing an image from the assets folder located within the src directory. However, when inspecting the network tab, the status for images shows:

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

This results in the image not being displayed properly.

The assets folder can be found at /src/assets/left.svg.

If I import the same image into the index.tsx file and display it using the Image component, everything works as expected.

// 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;
  }
`;

Answer №1

The reason for this behavior in Next.js is that static files are accessed from the 'public' directory located in the root of the project. To efficiently handle images, it's recommended to save them in the 'public' folder and then refer to them using "/".

For example: background-image: url('/Right');

Answer №2

Instead of relocating all our images to the public folder, a simpler solution would be to implement

background-image: url(${Left.src});

This method has proven successful for me :)

Just a little note: If you happen to console.log the object Left, it will look something like this:

{
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
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a custom CSS counter-style reminiscent of the toLocaleString function

Is there a way to customize the list counter style to display commas in the thousands place, similar to number.toLocaleString()? Or perhaps even adjust based on locale to show either 1,234 or 1.234? I have checked the Predefined counter styles but did not ...

Problem occurring with Bulma CDN version 0.8.0 failing to identify the spacing helper

Recently revisited an older project I had started some time ago using the Bulma framework. However, for some reason, the 0.8.0 CDN is not being imported or recognized correctly by the spacing classes. Here's the code snippet I had initially: <link ...

Avoiding conflicts with original CSS when implementing Mobile-First Media Queries

Suppose I have this CSS code: .about_text { font-weight: 300; font-size: 1.125rem; } Then, I add a media query like this: @media screen and (min-width: 64em) { .about_text { font-weight: 600; font-size: 1.500rem; } } Despite ...

How to Retrieve the Default Value in a React Hook?

I have a certain input field with a default value assigned to it. <input type="text" name="default" value="one" /> To handle the state of this input, I am utilizing a react hook. const [def, setdef] = useState({Defaul ...

Tips for aligning a div in the center while adjusting screen size

My goal is to showcase a dashboard with a card layout. The desired layout can be seen https://i.sstatic.net/OWgpE.png: Here is a snippet from the HTML File: <div id="overallCanvasWrapper" class="statistic-container"> <p ng-show=" ...

Issue with unique scrollbar customization tool

To access the plugin, visit: My webpage is still a work in progress: XXX Both divs move simultaneously when scrolling down! I am unfamiliar with jQuery and java, can someone assist me? ...

Easily adjust the size of your font using a sleek Material UI slider in combination with

I've been working on creating a basic slider to adjust the font size using React and Material UI. The specific slider I'm using is called PrettoSlider, which is a component of Material UI. My goal is to incorporate the slider in order to dynamic ...

Learn the step-by-step process of cropping and zooming an image using the react-image-crop

I am looking to implement zooming and cropping functionality for an image using react-image-crop instead of react-easy-crop. Currently, the cropping does not take into account the zoom level set by ReactCrop's scale property. I want to be able to zo ...

Utilizing jQuery to apply a class to an element and modify the CSS parameters with custom values simultaneously

Unsure if this idea is feasible, but I'll attempt to explain my requirements. I am interested in accomplishing something like the following: A CSS class with variables X, Y, and Z left undefined: .r {border-radius:Xpx;-webkit-border-radius:Ypx;-moz ...

Choose class based on the retrieved information

Good morning, I am working on dynamically setting the class of a td element based on data fetched from a database to reflect any changes or updates. One of the fields returned has four possible options, and I need to modify the CSS class of that field acc ...

To prevent using helperText to adjust the width of an input field, MUI recommends implementing

When utilizing the helpertext feature of the mui textfield component for validation errors, I encountered an issue where the helpertext was causing the width of the textfield to expand. This occurred even when using the "fullWidth" attribute. If there are ...

Styles and CSS have been successfully loaded, however, the content is not appearing

After attempting to import a web HTML CSS template into my self-made MVC, I encountered an issue. While the template works properly on its own, it fails to connect to the CSS files and display proper styles when accessed through my controller. Instead, onl ...

How can I make a div collapse in the same way as the Facebook chat

I am working on creating a chat feature similar to Facebook chat for personal use. Everything is functioning correctly, except for some issues with the CSS. I attempted using position absolute to arrange the div, which worked fine, but encountered problem ...

React: Enzyme lacks functional local scope

As a React user, I recently implemented a basic redux counter using redux-toolkit. Testing is new to me, and I am currently utilizing Enzyme and Jest for testing purposes. The initial state of my redux counter is set to 1. During testing, within the it sco ...

Toggle visibility of div element with a button press

Here is the div I have: <div class="margin-bottom hidden-xs"></div> This specific div is initially hidden on a small window size due to the bootstrap 'hidden-xs' class. However, I want it to be shown or hidden when the user clicks o ...

Accessing UPI apps such as Google Pay through deep linking from a web application

I am currently exploring the possibility of deep-linking to individual UPI apps, like Google Pay, that are installed on a user's phone. The goal is for users to be seamlessly redirected to their preferred UPI app when they click on the respective icon ...

Resize the div blocks by scaling the button placed beneath them

I decided to modify the g-recaptcha widget to fit my specific requirements, and it is working flawlessly. However, I noticed that the ng-click button directly beneath it is no longer responsive. The reason behind this issue eludes me. Here is a snippet of ...

Detecting when the Ctrl key is pressed while the mouse is hovering over an element in React

In my project, I have implemented a simple Grid that allows users to drag and drop items. The functionality I am trying to achieve is detecting when the mouse is positioned on the draggable icon and the user presses the Ctrl key. When this happens, I want ...

Utilizing erb within a coffeescript file for modifying the background styling

Is there a way to change the background image of a div based on user selection from a dropdown menu? For instance, if the user picks "white" the background image becomes white, and for "red" it changes to red. I'm struggling with this in coffeescript ...

`fetch` functionality does not function properly when included in middleware page after deployment on Vercel

On a specific route, I have a middleware page that first checks against the backend server. Within this middleware, I am attempting to call a next.js api page using the fetch API, which then communicates with the backend. This process functions correctly i ...