Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use data URI scheme?

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?

like image 630
Naftali Avatar asked Jul 25 '11 16:07

Naftali


People also ask

What is the use of data URI?

A data URI is a base64 encoded string that represents a file. Getting the contents of a file as a string means that you can directly embed the data within your HTML or CSS code. When the browser encounters a data URI in your code, it's able to decode the data and construct the original file.

What is data URI of image?

Introduction. A Data URL is a URI scheme that provides a way to inline data in an HTML document. Say you want to embed a small image. You could go the usual way, upload it to a folder and use the img tag to make the browser reference it from the network: <img src="image.png" />

How big can a data URI be?

Max URI length is 32KB. 2 Support is limited to images and linked resources like CSS or JS, not HTML files. Maximum size limit is 4GB.

How can I get URI data?

Right-click the image's URL (the cursor will turn into a hand) and choose Open link in Resources Panel. Right-click the image in the Resources Panel and choose Copy image as Data URL. Paste the Data URI wherever you need it.


1 Answers

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.

like image 106
Shaz Avatar answered Oct 17 '22 08:10

Shaz