Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blob based 'link stylesheet' vs standard 'style' tag

I wonder what is the benefit and difference when using styles as Blob links:

<link rel="stylesheet" href="blob:http://www.domain.com/hash-here"> 

over the standard tag:

<style>...</style>

I mean Blob creation like:

var blob = new Blob([css_here], {type: 'text/css'});
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = window.URL.createObjectURL(blob);
head.appendChild(link);

Thanks in advance.

like image 732
juliancwirko Avatar asked Apr 17 '16 14:04

juliancwirko


People also ask

What is a blob URL and why it is used?

What is blob url? Why it is used? Blob URL/Object URL is a pseudo protocol to allow Blob and File objects to be used as URL source for things like images, download links for binary data and so forth. For example, you can not hand an Image object raw byte-data as it would not know what to do with it.

What does blob mean before a URL?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.

How do blob links work?

Blob URLs contain pseudo protocols that can create a temporary URL to audio and video files. This type of URL essentially acts as a fake source for the media on the website, so you can't download it directly. Instead, you have to use third-party conversion tools.


1 Answers

  • Memory management

If you put stuff as style, and then remove - it's gone. However, if you put stuff as blob url and then remove - you still have the blob url stored in memory, and it should be released manually. See the notes here: https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL#Notes

  • Relative paths resolution

With style all relative urls inside are resolved transparently (e.g. @font-face { src: url('/assets/font.ttf'); }. But with blobs, those relative urls are treated as relative to the blob url itself (i.e. relative to blob:http://domain/some-hash). So the relative urls will effectively stop working in this case.

like image 127
amakhrov Avatar answered Sep 22 '22 06:09

amakhrov