Object URLs and resource lifetime
Quick answer
An object URL is a host-created URL referring to a Blob. It allows APIs that accept URLs to use local binary data without encoding the entire file as a string.
How it works
Call URL.createObjectURL(blob) and keep the returned URL for as long as consumers need it. Call URL.revokeObjectURL(url) when it is no longer used. For downloads or previews, revoke after the consumer has finished using the URL, not immediately before it reads. Object URLs are temporary references, not durable storage or public upload locations.
Local examples
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const url=URL.createObjectURL(new Blob(["x"]));try{console.log(url.startsWith("blob:"));}finally{URL.revokeObjectURL(url);}})()Expected console output:
trueThe generated identifier uses the blob scheme.
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const b=new Blob(["hi"]);const u=URL.createObjectURL(b);URL.revokeObjectURL(u);console.log(b.size,await b.text());})()Expected console output:
2 hiThe Blob remains independently usable.
Common mistakes
Do not persist object URLs in localStorage expecting them to survive a reload. Avoid leaking them through repeated previews.
Practice
Sketch a preview component that revokes its previous URL when the file changes and releases the final URL on cleanup.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Object URLs and resource lifetime — Knowledge Check
Apply the concept and reason about local example output.
When should an object URL be revoked?
Does revoking an object URL modify the Blob bytes?
Is an object URL a durable reference across reloads?
What is the expected console output of Object URLs and resource lifetime example 1?
(async () => {const url=URL.createObjectURL(new Blob(["x"]));try{console.log(url.startsWith("blob:"));}finally{URL.revokeObjectURL(url);}})()What is the expected console output of Object URLs and resource lifetime example 2?
(async () => {const b=new Blob(["hi"]);const u=URL.createObjectURL(b);URL.revokeObjectURL(u);console.log(b.size,await b.text());})()