TheOmniKit 作者分享打造 700 多款瀏覽器端工具的工程經驗
What I learned building 700+ browser-only tools (no uploads, ever)
TheOmniKit 作者分享打造 700 多款瀏覽器端工具的工程經驗,檔案處理遵循不上傳原則,並設有一項錯誤回報截圖例外。影片工具改用 WebCodecs、mp4box 和 mp4-muxer,相關程式 gzipped 約 50 KB,相比 ffmpeg.wasm 的 32 MB;短片裁剪可在不到一秒完成且不損失畫質。
I run theomnikit.com, a free site with 700+ small tools: PDF, image, video, developer utilities and calculators. Every file tool runs in the visitor's browser. Nothing is uploaded, and that one rule decided most of the engineering.
This is what I learned getting there, including the parts that broke. I wrote this with AI help and checked every claim against the code and the project's decision log.
The rule, and how it is enforced
The first architecture decision was "browser-only; any server-side processing needs a new decision". A rule in a doc is easy to break quietly, so it is enforced by a test:
- no API route accepts a file (no multipart parser, no upload interceptor);
- nginx refuses any request body over 1 MB;
- the API's JSON parser stops at 1 MB.
There is exactly one exception, and it has its own written decision: a screenshot attached to a bug report, redrawn on a canvas into a JPEG of at most 600 KB, so camera and location metadata never leave the device.
One thing I got wrong early: I listed OCR as "out of scope" because I assumed it needed a server. It was the upload I had ruled out, not the reading of text. Image to Text now runs tesseract.js as WebAssembly. Because the CSP is connect-src 'self' and every Tesseract default points at a CDN, the worker, cores and language files are served from our own domain.
How a file tool actually works
The flow is the same everywhere: the File API hands over the bytes, the format is detected from the bytes (not the extension), the heavy library is fetched as soon as the file arrives, and the job runs when the person taps one button that names the action.
Long jobs go to a Web Worker. The PDF tools, such as Grayscale PDF, share a small runner: one worker per tool, created on first use, the ArrayBuffer transferred rather than copied, progress posted back, and a fallback to the main thread when a worker cannot start.
Workers are also a safety valve. The Regex Tester builds its worker from the same function the unit tests cover, and terminates it after 2 seconds, because a pattern that needs longer is almost always backtracking catastrophically.
But a worker is not a fix for a bad algorithm. The diff checker froze on large inputs; moving it to a worker would have stopped the freeze and still failed the same comparisons. It got a better alignment algorithm with a time budget instead.
Video without ffmpeg.wasm
Video was where browser-only nearly broke. Single-threaded ffmpeg.wasm re-encoded a 5-minute 1080p video in 35 to 90 minutes. The multithreaded build needs SharedArrayBuffer, which means COOP/COEP headers, which blank out third-party ads, so that was not an option for a free site.
The video tools, like Trim Video, use WebCodecs with mp4box and mp4-muxer instead: about 50 KB gzipped against ffmpeg's 32 MB. Trimming copies the original frames when the shape stays the same, so a short clip finishes in under a second with no quality loss.
The pitfalls nobody warns you about
iOS Safari's canvas limit. Safari refuses a canvas over about 16.7 megapixels, and recent iPhones shoot 24 MP by default. The failure is not even consistent: sometimes no context, sometimes an InvalidStateError on encode. Image to PDF now caps every encode at 16.7 MP, retries at half the pixels down to 2 MP, and tries a DOM canvas when OffscreenCanvas refuses.
Thumbnails are full photos. Forty 12 MP photos in a preview strip is about 2 GB of decoded pixels on a phone. Thumbnails are now 480 px JPEGs made when the file is added.
HEIC. One image tool accepted HEIC and handed it straight to createImageBitmap, so every HEIC failed in Chrome and Firefox, which cannot decode it. The image tools now share one loader that handles HEIC, and HEIC to JPG decodes with a dedicated library in the browser.
Memory on big files. Before decoding an image, a pixel guard reads width and height from the first 256 KB and refuses an oversized image with a clear message. Running out of memory is reported as "too large for this browser", never as "damaged file", because it says nothing about the file. PDF limits, such as on Compress PDF, were deliberately not raised until browser memory is actually measured.
Feature probes that lie. Every "to WebP" page once told visitors their browser could not write WebP, in a browser that could. The probe encoded a 1x1 OffscreenCanvas that had never had a rendering context, and that throws for every format. Test the probe, not just the feature.
Why every tool is its own page
Each tool has its own URL, title, short answer at the top and structured data, because the site has no marketing budget: people arrive from search engines and AI assistants asking one specific question, and the page has to be the answer.
That also has a limit. Of 151 landing variants Google left "discovered, not indexed", 108 were folded into their parent with a 301. One page per real search intent, not one per keyword.
State lives in the URL too. When one tool suggests the next step (say, compress the PDF you just made), the plan travels as link parameters, and the file is handed over in memory. No account, no server-side history.
The standard that ties it together
The bar for a file tool is two taps on a phone: choose the file, confirm. Zero scrolls. The site never runs a job on a dropped file by itself; that one confirmation is what makes "your files never leave your device" believable.
If you are building something similar: write the no-upload rule down, enforce it with a test, put long jobs in workers, and test on a real iPhone early. Most of my bugs were in that last one.
來源:DEV Community · dev.to