My page looked ready after 2 seconds. It worked after 27.
I run PDFUmbrella, a small website with free PDF tools. I made it because merging two files shouldn't mean uploading your private papers to someone else's server. Everything on it runs in the browser. This post is about
I run PDFUmbrella, a small website with free PDF tools. I made it because merging two files shouldn't mean uploading your private papers to someone else's server.
Everything on it runs in the browser. This post is about one page of it, which turns photos into a PDF, and about three people who tried to use it on their phones.
One stayed for 82 seconds, tapped 18 times and left. One gave up after 11 seconds and 10 taps. The third tapped 15 times, reloaded the page three times, and then finally got their PDF.
There was no error, because nothing had gone wrong. The page just wasn't working yet.
Looking ready and being ready
The page is prerendered: the HTML is built in advance, so search engines and visitors get the full page straight away. React arrives later and takes over.
Between those two moments there's a big blue "Choose photos" button that does nothing. On a fast connection that gap is too short to notice. This is what I measured in a phone-sized test browser slowed down to 400 kbit/s:
- The page was on screen after 1.9 seconds.
- The button worked after 27.1 seconds.
So for 25 seconds it was a picture of a website. Prerendering hadn't caused the wait, but it was hiding it.
How I found out
I have a small visitor counter, without cookies, that sends two separate signals:
- pageview, from a tiny inline script, the moment the HTML arrives.
- ready, from React, once the page really works.
A visit with the first signal but not the second is someone who left before the page worked. If your analytics code is part of your main bundle, you never see these people. They leave before the code that would have counted them arrives.
If you take one thing from this post, take that second signal. It's a few lines:
useEffect(() => {
send({ type: 'ready', ms: Math.round(performance.now()) })
}, [])
What was so big
The page's own code was bundled together with everything that makes, reads and draws PDFs: pdf-lib, pdf.js and its worker, and a font library. That came to 1.3 MB compressed, and all of it had to arrive before the button could open a file picker.
But nobody needs a PDF engine to choose a photo. They need it a few seconds later, when they press Convert.
Fix 1: the page first, the engines after
Each engine is now loaded with a dynamic import(), through one small helper:
const pause = ms => new Promise(r => setTimeout(r, ms))
function engine(load) {
let got = null
const tries = async () => {
for (const wait of [700, 2500]) {
try { return await load() } catch { await pause(wait) }
}
return load()
}
return () => {
if (!got) {
got = tries()
got.catch(() => { got = null })
}
return got
}
}
const pdfLib = engine(() => import('pdf-lib'))
const pdfjs = engine(() => import('pdfjs-dist'))
However many parts of the page ask for an engine, it's downloaded once. If the connection drops, it's tried three times. If all three fail, the next press of the button starts again instead of getting the old error back.
As soon as the page works, it fetches the engines in the background, starting with the one this tool needs:
async function warm(drawsPages) {
const order = drawsPages ? [pdfjs, pdfLib] : [pdfLib, pdfjs]
for (const get of order) await get().catch(() => {})
}
And each job simply waits for what it uses:
async function convert(photos) {
const { PDFDocument } = await pdfLib() // usually here already
// ...
}
On a normal connection the engine has arrived before anyone has finished choosing photos. On a very slow one, the job waits for it and then carries on by itself.
One thing to check: some browsers used to remember a failed import(), so trying the same address again failed immediately. I tested this with a server that answers 503 once or twice and then works. Chrome 158 and Firefox 160 both recovered. I haven't tested Safari.
Fix 2: say so
Even the small version takes a few seconds on a bad connection, so the page now admits it. This goes inline in the HTML, so it works before any other file has arrived:
<script>
(function () {
var bar
function say(text) {
if (!bar) {
bar = document.createElement('div')
bar.className = 'wait-bar'
bar.setAttribute('role', 'status')
document.body.appendChild(bar)
}
bar.textContent = text
}
function tap(e) {
if (!e.target.closest('a[href]')) say('Still loading. One momentβ¦')
}
var timer = setTimeout(function () {
document.querySelectorAll('[data-choose]').forEach(function (b) {
b.textContent = 'Loadingβ¦'
})
}, 900)
document.addEventListener('click', tap, true)
window.pageIsUp = function () {
clearTimeout(timer)
document.removeEventListener('click', tap, true)
if (bar) bar.remove()
}
})()
</script>
Call pageIsUp() right after React renders. On a fast connection that happens before the 900 ms are over, so nobody sees "Loadingβ¦" flash by.
I use createRoot().render(), which draws the real page over the prerendered one, so the button gets its own words back. If you use hydrateRoot(), show and hide a separate element instead. Changing the button's text before React arrives would be a hydration mismatch.
My real version also shows "Try again" when the code can't be fetched at all.
Before and after
Headless Chrome, phone-sized screen, first visit, median of 3 runs. I didn't slow the processor down, so a cheap phone will be slower than this.
| Connection | Page visible | Usable before | Usable after |
|---|---|---|---|
| 400 kbit/s | 2 s | 27.1 s | 4.4 s |
| 1 Mbit/s | 1 s | 11.3 s | 2.0 s |
| 8 Mbit/s | 0.4 s | 1.7 s | 0.4 s |
Before the page works, it now downloads 173 KB instead of 1,303 KB. The engines are as big as they were, but nobody waits for them to get a working button.
What about real visitors? Before the change, 6 of 58 page loads on phones never got as far as "ready". Five of them were the three people above, reloads included. The sixth lasted under two seconds, so I can't blame the loading for that one. In the first 17 hours after the change it was 0 of 16. That's far too few to prove anything, but it's the right direction.
What I'd do differently
I had tested on a slow connection before, but I was measuring when the page appeared. Now there's a test that opens the page on a horrible connection and taps the main button while it's still loading, like a person who wants their PDF. It would have shown me all of this on day one.
The part I keep coming back to is the person who tapped 18 times. Someone who tries that hard really wanted the page to work, and it never told them to wait.
How do you measure the gap between "looks ready" and "is ready" on your own sites?
I build this site with an AI coding assistant (Claude), and it wrote this post from my site's own visitor data and tests. The three visits, the code and the measurements are real.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.