Skip to content

Scripts registry

Admin CRUD for the thin scripts S3 registry and the site setting scripts_split_config (epic AISD-230: schema AISD-231, API AISD-232). This is the editorial / ops surface for script definitions and files. The Cloudflare Worker does not inject these scripts yet — empty data-slot mounts only (see cms-cf-worker docs/script-ad-placeholders.md, portal: Script and ad placeholders).

Source: src/cms_api/admin/routes/scripts.py, src/cms_api/public/scripts_s3.py, hq/sql_models scripts table, hq/infrastructure/s3/cms_scripts/.

Model

Piece Role
DB scripts Thin registry: slug, name, definition, optional site_id, version, s3_key, is_active
S3 cms-scripts-{env}-… JS bytes; s3_key is bucket-relative
EAV scripts_split_config JSON map of definition → percentage (e.g. {"below_title": 20}) on site / global settings

Stage (stg) uses the dev bucket + /dev/cms-scripts/*-keys secrets (same dev/prd prefix pattern as cms-templates).

Suggested key layout (also in hq IaC README):

  • global: global/<slug>/<version>.js
  • site override: sites/<site_acronym>/<slug>/<version>.js
  • per-site s.js: sites/<site_acronym>/s.js/<sha256[:12]>.js -- the legacy settings bundle carries no version string, so its version is content-addressed. Every site's row shares name = "s.js" (the public resolver matches on name); slug is <acronym>-s.js, which is what stays globally unique.

definition is a slot name, and NULL is meaningful

definition names a live worker data-slot mount -- below_title, after_section, custom_scripts_header, custom_scripts_body. A row with definition = NULL is deliberate, not unfinished: it marks a script that is fetched at runtime by another script (prebid.js, which nwp-header.js requests from a hardcoded /wp-content/js/ prefix) or is not JS at all (skeleton.html). Giving those a slot double-loads them. Full rationale and the maintenance script that enforces this: hq docs/cms-script-registry-cleanup.md (AISD-308).

Admin routes

All under /admin/v1, JWT admin gate (see Auth flow).

Method Path Notes
GET /admin/v1/scripts Paginated list (limit, cursor); default sort id asc
POST /admin/v1/scripts Create registry row (slug, name, …) → 201
GET /admin/v1/scripts/{script_id} Fetch one
PATCH /admin/v1/scripts/{script_id} Partial update
DELETE /admin/v1/scripts/{script_id} Soft-delete (is_active=false) → 204
POST /admin/v1/scripts/bulk { "action": "activate"\|"deactivate", "ids": [...] } (max 100)
POST /admin/v1/scripts/{script_id}/file Multipart file (+ optional version); uploads to cms-scripts and stamps s3_key + version

ScriptOut fields: id, slug, name, description, version, s3_key, site_id, definition, is_active, edited_by, creator_type.

Upload example

curl -X POST "http://cms-api-stg.skyneto.com/admin/v1/scripts/${SCRIPT_ID}/file" \
  -H "Authorization: Bearer $CMS_ADMIN_TOKEN" \
  -F "file=@./header.js;type=application/javascript" \
  -F "version=v1"

Public read: by name + domain

GET /public/v2/scripts/{name}?domain=<host> resolves a row for one site -- per-site override first, then the global row of the same name -- and returns either the metadata (default) or the file bytes (raw=1). No admin credentials: the consumers that pick which build to serve (the CMS worker, the trending worker) have none. Full contract in Public v2 API.

Settings: custom_scripts

Which registry scripts the CMS worker injects into which mount, and how each loads (AISD-309). Registered in SETTING_KEY_NAMES, stored as JSON, merged like every other EAV setting (per-site row overrides site_id = -1), and included on the public /settings read -- the worker holds no admin credentials.

{"custom_scripts_body": [{"name": "jquery.js",     "strategy": "on-scroll"},
                         {"name": "s.js",          "strategy": "on-scroll"},
                         {"name": "nwp-header.js", "strategy": "on-scroll"}]}
  • Keys accept only the two mounts the worker renders: custom_scripts_header, custom_scripts_body. The ad slots (below_title, after_section) are filled by the ad stack, not by injected tags, and are rejected with a 422.
  • name is the registry name, resolved per site by GET /public/v2/scripts/{name} (per-site override beats global).
  • strategy is defer / async / on-scroll and is required -- the settings PATCH dumps with exclude_unset=True, which propagates into nested models, so a default would be dropped before storage and the worker would need its own duplicate default.
  • List order is load order and is preserved verbatim. The legacy bundle needs jQuery, then its settings bundle, then itself; a reordering breaks the ad stack silently.

The site_id = -1 default

A site with no override must still get a working list, so the global row is seeded, not assumed:

ENVIRONMENT=stg cms-api/.venv312/bin/python cms-api/scripts/seed_custom_scripts_default.py --dry-run
ENVIRONMENT=stg cms-api/.venv312/bin/python cms-api/scripts/seed_custom_scripts_default.py

Idempotent -- a re-run leaves an operator-tuned value alone (--force overwrites, --yes is required for a real write against prod). Without this row every site reads custom_scripts as null and the worker injects nothing.

Malformed entries are dropped on read

Values written through the admin PATCH are typed, but a seed, a hand-run SQL fix, or a DDL bypasses pydantic. coerce_setting_value therefore sanitizes on read: unknown slots, non-list slot values, and entries missing name or strategy are dropped (declared order preserved), and a blob with nothing renderable reads as absent rather than as an empty object. The worker never has to interpret a half-written entry.

Not to be confused with scripts_split_config below -- that is the A/B weights map. Two different settings.

Settings: scripts_split_config

Writable like other EAV settings (PATCH site or global settings). Included on admin settings reads, on the public /settings merge, and on the public /lists?kind=trending response (the trending worker needs it alongside the site acronym). Shape is a JSON object of string keys → numeric weights/percentages.