microsoft / ApplicationInsights-JS

Microsoft Application Insights SDK for JavaScript
MIT License
650 stars 240 forks source link

[BUG] ld: -1 not blocking as expected #2352

Open IvanJosipovic opened 5 months ago

IvanJosipovic commented 5 months ago

Hello,

I am the author of BlazorApplicationInsights. In my recent update, I am utilizing the updateCfg() function to update the AI settings programmatically when Blazor launches. This appears to have introduced a race condition where frequently the App Insights snippet isn't fully loaded when Blazor launches, even though we are using "ld: -1".

See issue, https://github.com/IvanJosipovic/BlazorApplicationInsights/issues/287



**Expected behavior**
Setting "ld: -1" should block the execution of the page.

**What I tried**
I tried launching Blazor using the onInit function, this does work, however, it breaks if Application Insights is blocked due to AdBlock.
MSNev commented 5 months ago

Yes, more recent browsers change the way they process the order of loading scripts and it is no longer possible (for all modern browsers) to use this to control the load / execution order. We have investigate and we don't have a solution to control this again.

And the updateCfg function current does not have an SDK Loader proxy (to delay the execution until the SDK was actually loaded), which would be one possible solution (to reduce the race condition).

We introduced an onInit function callback to try and address (help) the sequence race condition, with this callback, it's only called after the SDK is loaded. So you could set flags / etc to wait until the onInit has been called. https://github.com/Microsoft/ApplicationInsights-JS?tab=readme-ov-file#example-using-the-snippet-oninit-callback

Another option which we are currently coding and intent to release in the not too distant future (as v3.3.0) is the ability to provide Promises for the connectionString, instrumentationKey and endpointUrl which will cause events to "wait" until the promise(s) are resolve / rejected before continuing. https://github.com/microsoft/ApplicationInsights-JS/pull/2340

While this doesn't directly help with the race condition of calling updateCfg before the SDK is loaded, it would provide another to sequence the calls.