A PDF tool doesn't need to load a PDF engine just to show someone a button.
That sounds obvious when you say it out loud. Somehow, my website was doing exactly that.
I run Pocket PDF, a browser-based collection of file tools. Across roughly 200 tool pages, I had pdf.min.js included as a regular dependency.
The file was around 320 KB.
Every visitor downloading a page that needed no PDF.js functionality was downloading that dependency anyway. Even if they never selected a file. Even if they only wanted to read the instructions and leave.
That's a fairly expensive way to say hello.
The problem with loading everything upfront
The original implementation treated PDF.js as a global dependency.
Every relevant page loaded the library during its initial page load, regardless of whether the user actually needed it.
There were two problems:
The browser had additional JavaScript to download and parse before the page was fully ready.
Visitors paid the loading cost before performing the action that required the library.
I wanted the PDF engine available when needed, not sitting in the critical loading path.
So I moved it behind a lazy-loading helper.
Introducing DP.lib
Instead of loading PDF.js through every page's initial HTML, I created a shared helper called DP.lib.
The idea is simple: tools request the library when they actually need it.
The helper handles loading and gives the tool access to the dependency.
Conceptually, the flow looks like this:
await DP.lib("pdfjs");
const pdfjs = window.pdfjsLib;
// Continue PDF-related work
The important difference is when the dependency gets loaded.
The initial page can render without downloading the PDF.js engine. The first action that needs PDF.js triggers the load.
This also gives me one place to manage shared dependencies instead of maintaining slightly different loading logic across multiple tools.
The interesting part was what broke
Lazy-loading a global dependency is not just a matter of moving a script tag.
Some existing code assumes that the dependency is already available.
I discovered this while testing three tools:
PDF to Text.
OCR.
Extract PDF Pages.
The particularly annoying issue was pdfPageGrid.
It had a hard dependency on window.pdfjsLib.
Previously, that global existed because PDF.js was loaded during the initial page load.
After changing the loading strategy, pdfPageGrid was still expecting the old behaviour.
The result was a broken dependency chain.
The page itself could load, but the component couldn't access the PDF.js global it expected.
The fix wasn't to undo lazy-loading. That would have defeated the entire point.
I fixed the component so it could trigger its own lazy-loading path when PDF.js wasn't available.
That distinction matters.
A shared component should not silently assume that another script has already executed just because that happened to be true in the previous implementation.
What I verified
I tested the affected tools rather than assuming that a successful homepage load meant everything was fine.
The verification covered:
PDF-to-Text processing.
OCR processing.
Extracting individual PDF pages.
The pdfPageGrid dependency.
First-load behaviour without immediately selecting a document.
The result was a much lighter initial loading path, with first paint coming in under a second in my testing.
The 320 KB PDF.js dependency no longer needs to be downloaded by every visitor before they can interact with the page.
Actual performance will still depend on network conditions, device speed and browser caching, but removing unnecessary work from the initial load is a useful improvement regardless.
What I'd take away from this
If you're building a website with a lot of tools, don't confuse shared dependencies with mandatory initial dependencies.
A library can be shared without being loaded on every page. The dependency graph you ship to the browser is also a design decision, and one of the easiest performance wins is simply stopping loading code the visitor may never need.
What's the heaviest library on your site that you're still loading everywhere?
Top comments (0)