Loading workspace insights... Statistics interval
7 days30 daysLatest CI Pipeline Executions
8bc4f58c fix[gen2][abTests]: ENG-12786 stop injecting duplicate A/B test init scripts on pages with multiple Content components (#4694)
## Description
The Gen 2 React SDK injects the full `window.builderIoAbTest /
window.builderIoRenderContent` definition as an inline <script> once per
<Content> component, even on pages that have no A/B tests. On pages with
several Content regions (header, body, footer…) this ships multiple
identical ~4KB scripts.
JTI saw 11 copies per page.
**Fix:**
- Only emit the init script when a Content actually renders A/B variants
(gate it behind shouldRenderVariants). Pages with no A/B tests now emit
it zero times.
- Make the script idempotent `(if (!window.builderIoAbTest) { … })` so
repeated definitions never re-run and self-removing on hydration
targets, matching how the SDK's existing A/B scripts already behave.
Re-added the e2e assertion that the revert removed, counting init
scripts in the SSR response (the script self-removes from the live DOM,
so we check the response body). Asserts the realistic post-fix counts.
**Note:**
This eliminates the duplication for the reported case and reduces A/B
pages to one init per A/B-tested Content (defined once at runtime).
Collapsing to a single tag per page regardless of A/B count requires
cross-component coordination and is being tracked as a separate PR,
since that touches the same hydration-sensitive path that caused the
prior revert.
**JIRA Ticket:**
https://builder-io.atlassian.net/browse/ENG-12786
_Screenshot_
Clip of the issue:
https://clips.agent-native.com/r/9yBuG521T6eu
Clip of the fix:
https://clips.agent-native.com/r/JtAevmePWpKT
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes inline script placement and hydration-target self-removal on
the A/B SSR path, which is historically sensitive, though the fix
deliberately avoids reintroducing the prior DOM-mutation regression.
>
> **Overview**
> **Stops shipping duplicate `builderio-init-variants-fns` inline
scripts** when a page has multiple `<Content>` regions. The
`window.builderIoAbTest` / `window.builderIoRenderContent` init block is
**moved out of the always-on path** and **only rendered when
`shouldRenderVariants` is true**, so non–A/B content no longer injects
it.
>
> The init script string is now wrapped in an **idempotent IIFE**
(assign globals only if missing) and on React/React Native hydration
targets **removes itself via `document.currentScript`** after running,
avoiding repeat definitions and the client-side DOM tweaks that
previously caused hydration issues.
>
> **E2E:** SSR A/B tests count `builderio-init-variants-fns` in the
**raw navigation response** (not the live DOM) and expect **one** script
for pages with a single A/B-tested content; Gen1 React and non-SSR
frameworks skip the assertion.
>
> Patch changeset across Gen2 SDK packages documents the behavior
change.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
03df10b9db3f6a46ee812e33bd9dfa25b3769025. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->