Skip to main content

Elements

Worker

The Service Worker element for the Land editor. Implements multi-tier asset caching (network-first for app shell, cache-first for static assets) and a two-phase CSS import interception protocol that lets the VS Code workbench use standard import syntax inside the Tauri WebView.

PUBLISHED ELEMENTS Source/Content/doc/worker.md UNAVAILABLE UNAVAILABLE 14

Worker: Service Worker

Worker is the service worker for Land. We implement asset caching and offline support, a dynamic CSS loading strategy for the workbench bundle, and fetch interception that converts JS imports of CSS files into native <link> tag loading. Sky registers Worker at application startup, and we operate purely at the fetch layer with zero runtime dependencies.

Overview 📋

Worker is a standalone service worker script with no runtime dependencies.

  • We provide caching for Land’s UI assets.
  • We implement a custom CSS import interceptor.
  • We are registered by Sky at application startup.
AttributeValue
LanguageTypeScript (compiled via ESBuild)
RuntimeServiceWorker API (browser built-in)
DependenciesNone (zero runtime deps)
Consumed bySky (registered at startup)

Architecture 🏗️

Module Map 🗺️

PathPurpose
Source/Worker/Worker.tsMain service worker script
Source/Worker/CSS/Dynamic CSS loading module
Source/Worker/Policy.tsCache policy definitions
Source/Worker/Register.tsRegistration and update management
Source/Telemetry/Bridge.tsService worker telemetry bridge

Caching Strategy 💾

We implement a multi-tier caching strategy for different resource types.

Cache Categories

Cache NameStrategyResourcesDuration
CACHE_CORENetwork-firstNavigation requests (HTML pages, app shell)Session
CACHE_ASSETCache-firstStatic assets (JS, CSS, fonts, images)7 days
CACHE_EXTCache-firstExtension assets24 hours
(none)Network-onlyExternal resources (CDN, API calls)N/A

Network-First Strategy (CACHE_CORE)

flowchart TD
    A["Request navigation (HTML)"] --> B["1. Try network fetch"]
    B -->|"Network succeeds"| C["Return response to page<br/>Cache response in CACHE_CORE"]
    B -->|"Network fails"| D["Check CACHE_CORE for cached response<br/>Return cached response if available<br/>Return fallback page if not cached"]

Cache-First Strategy (CACHE_ASSET)

flowchart TD
    A["Request static asset (JS, CSS)"] --> B["1. Check CACHE_ASSET for cached response"]
    B -->|"Cache hit"| C["Return cached response<br/>Background-fetch update (stale-while-revalidate)<br/>Update cache if newer version available"]
    B -->|"Cache miss"| D["Fetch from network<br/>Cache response in CACHE_ASSET<br/>Return response"]

Dynamic CSS Loading 🎨

We implement a unique CSS loading strategy: JavaScript modules that import 'styles.css' are intercepted and served a JS proxy that injects a <link> element.

CSS Import Interception

JavaScript module imports CSS
  import './styles.css';     // At module scope
    |
    v
Worker intercepts fetch for styles.css
    |
    +---> Detects CSS import (MIME type or path pattern)
    |
    +---> Returns generated JS module:
    |
    |   // Generated by Worker:
    |   const link = document.createElement('link');
    |   link.rel = 'stylesheet';
    |   link.href = './styles.css?Skip=Intercept';
    |   document.head.appendChild(link);
    |   export default link;
    |
    v
Page receives JS module, not CSS
    |
    v
CSS loads via native browser <link> element

Two-Phase Loading

To avoid infinite interception we use a ?Skip=Intercept query parameter:

Phase 1: JS module import('styles.css')
    -> Worker intercepts
    -> Returns JS proxy module
    -> JS proxy creates <link href="styles.css?Skip=Intercept">

Phase 2: Browser loads styles.css?Skip=Intercept
    -> Worker checks for Skip=Intercept parameter
    -> Passes through to network (no interception)
    -> Returns actual CSS content
    -> <link> element loads CSS natively

Benefits

AspectWithout WorkerWith Worker
CSS loadingImported as JS string in bundleNative browser <link> loading
CSS cascadeBundle-dependent orderingProper cascade via DOM order
Source mapsLost during JS bundlingPreserved via native loading
Hot reloadRequires full rebuildWorks with CSS-only refresh

Service Worker Lifecycle 🔄

stateDiagram-v2
    [*] --> Installing: Sky registers Worker
    Installing --> Waiting: install event caches CACHE_CORE, pre-caches workbench bundles, installs CSS interceptor
    Waiting --> Activating: Sky sends postMessage ACTIVATE
    Activating --> Active: claims uncontrolled clients, clears old caches, intercepts fetch
    Active --> Installing: Sky re-registers, byte-different script detected

Installation

1. Sky registers Worker:
    navigator.serviceWorker.register('/worker.js', { scope: '/' })
    |
    v
2. Worker install event fires
    - Caches critical assets in CACHE_CORE
    - Pre-caches known workbench bundles
    - Installs CSS interceptor module
    |
    v
3. Worker enters waiting state (if existing SW active)

Activation

4. Sky sends activation signal via postMessage:
    worker.postMessage({ type: 'ACTIVATE' })
    |
    v
5. Worker activate event fires
    - Claims all uncontrolled clients
    - Clears old caches (version mismatch)
    - Begins intercepting fetch events
    - Sends 'activated' response to Sky

Update Cycle

6. Sky checks for Worker update periodically:
    navigator.serviceWorker.register('/worker.js')
    |
    v
7. New Worker detected (byte-different script)
    - New Worker installs in background
    - New Worker enters waiting state
    |
    v
8. Sky decides when to activate:
    - Immediately (during development)
    - On next navigation (during production)
    - User prompt (configurable)
    |
    v
9. New Worker activates, takes over, fresh caches

Client Scripts 📜

We provide client-side scripts for integration.

Register.ts

Handles service worker registration and update management:

export async function registerWorker(): Promise<ServiceWorkerRegistration> {
	const registration = await navigator.serviceWorker.register("/worker.js", {
		scope: "/",
	});

	// Check for updates on every page load
	registration.addEventListener("updatefound", () => {
		const newWorker = registration.installing;
		if (newWorker) {
			newWorker.addEventListener("statechange", () => {
				if (newWorker.state === "installed") {
					// New worker ready, notify Sky
					dispatchWorkerUpdate();
				}
			});
		}
	});

	return registration;
}

Funding 💎

Project Maintainers: Source Open (Source/[email protected]) | GitHub Repository | Report an Issue