When an agent's 3 GB walkthrough upload in a real-estate listing portal stops at 1.8 GB after a dropped uplink, an expired URL or a reload, how does the page resume?
answer
- the server's count, never yours
- keep the upload's identity, not its bytes
- fresh authority first, then the offset
- re-pick the file and match it
- warn while in flight, save when hidden
basics
~20 sAsk the server how many bytes it holds and send the rest from that offset with Blob.slice. Surviving a reload also needs the saved upload id and file fingerprint, a fresh URL, and the agent choosing the file again.
solid answer
~50 sAll three causes end on one path: get valid authority, ask the server for its offset, slice the file from there. The page's own count is wrong both ways (sent bytes may never have landed; a chunk may have landed after its response was lost), so resume from an offset query, such as `Upload-Offset` in the IETF resumable-uploads draft, the direction the protocol is taking. After a drop the page still has the `File`: back off, query, continue. After an expiry 403 it gets a fresh URL for the same upload id and retries the chunk, not the file. For a reload, the page saved the upload id and the file's name, size and last-modified time in IndexedDB at the start; it shows "1.8 of 3.2 GB sent, choose the file again", matches the re-picked file and continues. While bytes are in flight it warns before leaving and saves state on `visibilitychange`.
code
javascript · 36 lines// After a reload: the agent re-picked the file. Offset headers follow the IETF resumable-uploads draft.
function readOffset(response, size) {
const value = response.headers.get("Upload-Offset");
if (value === null) throw new Error("No offset from server; query again before sending");
const offset = Number(value);
if (offset > size) throw new Error("Server holds more than this file; start a new upload");
return offset;
}
async function resumeUpload(saved, file, api) {
if (file.name !== saved.name || file.size !== saved.size || file.lastModified !== saved.lastModified) {
throw new Error("Different file; start a new upload");
}
const { uploadUrl } = await api.refreshUpload(saved.uploadId); // the old URL has probably expired
const head = await fetch(uploadUrl, { method: "HEAD" });
if (!head.ok) throw new Error(`Offset query failed: ${head.status}`);
let offset = readOffset(head, file.size); // the server's count, not the page's
let done = false;
while (!done) {
const end = Math.min(offset + saved.chunkBytes, file.size);
const last = end === file.size; // an empty last slice still marks the upload complete
const res = await fetch(uploadUrl, {
method: "PATCH",
headers: {
"Content-Type": "application/partial-upload",
"Upload-Offset": String(offset),
"Upload-Complete": last ? "?1" : "?0",
},
body: file.slice(offset, end),
});
if (res.status === 409) { offset = readOffset(res, file.size); continue; } // take the server's offset
if (!res.ok) throw new Error(`Append failed: ${res.status}`); // the queue decides retry or stop
offset = end;
done = last;
}
}go deeper
Recall that a resumed upload continues from the offset the server reports and that Blob.slice sends just the remaining byte range.
Explain why the page's own count can be too high or too low, what an offset query returns, and how a 403 from an expired URL is recovered without restarting the file.
Show the reload design: what is persisted and what is not, re-picking and fingerprint matching, fresh authority before the offset query, and saving on visibilitychange because beforeunload fails on phones.
Weigh where resumability is owned: a standard offset protocol keeps the page simple across storage back ends, while a vendor's own protocol ties the queue to it. Push for one contract the page can rely on.
## One resume path for three failures An agent is uploading a 3.2 GB walkthrough video for a listing from a phone browser on a weak mobile uplink. At 1.8 GB something stops it. The causes differ, but the recovery is the same three moves: **re-establish authority, ask the server where it stands, continue from there.** | What stopped it | What the page still has | First move | |---|---|---| | Dropped uplink | the `File`, the upload id, a valid URL | back off, then query the offset | | Expired pre-signed URL (403 mid-upload) | the `File`, the upload id | ask the API for a fresh URL for the same upload, then query the offset | | Reloaded or closed tab | only what it saved to storage | read the saved entry, have the agent re-pick the file, get a fresh URL, query the offset | ## Why the server's offset and never the page's count The page's counter (bytes reported by progress events) is wrong in both directions: - bytes that left the browser may never have reached storage when the connection dropped; - a chunk may have been stored even though its response was lost, so the server holds **more** than the page believes. So the page asks. In the IETF HTTP working group's resumable-uploads draft (a draft that shows where the protocol is heading, not a deployed standard), the client sends a `HEAD` request to the upload resource and reads the `Upload-Offset` response header. The draft is explicit that the offset can be lower than what the client sent, so the client must be able to back up, or higher, and that each append carries the offset it starts from, with a `409 Conflict` and the correct offset if the two disagree. It also forbids querying the offset while an append to the same upload is still running, so the page cancels or waits for the in-flight request first. A storage service with its own resumable protocol has an equivalent "what do you have?" call; the principle is the same. With the offset in hand, `file.slice(offset, end)` returns a new `Blob` over just that byte range, without reading the earlier 1.8 GB into memory. ## What the page must keep to survive a reload A reload discards every variable, so before the first byte is sent the page writes an entry to IndexedDB: 1. the **upload id** the portal's API issued (the durable handle for the server's partial upload); 2. the listing id and the file's kind; 3. a **fingerprint**: the file's `name`, `size` and `lastModified` time; 4. the state and the time of the last confirmed offset, for display only. What it deliberately does not keep: - **The pre-signed URL.** It will probably have expired by the time the agent returns, and it is a working credential that should not sit in storage. - **The file's bytes.** Browser storage is best-effort by default and may be evicted under pressure, and a 3 GB copy on a phone is exactly what gets evicted or refused. The design should not depend on a stored `File` handle still being readable after a reload. The safe flow is to show the saved row ("walkthrough.mp4, 1.8 of 3.2 GB sent"), ask the agent to choose the file again, compare the fingerprint, and only then ask for a fresh URL and the offset. A file whose size or modified time differs is a different file and starts a new upload. ## An expired URL mid-upload Pre-signed URLs live minutes; a 3 GB upload on a weak uplink lives an hour. Two habits keep expiry from failing the file: - **Refresh ahead of time.** The API returns an expiry with each URL; when it is close, fetch the next URL before starting the next chunk. - **Recover on 403.** If a chunk fails with 403, ask for a fresh URL for the same upload id, query the offset, and retry from there. If the fresh URL also fails, stop and report; that is no longer expiry. How the server issues and scopes those URLs is the valet-key pattern's subject; the page only renews them. ## Leaving and backgrounding the page - **Warn before leaving.** Add a `beforeunload` listener only while bytes are in flight and remove it afterwards; the prompt needs the agent to have interacted with the page and is not reliably fired on mobile, and in some browsers the listener keeps the page out of the back/forward cache. - **Save on hidden.** `visibilitychange` to hidden is the dependable moment to write the latest state. - **Expect a frozen or discarded tab.** A hidden phone tab may be frozen, so timers and network callbacks stop, or discarded outright. On return, query the offset and continue; tell the agent to keep the tab open in front during a long upload. Transfers that continue with the app closed are an installed app's tool.
- Why not store the pre-signed URL and reuse it after the reload?It has probably expired by the time the agent returns, and it is a live credential that would sit in browser storage, where anything running on the origin can read it. The upload id is the durable handle; the page exchanges it for a fresh URL from the portal's API, which also re-checks that the agent may still write to this listing.
- The offset query says 1.75 GB, but the page had sent 1.8 GB. What now?Nothing is wrong: the last 50 MB never landed. Slice from 1.75 GB and continue; the draft expects clients to back up a reasonable distance. The opposite case, an offset beyond the file's size, means the saved entry and the file do not match, so stop and start a new upload rather than guess.
- Can the upload carry on while the agent's phone browser is in the background?Do not count on it. A hidden tab may be frozen, which stops timers and network callbacks, or discarded under memory pressure. Save state on `visibilitychange`, and on return query the offset and continue. The honest UX is a clear "keep this tab open" note during the video; surviving a closed app is an installed client's job.
- Resume from the byte count the page's progress events last reported
- Store the 3 GB file's bytes in IndexedDB so a reload can resume without re-picking
- Keep the pre-signed URL in storage and reuse it after a reload
- Rely on beforeunload to save upload state on phones
- An expired URL means restarting the whole file from byte zero