Skip to content

Optable RTD Module: use cached data before the bundle loads - #15748

Closed
MO-Thibault wants to merge 7 commits into
prebid:masterfrom
Optable:mo/optable-rtd-handlertd-early
Closed

MO-Thibault wants to merge 7 commits into
prebid:masterfrom
Optable:mo/optable-rtd-handlertd-early

Conversation

@MO-Thibault

@MO-Thibault MO-Thibault commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Type of change

  • Bugfix

  • Does this change affect user-facing APIs or examples documented on http://prebid.org?

Description of change

Without bundleUrl, getBidRequestData queues the whole call on window.optable.cmd, which only runs once the Optable bundle has loaded. With bundleUrl it waits for the script's onload. Either way, nothing runs before the bundle is ready. An auction that starts earlier waits for it (up to auctionDelay with waitForIt) or goes out with no Optable data, even when the bundle cached that visitor's targeting data on a previous page.

This also defeats publisher handlers written to cover that case (call window.optable.rtd.handleRtd if the bundle is ready, otherwise merge cached data). Since the module only calls them from inside the queue (or after the bundleUrl onload), the bundle is always ready by then and the cache branch never runs. In production we see 3 to 5% of first auctions on a page complete before the bundle loads.

The fix calls the handler, default or custom, as soon as the auction starts, and moves the cache logic into the default handler:

  1. Bundle loaded and exposes window.optable.rtd.handleRtd: call it. Bundles keep overriding that function to change how data is merged.
  2. Bundle not loaded: merge the targeting data cached in localStorage under OPTABLE_RESOLVED, read through the storage manager so deviceAccess and activity controls apply. No wait. OPTABLE_RESOLVED is the default optableCacheTargeting key of the Optable SDK's Prebid RTD helper (buildRTD), and the key Optable's bundles write their resolved targeting to. Pages that use the SDK with a different cache key get no cache hit and fall through to step 3, which is the previous behaviour.
  3. Nothing cached: wait for the bundle via window.optable.cmd, then continue as in 1. If the bundle has no rtd.handleRtd, this is the previous behaviour (wait for optable-targeting:change).

With bundleUrl, the bundle is still loaded through loadExternalScript, but no handler waits for onload.

Cached EIDs can carry _ref, cache-only UID2 refresh material written by older Optable bundles. The SDK strips it before EIDs reach bid requests; readCachedTargeting now strips it too, so it never reaches bidders.

A custom params.handleRtd is now also called right away, before the bundle may have loaded, and decides for itself whether to wait (for example through window.optable.cmd). Handlers written to read the cache before the bundle loads now work as intended. Behaviour change: a custom handler that assumes the bundle is loaded (like the previous docs example, which called window.optable.instance directly) now runs too early; the error is caught and the auction continues without Optable data for that call. The docs and the example page now show how to wait for the bundle first. Publishers who wrote a handler only to get the cache behaviour can remove it.

mergeOptableData now always awaits the handler. It used to await it only when handleRtdFn.constructor.name === 'AsyncFunction'. Builds that transpile async functions (gulp build --ES5, or a publisher's own Babel setup with older targets) turn the default handler into a plain function, so that check failed. With no cached data, the RTD callback then fired immediately, and the auction went out without waiting for the bundle. Awaiting a non-promise value is a no-op, so synchronous handlers behave as before. Custom handlers no longer need to stay native async functions.

Tests in test/spec/modules/optableRtdProvider_spec.js:

  • delegates to the bundle's rtd.handleRtd when available
  • merges cached data right away when the bundle is not loaded, nothing queued
  • waits for the bundle when nothing is cached, then uses its rtd.handleRtd
  • ignores a cache entry with no EIDs, and an unreadable one
  • strips cache-only _ref metadata from cached EIDs
  • mergeOptableData waits for a promise returned by a non-async function (e.g. a transpiled async function)
  • getBidRequestData calls back right away with cached data
  • a custom handleRtd is called right away, with and without bundleUrl
  • existing tests updated: with the bundle already loaded, the default handler no longer goes through the queue; the error-handling test no longer runs a queued custom handler

Also checked in a browser against builds of Prebid 9.53, 10.29 and 11.41, plus 10.29 with --ES5, with a simulated Optable bundle: cached EIDs are merged at the start of the auction; with nothing cached, the auction waits for the bundle; an unreadable cache entry falls back to waiting; a bundle without rtd.handleRtd still works through the targeting event.

modules/optableRtdProvider.md documents the default handler and when a custom handler runs; the commented-out custom handler in integrationExamples/gpt/optableRtdProvider_example.html now waits for the bundle. Docs PR on prebid.github.io to follow.

Other information

Supersedes the approach in #14291 (reverted in #14340 for lint), which also read the cache but kept the queue in front of it.

🤖 Generated with Claude Code

The default handler now runs as soon as the auction starts: it calls the
bundle's window.optable.rtd.handleRtd when available, merges the targeting
data cached under OPTABLE_RESOLVED (via the storage manager) when the bundle
has not loaded yet, and only waits for the bundle when nothing is cached.
A custom handleRtd still runs once the bundle has loaded, unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

This PR introduces changes that may not work on all browsers. According to Babel, the following polyfills may be needed, and they are not automatically included:

  • Changes to modules/optableRtdProvider.js may need:
    • es.iterator.map
    • es.json.parse
    • esnext.iterator.map
    • esnext.json.parse

The best way to address this is to provide good test coverage, as normal PR checks run unit tests on older browsers.

@barecheck

barecheck Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Barecheck - Code coverage report

Total: 91.66%

Your code coverage diff: 0.00% ▴

Uncovered files and lines
FileLines
modules/optableRtdProvider.js55, 215-216, 244-245

MO-Thibault and others added 4 commits October 15, 2026 09:34
A custom handleRtd was still queued on window.optable.cmd (or behind the
bundleUrl onload), so handlers that read cached data before the bundle
loads never got to. The module now calls every handler as soon as the
auction starts and loads bundleUrl in parallel. A custom handler decides
for itself whether to wait for the bundle; the docs and example show how.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
mergeOptableData only awaited handlers whose constructor is a native
AsyncFunction. Builds that transpile async functions (gulp --ES5, or a
publisher's own Babel setup with older targets) turn the default handler
into a plain function, so the RTD callback fired immediately and, with no
cached data, the auction went out without waiting for the Optable bundle.

Always await the handler instead. Awaiting a non-promise value is a no-op,
so synchronous handlers behave the same.

Co-Authored-By: Claude <noreply@anthropic.com>
Caches written by older Optable bundles carry UID2 refresh material as
`_ref` on each EID. The Optable SDK strips it before EIDs reach bid
requests, but the cache path merged the cached ORTB2 as is, so `_ref`
was sent to every bidder. Strip it in readCachedTargeting.

const result = { bundleUrl, adserverTargeting, handleRtd };
if (instance !== null) {
result.instance = instance;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs test coverage for warning

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests for every warning and error the module logs. The module's line coverage is now 100%. Line 55 is result.instance = instance, though, so I wasn't sure which warning you meant. Let me know if I missed one.

Comment thread modules/optableRtdProvider.js Outdated

// Call the handler right away. The default handler copes with the bundle
// not being loaded yet; a custom handler decides for itself whether to wait.
mergeOptableData(handleRtdFn, reqBidsConfigObj, optableExtraData, mergeDeep).then(callback, callback);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log error when this fails IMO

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ffb602c. A rejected handler now logs handleRtd failed, continuing the auction without Optable data with the error, then calls the callback.

const cachedOrtb2 = readCachedTargeting();
if (cachedOrtb2) {
logMessage('Optable bundle not loaded yet, using cached targeting data');
mergeFn(reqBidsConfigObj.ortb2Fragments.global, cachedOrtb2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also put them in user.ext.eids

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ffb602c, for both the cache path and the targeting-event path. I put the EIDs in user.ext.eids only, not in both places. The ORTB 2.5 translator (moveRule('user.eids', …)) overwrites user.ext.eids with user.eids, which would wipe other ID modules' EIDs for 2.5 bidders. user.ext.eids is also where the userId module puts EIDs and where userIdAsEids reads them. Happy to write both if you'd rather.

Comment thread modules/optableRtdProvider.js Outdated
Comment on lines +93 to +96
const isBundleLoaded = () => {
const optableBundle = /** @type {Object} */ (window.optable);
return typeof optableBundle?.rtd?.handleRtd === 'function' || !!optableBundle?.instance;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't support multi instance. We need to consider params.instance as well

Suggested change
const isBundleLoaded = () => {
const optableBundle = /** @type {Object} */ (window.optable);
return typeof optableBundle?.rtd?.handleRtd === 'function' || !!optableBundle?.instance;
};
const isBundleLoaded = (instance = null) => {
const optableBundle = /** @type {Object} */ (window.optable);
return typeof optableBundle?.rtd?.handleRtd === 'function' || !!optableBundle?.[instance || 'instance'];
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ffb602c. isBundleLoaded now takes params.instance as you suggested. I also passed it through to waitForOptableEvent, which had the same hardcoded window.optable.instance. The handler receives it as an optional 4th argument, so custom handlers get it too.

- Honour params.instance when checking whether the bundle has loaded and
  when reading targeting from the SDK, so named instances work.
- Merge EIDs from the cache and the targeting event into user.ext.eids,
  where the userId module puts them and userIdAsEids reads them. Writing
  user.eids would let the ORTB 2.5 translator overwrite user.ext.eids.
- Log an error when the handler fails instead of swallowing it.
- Cover every logged warning and error in tests; module line coverage is
  now 100%.
params.instance is the name of the SDK instance on window.optable, a
string defaulting to 'instance', not an object defaulting to
window.optable.rtd.instance. Also document the handler's instance
argument, when the bundle counts as loaded, and that cached EIDs go to
user.ext.eids.
@jplaroche

Copy link
Copy Markdown
Contributor

Docs PR: prebid/prebid.github.io#6961 (draft until this one is released).

mosherBT added a commit to mosherBT/Prebid.js that referenced this pull request Nov 16, 2026
Folds MO-Thibault's Optable/Prebid.js mo/optable-rtd-handlertd-early (af3301a) into this PR. Conflicts were the duplicate storage manager setup, the handleRtd typings, the spec imports and the example page (kept the enableTargeting example). Adds OPTABLE_RESOLVED to the storage disclosure.
@MO-Thibault
MO-Thibault deleted the mo/optable-rtd-handlertd-early branch November 20, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants