Object URLs Bind Blobs Until Revoked

createObjectURL binds a Blob to a blob: URL that stays valid until revokeObjectURL releases it; repeat calls create distinct URLs and double revocation is safe.

JavascriptFile Upload

Code

javascriptsnippet
19 lines
// createObjectURL binds a Blob to a blob: URL; revokeObjectURL unbinds and frees it -
// they must come in pairs, otherwise the reference pins the underlying resource
const blob = new Blob(['report,line,amount\n1,coffee,350'], { type: 'text/csv' });
const url = URL.createObjectURL(blob);

console.log('scheme:', url.split(':')[0]);
console.log('blob size:', blob.size, 'bytes');

// Calling createObjectURL on the same Blob again produces a fresh URL
const second = URL.createObjectURL(blob);
console.log('urls are distinct:', url !== second);

// After revocation the URL is dead - no navigation, download, or preview through it
URL.revokeObjectURL(url);
URL.revokeObjectURL(second);

// Revoking twice is a safe no-op: cleanup logic can call it liberally
URL.revokeObjectURL(url);
console.log('both urls revoked, double revoke is safe');

Output

Captured from a sandboxed run of this snippet in the build pipeline.

scheme: blob
blob size: 30 bytes
urls are distinct: true
both urls revoked, double revoke is safe

How the snippet works

Module-level bindings hold the data — blob, url, second — with no function scope in between.

Using it

The demo output is labeled (scheme:, blob size:, urls are distinct:), so every printed line maps back to its originating operation.

Key statements

const blob = new Blob(['report,line,amount\n1,coffee,350'], { type: 'text/csv' });
const second = URL.createObjectURL(blob);
const url = URL.createObjectURL(blob);

Interactive Tools for This Topic

FAQ

Is there an install step between copy and run?
No. Between copying and running there is nothing left to do. Its state lives in module-level bindings: `blob`, `url`, `second`.