Skip to main content

SHEET 06 OF 41

/DOC/:SLUG

PART:
03 DOCUMENTATION
CIRCULATION:
PUBLIC
SOURCE:
pages/Doc/[...Slug].astro
AS OF:
2026-12-02

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.

/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 ARCHITECTURE 14

STATUS UNAVAILABLE NEXT OPEN THE ELEMENT'S MANUAL - IT DESCRIBES THE THING THE CAPTURE WOULD HAVE SHOWN.

# PLATE_01 / WORKER

FIGURE: The capture this record's evidence would show: threads on a loom in macro.

This element has no evidence manifest in the build, so there is no capture to describe. The record above still prints its working - form, boundary, dependencies, manual and trace - and none of those fields is a picture.

SUBJECT:
THREADS ON A LOOM IN MACRO
MANIFEST:
UNAVAILABLE
STATE:
UNAVAILABLE
PLATE:
UNAVAILABLE

SOURCE: SOURCE/CONTENT/ELEMENT/WORKING.TS

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.
Attribute Value
Language TypeScript (compiled via ESBuild)
Runtime ServiceWorker API (browser built-in)
Dependencies None (zero runtime deps)
Consumed by Sky (registered at startup)

Architecture 🏗️

Module Map 🗺️

Path Purpose
Source/Worker/Worker.ts Main service worker script
Source/Worker/CSS/ Dynamic CSS loading module
Source/Worker/Policy.ts Cache policy definitions
Source/Worker/Register.ts Registration and update management
Source/Telemetry/Bridge.ts Service worker telemetry bridge

Caching Strategy 💾

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

Cache Categories

Cache Name Strategy Resources Duration
CACHE_CORE Network-first Navigation requests (HTML pages, app shell) Session
CACHE_ASSET Cache-first Static assets (JS, CSS, fonts, images) 7 days
CACHE_EXT Cache-first Extension assets 24 hours
(none) Network-only External 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

Aspect Without Worker With Worker
CSS loading Imported as JS string in bundle Native browser <link> loading
CSS cascade Bundle-dependent ordering Proper cascade via DOM order
Source maps Lost during JS bundling Preserved via native loading
Hot reload Requires full rebuild Works 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;
}

Credits 💎

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

The record this page documents, as the site files it: the six facts the claim is checked against, and the three files the manual names to check it.

WORKING, 06

FORM:
SERVICE WORKER
WRITTEN IN:
TYPESCRIPT
BOUNDARY:
HTTP CACHE
DEPENDS ON:
SKY
EVIDENCE:
UNAVAILABLE NO MANIFEST IN THE BUILD

# TRACE / WORKER 03 STEPS

  1. 01 Source/Worker/Register.ts REGISTRATION
  2. 02 Source/Worker/Policy.ts THE CACHE POLICY
  3. 03 Source/Worker/CSS/Load.ts CSS LOADING

SOURCE: /DOC/DEEP-DIVE-WORKER

Elements, 17 PAGES, page 16 of 17

THE LAYER'S INDEX

NEXT: /BLOG 04 BLOG

NOTES FROM THE BUILD, NEWEST FIRST

〈06〉 /DOC/:SLUG 41 SHEETS AS OF: 2026-12-02