A five-second video took about two to three minutes to generate in our project tests. That's long enough for someone to close the tab or lose their connection. Once the server has accepted the job, either can happen while the video is still rendering. The user can reopen the site and pick up where they left off.
The image on the left is a synthetic input portrait. The image on the right is a frame from the unedited video the app generated. The clip is part of our old photo animation examples.
| Synthetic input portrait | Frame from the generated video |
|---|---|
![]() | ![]() |
Hand off the job before returning to the browser
The browser uploads the photo, then sends POST /api/jobs with a reference to that upload. The Worker writes a queued job to D1 and creates a Workflow instance using the same job ID. Once the Workflow binding accepts the instance, the Worker returns 201 with the job ID and its initial status. If dispatch fails, the API returns an error.
The Workflow receives the job ID and loads the job from D1. That handoff is what lets the generation outlive the page. Closing the tab after the server accepts the job stops the browser's status requests. The Workflow has its own execution and can keep running.
Give each polling loop one job
While the tab is open, the browser asks GET /api/jobs/<id> for the status recorded in D1. The Workflow separately asks the video provider whether the render is done. Even with zero requests from the browser, the provider can finish rendering and the Workflow can fetch the result.
Each provider status check runs inside a step.do() with a name that includes the poll number. If the provider says the job is still running, the Workflow pauses with step.sleep() before checking again. Here's the shape of that loop, with the retry configuration and terminal-state handling omitted.
for (let attempt = 0; ; attempt++) { const result = await step.do(`poll:${attempt}`, async () => { return adapter.poll(upstreamJobId, pollWaitSeconds); }); if (result.state !== 'running') return result; await step.sleep(`pause:${attempt}`, 3_000); } The distinct names matter because Workflows persists completed step results and reuses them on replay. Each new poll needs its own step. The job deadline is checked inside the real poll step, so the Workflow can stop waiting and record a failure when the provider takes too long. Cloudflare Workflows step API
Submitting the render has a different retry problem. The provider could accept the request before the Workflow manages to save the provider's task ID. For a single render, the submit step checks D1 for an existing provider task ID and uses the local job ID as the provider's idempotency key. If the step retries before the task ID was saved, it sends the same key again. That protection depends on the provider honoring the key. Cloudflare's guidance on idempotent Workflow steps
Pick up where the user left off
When the provider reports success, the Workflow copies the video to R2 and then marks the D1 job succeeded with its output reference. The order matters. If the browser sees succeeded, the copy step has already finished.
On reopening, the page fetches GET /api/jobs to load recent jobs. The server finds them using the signed-in account or the guest cookie on the original browser. If a job is still running, the page resumes querying its status. If it finished while the tab was closed, the response already carries the result link.
A simple way to check this flow is to submit a job, wait for the 201, close the whole tab, and confirm in the Workflow logs that it finished before reopening the page. Then reopen with the same account or guest cookie and check that the existing video appears without submitting another render.
If your app makes users wait a few minutes for a result, how do you help them find it when they come back?
AI generated the first draft of this article from the project's code and documentation. Animate Old Photos AI is our project.


