Why use data URI scheme?

20,948

Solution 1

According to Wikipedia:

Advantages:

  • HTTP request and header traffic is not required for embedded data, so data URIs consume less bandwidth whenever the overhead of encoding the inline content as a data URI is smaller than the HTTP overhead. For example, the required base64 encoding for an image 600 bytes long would be 800 bytes, so if an HTTP request required more than 200 bytes of overhead, the data URI would be more efficient.

  • For transferring many small files (less than a few kilobytes each), this can be faster. TCP transfers tend to start slowly. If each file requires a new TCP connection, the transfer speed is limited by the round-trip time rather than the available bandwidth. Using HTTP keep-alive improves the situation, but may not entirely alleviate the bottleneck.

  • When browsing a secure HTTPS web site, web browsers commonly require that all elements of a web page be downloaded over secure connections, or the user will be notified of reduced security due to a mixture of secure and insecure elements. On badly configured servers, HTTPS requests have significant overhead over common HTTP requests, so embedding data in data URIs may improve speed in this case.

  • Web browsers are usually configured to make only a certain number (often two) of concurrent HTTP connections to a domain, so inline data frees up a download connection for other content.

  • Environments with limited or restricted access to external resources may embed content when it is disallowed or impractical to reference it externally. For example, an advanced HTML editing field could accept a pasted or inserted image and convert it to a data URI to hide the complexity of external resources from the user. Alternatively, a browser can convert (encode) image based data from the clipboard to a data URI and paste it in a HTML editing field. Mozilla Firefox 4 supports this functionality.

  • It is possible to manage a multimedia page as a single file. Email message templates can contain images (for backgrounds or signatures) without the image appearing to be an "attachment".

Disadvantages:

  • Data URIs are not separately cached from their containing documents (e.g. CSS or HTML files) so data is downloaded every time the containing documents are redownloaded. Content must be re-encoded and re-embedded every time a change is made.

  • Internet Explorer through version 7 (approximately 15% of the market as of January 2011), lacks support. However this can be overcome by serving browser specific content. Internet Explorer 8 limits data URIs to a maximum length of 32 KB.

  • Data is included as a simple stream, and many processing environments (such as web browsers) may not support using containers (such as multipart/alternative or message/rfc822) to provide greater complexity such as metadata, data compression, or content negotiation.

  • Base64-encoded data URIs are 1/3 larger in size than their binary equivalent. (However, this overhead is reduced to 2-3% if the HTTP server compresses the response using gzip) Data URIs make it more difficult for security software to filter content.

According to other sources - Data URLs are significantly slower on mobile browsers.

Solution 2

A good use of Data URI is allowing the download of content that have been generated client side, without resorting to a server-side 'proxy'. Here are some example I can think of:

  • saving the output of a canvas element as an image.
  • offering download of a table as CSV
  • downloading output of any kind of online editor (text, drawing, CSS code ...etc)

Solution 3

I have used the data URI scheme in several (C++, Python) command line applications to generate reports which include data plots.

Having a single file is quite convenient to send the reports by email (or move them around in general). Compared to PDF I did not need an additional library (other than a base64 encoding routine) and I don't need to take care of page breaks (and I almost never need to print these reports). I usually don't put these reports on a web server, just view them on the local filesystem with a browser.

Solution 4

Mainly I find use of this if I can't (for some reason) use CSS sprites and I don't want to download every little single image that I will use for styling.

Or for some reason you don't want anyone to link the image from external page. This can be achieved by other methodologies but embedding works as well.

Otherwise, personally I wouldn't encode large images as photos. It's better to have your media at a different server. A server that can lack all of the web-server related software installed. Simply delivering media. Much better use of resources.

Solution 5

I agree with BiAiB that the real value of Data URIs is making client-side generated content available as file download without any need for server round-trips.

A working example of using Data URIs for "offering download of a table as CSV" is described on my blog.

IMHO, the embedding of image (or other binary resource) data into an HTML file for performance reasons is a red herring. The speed gain due to less HTTP connections is negligible and breaks the nice principle of separation between (textual) markup and binary resources (image files, videos, etc.).

I think HTTP 1.1 pipelining and some suggested improvements to HTTP are a cleaner and better way to handle HTTP network speed issues.

Share:
20,948
Naftali
Author by

Naftali

I AM HERE G+: https://plus.google.com/114132678747122742656/posts Baseball anyone? http://jsfiddle.net/maniator/K3wCM/embedded/result/ Baseball v2 anyone? https://blipit.net/ WOF anyone? http://jsfiddle.net/maniator/XP9Qv/embedded/result/ Maybe some snake? https://snace.herokuapp.com Or even minesweeper? https://mineweeper.herokuapp.com I am am usually here I am writing here Neal @ Miaou #SOreadytohelp

Updated on July 09, 2022

Comments

  • Naftali
    Naftali almost 2 years

    Basically the question is in the title.

    Many people have had the question stackoverflow of how to create a data URI and problems therein.

    My question is why use data URI?

    What are the advantages to doing:

    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
    AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
    9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
    

    Over doing:

    <img src="dot.png" alt="Red dot" />
    

    I understand one has less overhead on the server side (maybe), but what are the real advantages/disadvantages to using data URI?