fix: Ensure adSlot is available before method calls #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ensure the availability of the ad slot before calling methods on it.
First of all I'd like to thank you for your work in keeping this package up to date. We have been dependent on the original NFL and now the Ticketmaster fork, but they don't seem to be making any more updates. We're evaluating our ability to move over to your fork.
Issue
We've had a problem for a while (original and fork) where we are presented with this error. Before it was only on specific pages of our app and on refresh. With the atmedia fork it's on every in-app page transition.
Investigation
I've tried my best to understand what all is happening here functionally and logically, but I am sure that I don't have full knowledge of what is happening. This is my attempt at fixing the issue and the rationale behind it.
The Bling
this.defineSizeMapping(adSlot, sizeMapping)
method seems to be called before the adSlot is even created yet. Thethis._adSlot
being passed down from theshouldComponentUpdate
isundefined
. I tried callingthis.isAdMounted()
to see if I could use that as a check, but it returnstrue
.When looking for where
this._adSlot
is set, I found that it is only set as a result ofcomponentDidUpdate
, which was a little disheartening. In checking which properties/values need to be available for different paths, the best option I could find is the change I'm proposing here.Proposed solution
If
this._adSlot
being set is a result of thethis.renderAd()
andthis.defineSlot()
methods, then it seems to logically make sense to say thatshouldRender
should betrue
whenthis._adSlot
is false-y. This also makesshouldRefresh
becomefalse
because it logically has an inverse relationship withshouldRender
. TheshouldRefresh
wastrue
before which allowed its conditional block below to run, call these methods, and fail.Do you have any better suggestions here? I have seen reference to similar issues in the NFL repo, so I don't think I'm the only one with this problem, but it also doesn't seem widespread either. This error doesn't happen on all the ads on our page.
All tests that were passing before are still passing.