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.
Code
// 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 safeHow 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
Base64url CodecEncode and decode Base64url, the URL- and filename-safe variant that swaps + and / for - and _ and omits padding.Nano ID GeneratorGenerate URL-safe Nano ID strings with a configurable length and alphabet using the Web Crypto API.Query String ParserParse a query string or URL into decoded key-value rows, preserving repeated keys and empty values for debugging and API work.URL Slug GeneratorCreate readable URL slugs from titles and phrases with accent removal, configurable separators, and a maximum length while preserving Unicode letters.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.