Skip to content

Commit c06ae9c

Browse files
authored
Add Prebid.js NPM integration with per-module JS builds (#242)
1 parent d118aa9 commit c06ae9c

37 files changed

Lines changed: 5455 additions & 1253 deletions

SEQUENCE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ sequenceDiagram
129129

130130
## Notes
131131
- TSJS
132-
- Served first-party at `/static/tsjs-core.min.js` (and `/static/tsjs-ext.min.js` if prebid auto-config is enabled).
132+
- Served first-party at `/static/tsjs=tsjs-unified.min.js?v=<hash>`. The server dynamically concatenates core + enabled integration modules based on config.
133133
- Discovers ad units and renders placeholders; either uses slot-level HTML (`/first-party/ad`) or JSON auction (`/auction`).
134134
- Publisher HTML Rewriting
135135
- Injects TSJS loader and rewrites absolute URLs from origin domain to first-party domain during streaming.

crates/common/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ Helpers:
3838
- `rewrite_srcset(settings, srcset) -> String` — proxy absolute candidates; preserve descriptors (`1x`, `1.5x`, `100w`)
3939
- `split_srcset_candidates(srcset) -> Vec<&str>` — robust splitting for commas with/without spaces; avoids splitting the first `data:` mediatype comma
4040

41-
Static bundles (served by publisher module):
41+
JS bundles (served by publisher module):
4242

43-
- Unified dynamic endpoint: `/static/tsjs=<filename>`
44-
- `tsjs-core(.min).js` — core library
45-
- `tsjs-ext(.min).js` — Prebid.js shim/extension
46-
- `tsjs-creative(.min).js` — creative click‑guard injected into proxied creatives
43+
- Dynamic endpoint: `/static/tsjs=tsjs-unified.min.js?v=<hash>`
44+
- At build time, each integration is compiled as a separate IIFE (`tsjs-core.js`, `tsjs-prebid.js`, `tsjs-creative.js`, etc.)
45+
- At runtime, the server concatenates `tsjs-core.js` + enabled integration modules based on `IntegrationRegistry` config
46+
- The URL filename is fixed for backward compatibility; the `?v=` hash changes when modules change
4747

4848
Behavior is covered by an extensive test suite in `crates/common/src/creative.rs`.
4949

crates/common/src/creative.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub fn rewrite_creative_html(settings: &Settings, markup: &str) -> String {
322322
let injected = injected_ts_creative.clone();
323323
move |el| {
324324
if !injected.get() {
325-
let script_tag = tsjs::unified_script_tag();
325+
let script_tag = tsjs::tsjs_script_tag_all();
326326
el.prepend(&script_tag, ContentType::Html);
327327
injected.set(true);
328328
}

crates/common/src/html_processor.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@ pub fn create_html_processor(config: HtmlProcessorConfig) -> impl StreamProcesso
203203
origin_host: &patterns.origin_host,
204204
document_state: &document_state,
205205
};
206-
// First inject the unified TSJS bundle (defines tsjs.setConfig, etc.)
207-
snippet.push_str(&tsjs::unified_script_tag());
208-
// Then add any integration-specific head inserts (e.g., mode config)
209-
// These run after the bundle so tsjs API is available
206+
// First inject integration-specific config (e.g., window.__tsjs_prebid)
207+
// so it's available when the bundle's auto-init code reads it.
210208
for insert in integrations.head_inserts(&ctx) {
211209
snippet.push_str(&insert);
212210
}
211+
// Then inject the TSJS bundle — its top-level init code can now
212+
// read the config that was set by the inline scripts above.
213+
let module_ids = integrations.js_module_ids();
214+
snippet.push_str(&tsjs::tsjs_script_tag(&module_ids));
213215
el.prepend(&snippet, ContentType::Html);
214216
injected_tsjs.set(true);
215217
}
@@ -607,12 +609,12 @@ mod tests {
607609
.expect("should keep existing head content");
608610

609611
assert!(
610-
tsjs_index < head_index,
611-
"should inject head snippet after tsjs tag"
612+
head_index < tsjs_index,
613+
"should inject config before tsjs bundle so auto-init can read it"
612614
);
613615
assert!(
614-
head_index < title_index,
615-
"should prepend head snippet before existing head content"
616+
tsjs_index < title_index,
617+
"should prepend all injected content before existing head content"
616618
);
617619
}
618620

0 commit comments

Comments
 (0)