5252//! automatically rewrite `DataDome` script URLs in HTML responses:
5353//!
5454//! - `<script src="https://js.datadome.co/tags.js">` becomes
55- //! `<script src="https://publisher.com /integrations/datadome/tags.js">`
55+ //! `<script src="/integrations/datadome/tags.js">`
5656//! - Handles both `src` and `href` attributes (for preload/prefetch links)
5757
5858use std:: sync:: { Arc , LazyLock } ;
@@ -803,7 +803,7 @@ impl IntegrationAttributeRewriter for DataDomeIntegration {
803803 & self ,
804804 _attr_name : & str ,
805805 attr_value : & str ,
806- ctx : & IntegrationAttributeContext < ' _ > ,
806+ _ctx : & IntegrationAttributeContext < ' _ > ,
807807 ) -> AttributeRewriteAction {
808808 // Check if this is a DataDome script URL
809809 let is_datadome =
@@ -814,10 +814,12 @@ impl IntegrationAttributeRewriter for DataDomeIntegration {
814814 }
815815
816816 let path = Self :: extract_datadome_path ( attr_value) ;
817- let new_url = format ! (
818- "{}://{}/integrations/datadome{}" ,
819- ctx. request_scheme, ctx. request_host, path
820- ) ;
817+ // Root-relative so the browser resolves it against the page host.
818+ // Note: a page-level `<base href>` participates in this resolution, so
819+ // on pages that set an external base URL these resolve against that base
820+ // rather than the address-bar origin — an accepted tradeoff, matching
821+ // GTM/Didomi/Testlight which are also relative.
822+ let new_url = format ! ( "/integrations/datadome{path}" ) ;
821823
822824 log:: info!(
823825 "[datadome] Rewriting script src from {} to {}" ,
@@ -1328,10 +1330,7 @@ mod tests {
13281330 let action = integration. rewrite ( "src" , "https://js.datadome.co/tags.js" , & ctx) ;
13291331 match action {
13301332 AttributeRewriteAction :: Replace ( new_url) => {
1331- assert_eq ! (
1332- new_url,
1333- "https://publisher.com/integrations/datadome/tags.js"
1334- ) ;
1333+ assert_eq ! ( new_url, "/integrations/datadome/tags.js" ) ;
13351334 }
13361335 _ => panic ! ( "Expected Replace action" ) ,
13371336 }
@@ -1340,10 +1339,7 @@ mod tests {
13401339 let action = integration. rewrite ( "href" , "https://js.datadome.co/tags.js" , & ctx) ;
13411340 match action {
13421341 AttributeRewriteAction :: Replace ( new_url) => {
1343- assert_eq ! (
1344- new_url,
1345- "https://publisher.com/integrations/datadome/tags.js"
1346- ) ;
1342+ assert_eq ! ( new_url, "/integrations/datadome/tags.js" ) ;
13471343 }
13481344 _ => panic ! ( "Expected Replace action for href" ) ,
13491345 }
@@ -1368,10 +1364,7 @@ mod tests {
13681364 let action = integration. rewrite ( "src" , "https://js.datadome.co/js/check" , & ctx) ;
13691365 match action {
13701366 AttributeRewriteAction :: Replace ( new_url) => {
1371- assert_eq ! (
1372- new_url,
1373- "https://publisher.com/integrations/datadome/js/check"
1374- ) ;
1367+ assert_eq ! ( new_url, "/integrations/datadome/js/check" ) ;
13751368 }
13761369 _ => panic ! ( "Expected Replace action" ) ,
13771370 }
@@ -1380,10 +1373,7 @@ mod tests {
13801373 let action = integration. rewrite ( "href" , "//js.datadome.co/js/signal" , & ctx) ;
13811374 match action {
13821375 AttributeRewriteAction :: Replace ( new_url) => {
1383- assert_eq ! (
1384- new_url,
1385- "https://publisher.com/integrations/datadome/js/signal"
1386- ) ;
1376+ assert_eq ! ( new_url, "/integrations/datadome/js/signal" ) ;
13871377 }
13881378 _ => panic ! ( "Expected Replace action for protocol-relative URL" ) ,
13891379 }
@@ -1392,10 +1382,7 @@ mod tests {
13921382 let action = integration. rewrite ( "src" , "https://js.datadome.co" , & ctx) ;
13931383 match action {
13941384 AttributeRewriteAction :: Replace ( new_url) => {
1395- assert_eq ! (
1396- new_url,
1397- "https://publisher.com/integrations/datadome/tags.js"
1398- ) ;
1385+ assert_eq ! ( new_url, "/integrations/datadome/tags.js" ) ;
13991386 }
14001387 _ => panic ! ( "Expected Replace action for bare domain" ) ,
14011388 }
0 commit comments