๐ I Served My React SPA from Android Assets Like a Professional Web Server โ Here's What Happened
First load: 77ms. Reload: 2ms. 38x faster with LRU cache. No server, no permissions, no dependencies.
๐ค The Problem Every React Dev Faces
You've got your SPA running perfectly on localhost:5173. React, TypeScript, TailwindCSS, React Router, lazy loading... everything works beautifully.
Now you need to take it to Android.
Your traditional options:
// Option 1: Capacitor โ 30MB runtime, complex config
// Option 2: Cordova โ 15MB, outdated plugins
// Option 3: file:// protocol โ broken CORS, SPA routes don't work
// Option 4: 50-line homemade script โ fragile, no cache, no security
None of them feel right. You want something lightweight, fast, secure, and respectful of your architecture.
โจ The Solution: WebVirt Engine
An Android library of ~600 lines that simulates a virtual web server inside the WebView. Your SPA thinks it's at https://app.local, but everything comes from assets/.
// 5 lines. That's it.
WebVirt.with(this)
.host("app.local")
.bind(webView);
webView.loadUrl("https://app.local/");
That's all. Your React app running. SPA routes intact. No weird configuration.
๐ฌ But Don't Take My Word for It. Look at the Real Data.
To validate that WebVirt Engine was as fast as promised, I needed real metrics. Not synthetic benchmarks. Not "it feels fast." Cold, hard data.
The Secret Weapon: WebVirtMetrics
WebVirt Engine includes an optional metrics module that captures every asset load in real time:
// Enable only in debug. Zero overhead in production.
WebVirtMetrics.ENABLED = BuildConfig.DEBUG;
WebVirtMetrics.startSession();
// Every asset WebVirt loads gets recorded:
// - File path
// - Load time in milliseconds
// - Whether it came from cache or disk
// - Size in bytes
// - MIME type
Metrics are automatically persisted using LoggingUtil, which writes a log file to the device storage without requiring any permissions.
๐ The Results (Real Financial App)
Stack: React 18 + TypeScript + TailwindCSS + Vite + React Router
Assets: 1.4MB (3 main files + 13 lazy chunks)
Device: Physical Android, mid-range
First Load (Assets from Disk)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ WEBVIRT ENGINE - PERFORMANCE REPORT โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ Session duration: 4214 ms โ
โ Total assets loaded: 3 โ
โ Total load time: 77 ms โ
โ Avg load time: 25 ms โ
โ Min load time: 10 ms โ
โ Max load time: 49 ms โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ Cache hits: 0 โ
โ Cache misses: 3 โ
โ Cache hit rate: 0.0% โ
โ Bytes from cache: 0 bytes โ
โ Total bytes loaded: 1426251 bytes โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ HTTP errors: 0 โ
โ SPA fallbacks: 1 โ
โ Range requests: 0 โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ BY MIME TYPE: โ
โ HTML x1 avg 10ms โ
โ CSS x1 avg 18ms โ
โ JavaScript x1 avg 49ms โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ RECENT LOADS (last 5): โ
โ ๐ /index.html 10msโ
โ ๐ /assets/index-DGe01YXs.css 18msโ
โ ๐ /assets/index-B3g6t1vt.js 49msโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
3 assets. 77ms total. Zero errors.
The 4214ms "session" includes: app startup, welcome animation, and the user tapping the "Start" button. WebVirt only took 77ms.
Second Load (LRU Cache in RAM)
By long-pressing the WebView (a hidden debug gesture), I forced a reload to measure cache performance:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ WEBVIRT ENGINE - PERFORMANCE REPORT โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ Session duration: 513 ms โ
โ Total assets loaded: 3 โ
โ Total load time: 2 ms โ
โ Avg load time: 0 ms โ
โ Min load time: 0 ms โ
โ Max load time: 1 ms โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ Cache hits: 3 โ
โ Cache misses: 0 โ
โ Cache hit rate: 100.0% โ
โ Bytes from cache: 1426251 bytes โ
โ Total bytes loaded: 1426251 bytes โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฃ
โ RECENT LOADS (last 5): โ
โ ๐พ /index.html 1msโ
โ ๐พ /assets/index-B3g6t1vt.js 0msโ
โ ๐พ /assets/index-DGe01YXs.css 1msโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
3 assets. 2ms total. 100% cache hit rate.
Notice the emoji: ๐พ = served from cache. The JS bundle took 0ms (less than 1ms, rounded down). HTML took 1ms. CSS took 1ms.
๐ The Side-by-Side Comparison
Metric First Load Reload (Cache) Improvement
Total load time 77ms 2ms 38.5x faster
Average time 25ms 0ms Instant
Slowest asset 49ms (JS) 1ms (CSS) 49x faster
Cache hit rate 0% 100% Perfect
Bytes transferred 1.4MB 0 All from RAM
HTTP errors 0 0 Perfect
๐ง Why Is It So Fast?
WebVirt Engine uses an in-memory LRU cache with SHA-1 ETags:
First load:
assets/index-B3g6t1vt.js โ read from APK โ cached in RAM โ ETag generated
Second load:
assets/index-B3g6t1vt.js โ ETag match? โ Yes โ 304 Not Modified โ 0ms
ยท No asset decoding (Android stores them compressed in the APK)
ยท No disk I/O on reloads (everything in RAM)
ยท No real HTTP header parsing (everything is local)
ยท LruCache with memory awareness that cleans up on onTrimMemory()
๐ Security That Doesn't Sacrifice Speed
Every response includes automatic security headers:
Content-Security-Policy: default-src 'self'; script-src 'self'...
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Access-Control-Allow-Origin: *
And CSP is fully configurable:
WebVirt.with(this)
.host("app.local")
.cspPolicy("default-src 'self'; script-src 'self' https://api.external.com")
.bind(webView);
๐ค Plays Beautifully with Nexus
Need native APIs? Nexus is a JavaScript โ Android bridge that doesn't interfere with WebVirt:
// WebVirt: serves the SPA
WebVirt.with(this).host("app.local").bind(webView);
// Nexus: export, import, PDF, camera, whatever you need
Nexus.installOn(webView)
.registerHandler("export", new ExportAdapter())
.registerHandler("import", new ImportAdapter())
.registerHandler("pdf", new PdfAdapter())
.initialize()
.withFilePicker(this);
nexus.attachToWebViewLifecycle(); // Doesn't break WebVirt
webView.loadUrl("https://app.local/");
WebVirt doesn't know Nexus exists. Nexus doesn't know WebVirt exists. They collaborate without coupling. This is real architecture.
๐๏ธ The Architecture That Makes This Possible
WebView
โโโ WebViewClient โ WebVirt (owner)
โ โโโ shouldInterceptRequest() โ assets/
โ
โโโ WebViewLifecycleObserver โ Nexus (decorator)
โ โโโ Wraps WebVirt's client without breaking it
โ
โโโ JavascriptInterface โ Nexus (parallel channel)
โโโ window.__nexus.call("export", data)
Three layers that don't compete. Decorator Pattern for lifecycle. Builder Pattern for fluent configuration. Strategy Pattern for PathHandlers.
๐ฆ Production Proven
This isn't a "hello world" library. It's running in production in a real financial app with:
ยท โ๏ธ React 18 + TypeScript + TailwindCSS
ยท ๐ฆ 5MB of assets (1.4MB main bundle)
ยท ๐ React Router with lazy loading
ยท ๐ค Native JSON export
ยท ๐ฅ Native JSON import with FilePicker (no permissions required)
ยท ๐ Native PDF export
ยท ๐ Restrictive CSP
ยท โก 77ms first load, 2ms reloads
๐ Coming Soon to GitHub & JitPack
WebVirt Engine v3.1.1
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.fouzstack:webvirt-engine:3.1.1'
}
Nexus v2.0.0
implementation 'com.github.fouzstack:nexus:2.0.0'
๐ฏ Is This for You?
โ Use WebVirt Engine if you:
ยท Have an SPA in React/Vue/Svelte
ยท Want full control without heavy dependencies
ยท Need maximum offline performance
ยท Value clean architecture and real decoupling
โ Not for you if you:
ยท Need hot reload during development (for now)
ยท Your company is already committed to Capacitor/Cordova
ยท Your app is purely native with no web content
๐ Acknowledgments
To Fouzstack for creating and maintaining both WebVirt and Nexus.
To the GoF design patterns that still hold up 30 years later.
To WebVirtMetrics and LoggingUtil for making it possible to collect this data without extra permissions.
And to you, for reading this far.
Questions? Ideas? Want to contribute? The repos will be open for issues and PRs as soon as they go live.
Drop a comment: Which metric surprised you most? The 77ms first load or the 2ms cached reload?
Top comments (0)