29

Why so many big and little sites inserts static files (CSS, images, JavaScript, ECC) in a subdomain like media.example.com or s2.static.example.com?

What are the advantages? Why not just a directory like example.com/media/?

migrated from stackoverflow.com Jan 23 '12 at 18:32

This question came from our site for professional and enthusiast programmers.

24

I see at least three possible (good) reasons:

  • Use another machine to serve the static content
    • Including some CDN
  • Use another web-server to serve the static content
    • Something more lightweight and faster
    • No need for a full PHP/.NET/JAVA server to serve static content!
  • Using another domain name means that you'll be able to not have the cookies that are used on the main domain

That's what is done on StackOverflow if I remember correctly

15

The primary reason I think, is for cookies.

Cookies will be sent along with every request, let's say if you have 2kb of cookie data and load 20 images on a page.

That's an extra 40kb of data, multiply that by the number of page-views you have in a month, and you might be surprised about how much bandwidth you have lost in something useless, and bandwidth is not free...

Also, cookies set on the top-level domain, are sent across all requests made to any subdomain, in those cases, is even recommended to buy a new domain to host the static components there, in a cookie-free domain.

For example, StackOverflow uses sstatic.net, Yahoo uses yimg.com, YouTube uses ytimg.com, Amazon uses images-amazon.com, etc.

Give a look to this:

8

To add to some of the answers above: some web browsers can only download two files simultaneously from any domain.

Serving static content from a different host name (or names - e.g. a.domain, b.domain, c.domain) allows these older browsers to download more files in parallel.

2

... use cookie-free domains for components.

When the browser makes a request for a static image and sends cookies together with the request, the server doesn’t have any use for those cookies. So they only create network traffic for no good reason. You should make sure static components are requested with cookie-free requests. Create a subdomain and host all your static components there.

This advise was taken from Yahoo's Best Practices for Speeding Up Your Web Site.

0

Just because your http request will be smaller and the server will run fast and the requested file will be given in a very small time which will ultimately lead to the fastest page load

  • This doesn't add anything that other answers don't already say. You don't even say why the request is smaller (because of no cookies), so this answer is worse than many others. – Stephen Ostermiller Sep 10 '17 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.