How to display a loading animation while file is generated for download?

How to display a loading animation while file is generated for download?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to display a loading animation while file is generated for download?

Generating large files like PDFs or PowerPoint presentations directly in memory and pushing them straight to the browser often results in a frozen interface. The browser hangs while waiting for the response body, and by the time the data stream finishes, the user is left wondering if the application crashed. You might want to show a loading spinner during this generation phase, but there is a fundamental architectural hurdle: standard server-side rendering blocks the request until completion. Once the file is generated, the server sends it to the browser in one go. If you try to render a loader on the client side before receiving the response, your window.location.href redirect or direct download header will terminate the page immediately, hiding the animation instantly.

The core issue described in many scenarios is the lack of a signal indicating when the generation process has successfully completed and the file stream has begun downloading. In a synchronous request cycle, the client cannot distinguish between "generation is ongoing" and "the file is currently being streamed." The browser assumes the download started immediately upon receiving headers, so any UI element suggesting "processing" is technically redundant if it appears before the headers are sent.

To solve this while maintaining a locked user experience, you must decouple the generation process from the immediate response delivery. This typically involves an asynchronous workflow where the server initiates the heavy lifting in the background and notifies the client once the file is ready.

Asynchronous Generation with Job Queues

The most robust approach for applications like Laravel (where this pattern is common) is to offload the file generation to a queue worker. Instead of generating the PDF or PPTX immediately within the HTTP request lifecycle, you trigger a background job.

  1. Trigger the Job: Upon user request, your controller creates a unique identifier (UUID) and dispatches a queue job containing the necessary parameters (e.g., data source, template ID).
  2. Return Instantly: The controller returns an immediate JSON response to the frontend confirming that the job has been queued. At this exact moment, you can safely render your loading animation because the heavy processing is not blocking the HTTP response anymore.
  3. Handle Completion: The background worker processes the file, saves it to disk or storage, and then dispatches a completion event or updates a status table entry.

The frontend polls this endpoint or listens for WebSocket events to determine when the file is ready. Once the job finishes, the server responds with the download URL or triggers a redirect. This ensures the loading animation appears exactly when the user needs it (during the wait time) and disappears precisely when the file is available for download.

Trade-offs and Pitfalls

While queuing provides a smooth UX, it introduces specific complexities you must manage:

  • Storage Cleanup: If generation fails or times out, ensure your cleanup logic removes temporary files from disk to prevent storage bloat.
  • Polling Overhead: Simple AJAX polling can waste bandwidth. Implement exponential backoff strategies or use WebSockets/Server-Sent Events for real-time updates instead of constant polling.
  • User Impatience: If the generation takes too long (e.g., over 10 minutes), a simple "loading" state may become frustrating. Consider providing progress estimates or estimated time remaining if your job queue supports it.

By shifting from a synchronous response to an asynchronous workflow, you gain the ability to control the UI lifecycle independently of the file generation duration. The loading animation becomes meaningful because it reflects the actual state of the system: "We are working on this, and we will let you know when it is done."

Our network