π 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)