Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

302 image redirects slower in browsers

I am on a WAMP stack and have the below one line of code for demo.html

<img src="http://localhost/redirect/demo.php"></img>

demo.php code as below

<?php
header("Location: http://localhost/redirect/blah");
exit();
?>

The code works fine. but there is huge response time during content download Response time using IMG tag

when I change demo.html to use script tag vs img tag, there are no problems during response times

<script src="http://localhost/redirect/demo.php"></script>

Response time using SCRIPT tag

Not sure why this is happening to IMG tags. Could anyone explain why this is happening and how do I avoid this? Are there any alternate methods to loading IMG via 302 without a javascript solution.

Note - believe this cannot be a PHP/WAMP problem as the response times are not affected when I call http://localhost/redirect/demo.php directly. Trust this has something to do with browser, its rendering, its load events.

like image 444
Faiz Mohamed Haneef Avatar asked Oct 28 '15 09:10

Faiz Mohamed Haneef


People also ask

How do I fix 302 redirect?

You can follow these five steps to fix HTTP 302 errors on your website: Determine whether the redirects are appropriate or not by examining the URLs that are issuing the 302 redirects. Check your plugins to make sure any redirect settings are valid. Ensure that your WordPress URL settings are configured correctly.

Does browser automatically redirect 302?

The 302 status code is a redirection message that occurs when a resource or page you're attempting to load has been temporarily moved to a different location. It's usually caused by the web server and doesn't impact the user experience, as the redirect happens automatically.

How does a 302 redirect work?

A 302 redirect does not pass the “juice,” or keep your domain authority to its new location. It simply redirects the user to the new location for you so they don't view a broken link, a 404 not found page, or an error page.

What is code 302 in HTTP?

The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.


1 Answers

If I'm not wrong, scripts are loaded synchronously whereas images are queued and loaded asynchronously.

So my understanding is if you use script tag, browsers wait to load http://localhost/redirect/demo.php which sends 302. This forces browser to execute http://localhost/redirect/blah before loading anything else.

Instead if you use img tag, browsers execute http://localhost/redirect/demo.php and continue to load remaining portion of the page. When demo.php returns 302, http://localhost/redirect/blah gets added to the queue of URLs to be loaded. Because of which the overall time to load the image is more.

Not sure if you can avoid it. Probably, enabling caching on demo.php could help in subsequent requests.

like image 187
Vivek Athalye Avatar answered Sep 21 '22 12:09

Vivek Athalye