prebid / Prebid.js

Setup and manage header bidding advertising partners without writing code or confusing line items. Prebid.js is open source and free.
https://docs.prebid.org
Apache License 2.0
1.28k stars 2.05k forks source link

[Question] Prebid + AMP: Possible fix to refererInfo infinite loop #6483

Closed fasenderos closed 3 years ago

fasenderos commented 3 years ago

Hi, I had problems with Prebid and AMP. I can see from the terminal that prebid.js is loaded, but no request is sent to appnexus. After reading your docs, I discovered that AMP restricts header bidding wrappers like Prebid.js and that I have to create a (or to sign up for a hosted solution of) Prebid Server and using the AMP RTC Config.

Anyway, it seemed really strange to me that prebid.js is correctly loaded on page and nothing happens without raising any errors. After a lot of debugging I found out why. Prebid ends in an endless loop and AMP kills the process.

The loop is generated by the function refererInfo() in refererDetection.js because under AMP currentWindow is always different than win.top. https://github.com/prebid/Prebid.js/blob/42182d6c1844ebc012f3536da3670bc14f75b5db/src/refererDetection.js#L164

I understend that this function try to traverse the DOM to find the windows top, but I don't know if it was intended to have a do while without an escape strategy (e.g. max 100 iterations)

In my case the foundReferrer pushed in the stack array was for the 1st iteration the ampproject url, than the url of my site and then null. And so on following the same pattern

stack = [
   "https://ampproject.org",
   "https://mysite.com",
    null,
   "https://ampproject.org,
   "https://mysite.com",
    null,
   "https://ampproject.org",
   "https://mysite.com",
    null,
    ...
    ...
    ...
]

So to solve this specific problem it was enough to force the exit from the loop at the 2nd iteration while (currentWindow !== win.top && level < 2) - I know i can make a better condition, but this is not the place to talk about that.

After that prebid started working as usual, and the referrer_detection.rd_ref sent to appnexus has the right referer.

My concerns now are:

  1. Is there a way to override Prebid functions without edit the core code?
  2. Are you aware of any cases where are requried more than 2 iterations? I can think only Iframe that is inside an Iframe, that is inside an Iframe etc...
  3. Are these common scenarios?
  4. This change can have any drawbacks in other part of prebid.js?

Thanks in advance.

gglas commented 3 years ago

Does this have ramifications for non-amp implementations of prebid? otherwise, I'm not entirely surprised that .js encounters issues in an amp environment especially with amp's iframe work. @bretg what do you think?

bretg commented 3 years ago

A PR implementing some large number of max iterations in referee discovery would be fine.

We will not be attempting to make Prebid.js work with AMP, though I suppose if someone finds a way better than AMP RTC, that would be interesting.

fasenderos commented 3 years ago

@bretg if you give me some hints on what do you mean for “large number” (50? 100? 1000? or more?) I can make a PR for that.

bretg commented 3 years ago

Seems to me like 10 levels of iframes would be absurd.

But the Prebid Way is to make things configurable because someone out there has 12 nested iframes. So how about setConfig({maxNestedIframes})?

fasenderos commented 3 years ago

Sounds good to me. I’ll make a PR in the next days

ChrisHuie commented 3 years ago

closing issue with the submission of pr #6615