DEV Community

Giovani Fouz
Giovani Fouz

Posted on

I Served My React SPA from Android Assets Like a Professional Web Server

๐Ÿš€ 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
Enter fullscreen mode Exit fullscreen mode

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/");
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Enter fullscreen mode Exit fullscreen mode

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โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

ยท 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: *
Enter fullscreen mode Exit fullscreen mode

And CSP is fully configurable:

WebVirt.with(this)
    .host("app.local")
    .cspPolicy("default-src 'self'; script-src 'self' https://api.external.com")
    .bind(webView);
Enter fullscreen mode Exit fullscreen mode

๐Ÿค 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/");
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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'
}
Enter fullscreen mode Exit fullscreen mode

Nexus v2.0.0

implementation 'com.github.fouzstack:nexus:2.0.0'
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ 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)