Javascript required
Skip to content Skip to sidebar Skip to footer

404 Server Error When Uploading Background Image in React

When you are handling millions of images on your website or mobile awarding, it is quite possible that over a menstruum of time, a few images may cease to exist. And when y'all endeavor to deliver those images on your applications, the users end up seeing a broken image like the one below.

A sample page with broken images as seen on Google Chrome browser

Definitely, this is not something that you lot would want showing up on your application. At that place are a few means on how yous tin can prepare this broken image problem to maintain the UX standards of your application.

1. Periodic information cleanup

If an prototype does not be, then it should be removed from the database.

An image URL that is incorrect or points to an image that no longer exists, is most likely a data issue. So the start thing that you tin can practice to counter the broken prototype problem is to periodically check all images for sanity. Y'all would option up a list of image URLs that have been added over let's say the last calendar week and check if you are able to retrieve those images (a 200 OK HTTP response). If yes, so the image stays in your database, else you tin discard the epitome.

Advantages — Solves the problem at the root level. One-fourth dimension effort in writing the script

Disadvantages — Lengthy execution times (most impossible) for millions of images, waste matter of network bandwidth and cannot catch errors for the time betwixt ii consecutive runs of the cleanup task.

two. Handle it in your application

If an image does not exist, we can replace it with a new ane.

Another way to handle this issue is past listening to the fault event that gets fired when the image fails to load. In HTML, this can be washed with the onerror aspect of the <img> tag.

          <img src="http://instance.com/not-existent-paradigm.jpg" onerror="this.onerror=null;this.src='http://case.com/existent-image.jpg';" />        

If the original prototype fails to load, and so it is replaced by the epitome mentioned in the onerror attribute handler. Like treatment tin be done in mobile apps as well.

Advantages — Tin handle data issues in existent-time, no need for periodic checks

Disadvantages — Won't work for images loaded as background (at to the lowest degree for websites). Bug may continue in older versions of your website or application that cannot be updated with the new lawmaking. Dissimilar code for different platforms.

3. Handle information technology on your epitome server

If the epitome does not be, then the server should not send the mistake at all.

Your image server knows if an image does non exist and sends a 404 Not Found HTTP status lawmaking to the client. Instead, the server itself could supersede information technology with a default image, right the HTTP status code then send it to the browser or the application.

Advantages— Works in real time without whatever periodic checks. No handling needed on whatever application or any version of whatever application. Works for all kinds of images

Disadvantages — Yous need to build a server that can handle this rerouting for you. Difficult, if non impossible, to go it working with simple epitome delivery setups like CDN + S3 storage.

4. Handle it using a third-party image server like ImageKit.io

ImageKit.io automatically provides server-side handling of not-real images. Using ImageKit's URL-based transformations, you can specify the default image that should be delivered, if the original image does not be, with the original image URL itself. To give an case,

          <!-- The non-real epitome URL -->
<img src="https://ik.imagekit.io/demo/img/non_existent_image.jpg" />
<!-- Specifying the default prototype to exist displayed in the URL -->
<img src="https://ik.imagekit.io/demo/img/tr:di-medium_cafe_B1iTdD0C.jpg/non_existent_image.jpg" />

The default prototype is specified using the di- parameter in the URL. In this case, the default image is from a buffet.

Deliver a default image, the one on the right, instead of the broken epitome, the one on the left.

Existence a URL-transformation, this gives you the flexibility to specify a different default image for different kinds of images without having to write whatever code to handle the fault cases. Y'all can read more about handling of non-existent images or default images using ImageKit here.

Considerations for epitome caching in fault cases

Default images sent instead of a non-existent prototype should not be cached at all or if it is cached, then the enshroud duration should be small, preferably a few hours. This leaves an opportunity for the not-existent epitome to be "fixed" or made available. When this happens, then the correct image would first getting delivered to the users automatically. However if the image is non expected to be stock-still, then the cache duration tin can be longer.

Considerations for response code in mistake cases

If you wish to handle the fault case in your application, then the image asking should exist returned with a 404 status code for the onerror to work. If you are delivering the default paradigm instead of a broken prototype from the server, so the response code can be 302 Temporarily Moved (ideally to forbid caching on intermediate layers like CDN but handling with success and error handlers in awarding is impaired) or 200 OK (caching on intermediate layers can exist controlled with caching headers)

Since images class a disquisitional function of our awarding, ensuring that the UX is not broken because of data issues with images is equally important. I hope the above techniques volition assistance in ensuring a better feel for your users on your website and app.

Delight share if you lot accept any other ideas around handling images that no longer exist on a web awarding.

begglere1964.blogspot.com

Source: https://blog.imagekit.io/how-to-handle-loading-images-that-may-not-exist-on-your-website-92e6c3c6ea63