When we include a script or CSS file in the HTML document, does an HTTP request occur?

Asked

Viewed 135 times

3

For example: <script src="file.js" > </script> or <link rel="stylesheet" href="style.css" >

Doubt: When we do this, it occurs request HTTP? It would be more performative if I didn’t do this, and put everything right into the HTML document?

3 answers

4


Yes, for each referenced file there will be a different request.

It would be more performative to upload everything in one file, even the images, saves some round Trips to the server and in the end it will be faster.

But it won’t necessarily offer a better user experience. It is possible that the user is waiting longer to see the assembled page since the load is unique. If you are bringing smaller things slowly you can already ride and start learning something first.

If the same things are needed on another page, and it is very common that this happens, very often, you will have to transmit again. Once this occurs the advantage has already been pro bog. Separating allows the reuse.

Today a lot of requests are made later via AJAX or something like that. More and more we see loading taking place in parts. Of course, everyone could be doing it wrong, but you think if everyone’s doing it that way it’s probably not so much better in most cases. Of course each case is a case. I don’t like it when everyone takes a unique path without seeing the context. Your case could benefit more if you put it all together, you’ll know...

2

Yes, there’s an http request for every call. On the question of performance it may be that yes, if it is something small, and it may not, if it is a lot, because instead of making several small requests, you will only make a large request, which may decrease performance rather than increase.

It is best to test by monitoring the Devtools Network tab and check how long your page is loaded.

Another thing you can do is minify your javascript and css files, so they get smaller and download faster.

0

Separate is better than Together

See how the flow works Browser <-> Server: You type the URL in the browser and the request (Request) is done to the server. The server, after processing whatever is on its side, will return ONE response (Response). The protocol (HTTP) works like this. A request -> An answer. There is no way to give you more than one response to a request.

What does the browser do? It processes the HTML and for each dependency it encounters will make new requests to the server to fetch the information it needs.

The issue of performing better or worse by putting everything in the body of the page and coming all in the same request depends on some factors, but overall it doesn’t make sense.

For example, will your server only serve 1 client? Your CSS or Script is staying at a CDN? You have a server control to tell the browser that the CSS or Script it is downloading has no change and you can use whatever is in the browser cache?

In most cases the answer is that Web servers are made to meet as many requests as possible in the shortest possible time and thus be able to have more clients served by the same process. And if your application does not collaborate to make this happen you will be killing the web server and probably your system will have scalability difficulties.

In short: The best thing would be for you to break the pieces instead of leaving everything the same HTML. #tamoseseated

Browser other questions tagged

You are not signed in. Login or sign up in order to post.