Trailblazer Server-Driven UI (SDUI) Protocol
Overview
The Trailblazer SDUI Protocol enables fully server-controlled user interfaces in mobile/web apps. The server defines UI structure, data, and behavior via lightweight payloads, while the client handles rendering, diffing, and interaction forwarding. Benefits:
Rapid iteration : Update UI without app store releases.
Personalization : Tailor UIs per user/device.
Consistency : Enforce design across platforms.
Transport : WebSockets (preferred for real-time) or HTTP/2 long-polling. All payloads are JSON over TLS 1.3+.
Core Flow :
Client requests screen: GET /sdui/{screenId}?userId=...&device=....
Server responds with initial Template + Data .
Client renders via native/web components.
Server pushes Diffs for updates.
Client sends Events (e.g., taps) to server.
1. Templates
Templates are declarative JSON trees defining UI hierarchy. Immutable structure; data binds dynamically.
Schema (JSON Schema v2020-12)
json Copy code
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": {"type": "string"}, // Unique stable ID (e.g., "screen:home")
"type": {"enum": ["screen", "container", "row", "column", "stack", "list", "text", "image", "button", "input", "modal"]}, // Whitelisted components
"key": {"type": "string"}, // Stable key for diffing/reconciliation (required for lists)
"props": {
"type": "object",
"properties": {
"style": {"type": "object"}, // {bgColor: "#fff", padding: 16}
"data": {"oneOf": [{"type": "string"}, {"type": "number"}, {"type": "boolean"}, {"type": "array"}]}, // Bindable data
"action": {"type": "string"}, // e.g., "navigate:/profile" or "custom:like"
"placeholder": {"type": "string"},
"src": {"type": "string", "format": "uri"} // Images/videos only
}
},
"children": {"type": "array", "items": {"$ref": "#"}}
},
"required": ["type", "key"]
}
Example Template
json Copy code
{
"id": "home-screen",
"type": "screen",
"props": {"style": {"bgColor": "#f0f0f0"}},
"children": [
{
"key": "header",
"type": "row",
"props": {"style": {"justify": "space-between"}},
"children": [
{"key": "title", "type": "text", "props": {"data": "{{user.name}}", "style": {"fontSize": 24}}},
{"key": "avatar", "type": "image", "props": {"src": "{{user.avatarUrl}}"}}
]
},
{
"key": "feed",
"type": "list",
"props": {"data": "{{feedItems}}"}, // Binds to data array
"children": [ // Template for each list item
{"type": "container", "children": [{"type": "text", "props": {"data": "{{item.title}}"}}]}
]
}
]
}
Data Binding : {{path.to.data}} syntax. Server provides separate Data Payload (JSON object).
Components : Client maps to natives (e.g., text → UILabel/TextView). Extend via client config (whitelisted).
Initial Payload : {template: Template, data: DataObject, version: 1}.
2. Diffing
Efficient tree diffing minimizes bandwidth. Uses stable keys (like React keys) for reconciliation. Server computes diffs; client applies atomically.
Diff Operations (JSON Patch-inspired, tree-aware)
Diff payload: {version: 2, operations: [Op[]], dataDelta: {partialData}}.
Op types:
Type Payload Example Effect replace{"path": "/children/0/props/data", "value": "New Title"}Update leaf prop/data. add{"path": "/children/1", "value": {fullNode}}Insert subtree. remove{"path": "/children/2"}Delete subtree. move{"path": "/children/0", "from": "/children/2"}Reorder (list-friendly). listDiff{"path": "/feed", "items": [{"op": "add", "index": 0, "item": {...}}]}Optimized list patches (add/remove/reorder).
Diffing Algorithm (Server-Side)
Assign/compare stable keys recursively.
Tree Diff (inspired by heuristic diff):
Lists: Longest increasing subsequence (LIS) for moves.
Nodes: Structural similarity (type + key match → update props/data).
Data Delta : Only changed bindings (e.g., {feedItems: [newItems]}).
Threshold: If >50% changed, send full template+data.
Client Reconciliation
Virtual tree mirror of rendered UI.
Apply ops in order (depth-first).
Animate transitions (e.g., fade-ins).
Idempotent: version check; ignore old diffs.
Example Diff :
json Copy code
{
"version": 2,
"operations": [
{"op": "replace", "path": "/children/0/props/data", "value": "Welcome, Alice!"},
{"op": "listDiff", "path": "/feed", "items": [{"op": "add", "index": 0, "key": "newpost1", "value": {...}}]}
],
"dataDelta": {"feedItems": [{"id": "newpost1", "title": "Hot take"}]}
}
3. Safety
Prioritizes client protection from malicious servers (even compromised).
Validation (Client-Enforced)
Schema Validation : Strict JSON Schema on all payloads (libs: ajv for web, swift-json-schema for iOS).
Component Whitelist : Hardcoded allowed types; reject others.
Prop Sanitization :
Prop Rules styleWhitelist keys/values (e.g., no position: fixed; script: url()). Parse CSS-in-JSON safely. src/actionValidate URIs (no javascript:, data: schemes). dataEscape HTML/JS in text; size limits (e.g., <1MB).
CSP-like : Web clients enforce sandbox iframe if experimental components.
Security Features
Signing : Server signs payloads (signature: ed25519(payload + secret)). Client verifies.
Actions : Client proxies events to server (no local JS exec).
Event: {type: "tap", targetKey: "likeBtn", payload: {}}.
Server responds: Diff or redirect.
Rate Limiting : Client throttles diffs (<10/sec), queues events.
Isolation :
Mobile: Render in isolated view controller.
Web: Shadow DOM + strict CSP (script-src 'none').
Fallbacks :
Invalid payload → Revert to last valid state + error screen.
Offline: Cache last N templates/diffs (encrypted).
Privacy : No client telemetry without opt-in; data paths anonymized.
Auditing
Server logs all diffs/events.
Client: Optional debug mode dumps tree states.
Implementation Notes
Client Libs : ~10KB minified JS/Swift/Kotlin. Open-source reference impl at Trailblazer Labs GitHub.
Perf : <16ms render on mid-tier devices; diffs <1KB avg.
Extensibility : Custom components via {type: "custom:mycomp", module: "whitelisted"}.
Testing : Fuzz schema; diff regression suite.
This protocol powers dynamic UIs at scale. Prototype available—let me know if you need code samples!