digital-identity-platform-docs
digital-identity-platform-docs / workers/sst-loader/worker
workers/sst-loader/worker
Cloudflare Worker responsible for loader delivery, session bootstrapping, and GTM container script proxying for the platform.
Role in the platform
- Serves the first-party loader script (
/l/:loaderToken.js) that initializes client-side logic. - Handles loader bootstrap (
/boot) to resolve site, container, profile, and session state via KV. - Proxies GTM container scripts (
/c/:containerToken.js) while preserving preview/debug behavior.
Request flow
- Runtime Config: Fetched/cached from KV
EDGE_CONFIGfor every request (fail-open). GET /l/:loaderToken.js- Returns a dynamically rendered loader JS template with environment-specific URLs injected.
POST /boot- Validates loader + site configuration from KV.
- Resolves container, site, profile, and session.
- Persists loader session and cookies (Client-side logic).
- Returns container metadata and cookie restoration instructions (gated by
cookie_restorationflag).
GET /c/:containerToken.js- Resolves Web GTM ID from KV.
- Proxies
gtm.jsfrom Google with cache headers.
POST /mark-session-detection- Forwards session detection signals (or skips if
adblock_itp_detectionis disabled).
- Forwards session detection signals (or skips if
Security & trust boundaries
- KV Storage: Acts as the single source of truth for configuration:
EDGE_CONFIG: Runtime features and module gating.SITE_REGISTRY: Site-specific settings (GTM ID, domains).LOADER_TOKEN_MAP: Maps public tokens to internal Container IDs.
- Observability: Direct writes to Cloudflare Analytics Engine (AE) and R2.
- No external database dependencies.
CORS & browser behavior
- Explicit CORS handling for
/bootand/mark-session-detection. - Allows credentials and custom headers used by the loader.
Error handling
- Returns
400for malformed client payloads. - Returns
404for unknown loaders, sites, or containers. - Internal errors are surfaced as generic 500 responses (fail-safe).
Observability
- Uses
writeLogToAEfor structured event logging to Analytics Engine. - Uses
pickR2Bucketfor raw log archival to R2 (partitioned by day). - Session identifiers are generated server-side and returned to the client.
Notes
- This Worker intentionally embeds a large JS template string; it is not executed server-side.
- No request routing occurs beyond explicit path matching.
- Designed for Cloudflare Workers runtime semantics.
Runtime Configuration & Module Gating Architecture
This worker participates in the v2 runtime configuration system:
- Source of Truth: Fetches config from KV (
EDGE_CONFIG) keyed by container ID. - Caching: Config is cached in
caches.defaultwith a generic TTL (default 60s). - Fail-Open: If config is unavailable, the worker proceeds without breaking traffic.
Boot Endpoint Behavior (/boot)
- Cookie Restoration: Controlled by
cookie_restorationfeature flag.true: Cookies retrieved from KV storage (keyed by_fpid) and returned inrestoreCookies.false:restoreCookiesis empty.
- Note: Client snippet uses
_fpid(writable) to ensure persistence across browser restarts.
Feature Model
- Mandatory Modules:
server_proxy,custom_loader,adblock_itp_detection(always on). - Optional Modules:
cookie_restoration,multi_domain.