Image Not Accepted Please Try Again
How to handle loading of images that may not exist?
When you are treatment millions of images on your website or mobile awarding, it is quite possible that over a menses of time, a few images may cease to exist. And when you endeavour to evangelize those images on your applications, the users finish up seeing a cleaved image like the 1 below.
Definitely, this is not something that you lot would want showing up on your application. There are a few ways on how you can fix this broken image problem to maintain the UX standards of your application.
1. Periodic information cleanup
If an prototype does not exist, then information technology should exist removed from the database.
An image URL that is incorrect or points to an image that no longer exists, is most probable a information issue. So the first thing that yous tin can exercise to counter the broken image trouble is to periodically check all images for sanity. You would selection up a list of epitome URLs that have been added over let's say the last week and check if y'all are able to retrieve those images (a 200 OK HTTP response). If yes, then the image stays in your database, else you tin discard the prototype.
Advantages — Solves the problem at the root level. One-time effort in writing the script
Disadvantages — Lengthy execution times (almost incommunicable) for millions of images, waste of network bandwidth and cannot catch errors for the fourth dimension between two consecutive runs of the cleanup task.
2. Handle it in your application
If an prototype does not exist, we tin can supplant it with a new one.
Some other way to handle this event is by listening to the error event that gets fired when the image fails to load. In HTML, this can exist done with the onerror aspect of the <img> tag.
<img src="http://example.com/non-existent-image.jpg" onerror="this.onerror=null;this.src='http://example.com/existent-image.jpg';" />
If the original image fails to load, then it is replaced by the image mentioned in the onerror attribute handler. Similar handling tin be washed in mobile apps likewise.
Advantages — Tin can handle data issues in existent-time, no need for periodic checks
Disadvantages — Won't piece of work for images loaded as background (at least for websites). Problems may continue in older versions of your website or application that cannot exist updated with the new code. Different code for unlike platforms.
three. Handle it on your image server
If the image does non exist, then the server should not send the mistake at all.
Your image server knows if an image does not exist and sends a 404 Not Found HTTP status code to the client. Instead, the server itself could replace information technology with a default image, correct the HTTP condition code and so send it to the browser or the application.
Advantages— Works in real time without whatsoever periodic checks. No handling needed on whatsoever awarding or whatever version of whatsoever application. Works for all kinds of images
Disadvantages — You lot need to build a server that tin handle this rerouting for you. Difficult, if non impossible, to go it working with unproblematic image 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 non-existent images. Using ImageKit's URL-based transformations, you tin can specify the default epitome that should be delivered, if the original image does non exist, with the original image URL itself. To requite an example,
<!-- The not-existent prototype URL -->
<img src="https://ik.imagekit.io/demo/img/non_existent_image.jpg" /> <!-- Specifying the default image to be displayed in the URL -->
<img src="https://ik.imagekit.io/demo/img/tr:di-medium_cafe_B1iTdD0C.jpg/non_existent_image.jpg" />
The default image is specified using the di-
parameter in the URL. In this instance, the default image is from a cafe.
Being a URL-transformation, this gives you lot the flexibility to specify a different default image for dissimilar kinds of images without having to write any code to handle the error cases. Yous tin can read more than about handling of non-existent images or default images using ImageKit here.
Considerations for image caching in error cases
Default images sent instead of a non-existent paradigm should not be cached at all or if it is cached, then the cache duration should exist small, preferably a few hours. This leaves an opportunity for the not-existent image to be "fixed" or fabricated available. When this happens, then the correct image would start getting delivered to the users automatically. However if the image is not expected to be stock-still, then the cache duration tin exist longer.
Considerations for response code in error cases
If you wish to handle the fault case in your application, then the image request should be returned with a 404 status code for the onerror to work. If y'all are delivering the default image instead of a cleaved image from the server, then the response lawmaking tin be 302 Temporarily Moved (ideally to prevent caching on intermediate layers like CDN but handling with success and mistake handlers in awarding is impaired) or 200 OK (caching on intermediate layers tin can be controlled with caching headers)
Since images class a critical part of our application, ensuring that the UX is non broken because of information issues with images is equally important. I hope the in a higher place techniques will help in ensuring a better feel for your users on your website and app.
Please share if y'all have any other ideas effectually handling images that no longer exist on a web application.
Source: https://blog.imagekit.io/how-to-handle-loading-images-that-may-not-exist-on-your-website-92e6c3c6ea63
0 Response to "Image Not Accepted Please Try Again"
Post a Comment