From dcff90c55158734dad1b2d2962cfd2783d383e0d Mon Sep 17 00:00:00 2001 From: Sunny Kolattukudy Date: Sat, 21 Mar 2026 08:40:34 -0400 Subject: [PATCH] =?UTF-8?q?fix(appinsights):=20=F0=9F=90=9B=20inline=20bla?= =?UTF-8?q?zorApplicationInsights=20JS=20in=20rendered=20script=20to=20fix?= =?UTF-8?q?=20404=20on=20=5Fcontent/=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApplicationInsightsInit.razor.cs | 64 +++++++++++++++++-- 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/Imagile.Framework.Blazor.ApplicationInsights/Components/ApplicationInsightsInit.razor.cs b/src/Imagile.Framework.Blazor.ApplicationInsights/Components/ApplicationInsightsInit.razor.cs index 3afb1a8..1ea8a4f 100644 --- a/src/Imagile.Framework.Blazor.ApplicationInsights/Components/ApplicationInsightsInit.razor.cs +++ b/src/Imagile.Framework.Blazor.ApplicationInsights/Components/ApplicationInsightsInit.razor.cs @@ -41,6 +41,58 @@ public partial class ApplicationInsightsInit : ComponentBase, IDisposable private string script { get; set; } = string.Empty; + // Inlined from wwwroot/Imagile.Framework.Blazor.ApplicationInsights.lib.module.js + // Avoids dependency on _content/ static asset serving, which is unreliable for NuGet-distributed RCLs. + private const string BlazerApplicationInsightsJs = """ + window.blazorApplicationInsights = { + addTelemetryInitializer: function (telemetryItem) { + var telemetryInitializer = (envelope) => { + if (telemetryItem.ver !== null) { envelope.ver = telemetryItem.ver; } + if (telemetryItem.name !== null) { envelope.name = telemetryItem.name; } + if (telemetryItem.time !== null) { envelope.time = telemetryItem.time; } + if (telemetryItem.iKey !== null) { envelope.iKey = telemetryItem.iKey; } + if (telemetryItem.ext !== null) { Object.assign(envelope.ext, telemetryItem.ext); } + if (telemetryItem.tags !== null) { Object.assign(envelope.tags, telemetryItem.tags); } + if (telemetryItem.data !== null) { Object.assign(envelope.data, telemetryItem.data); } + if (telemetryItem.baseType !== null) { envelope.baseType = telemetryItem.baseType; } + if (telemetryItem.baseData !== null) { Object.assign(envelope.baseData, telemetryItem.baseData); } + }; + appInsights.addTelemetryInitializer(telemetryInitializer); + }, + trackDependencyData: function (dependencyTelemetry) { + if (dependencyTelemetry.startTime !== null) { + dependencyTelemetry.startTime = new Date(dependencyTelemetry.startTime); + } + appInsights.trackDependencyData(dependencyTelemetry); + }, + getContext: function () { + if (appInsights.context !== undefined) { return appInsights.context; } + }, + cookieMgrSetCookiesEnabled: function (enabled) { + if (appInsights.getCookieMgr !== undefined) { appInsights.getCookieMgr().setEnabled(enabled); } + }, + cookieMgrGetCookiesEnabled: function () { + if (appInsights.getCookieMgr !== undefined) { return appInsights.getCookieMgr().isEnabled(); } + return false; + }, + cookieMgrSet: function (name, value, maxAgeSec, domain, path) { + if (appInsights.getCookieMgr !== undefined) { appInsights.getCookieMgr().set(name, value, maxAgeSec, domain, path); } + }, + cookieMgrGet: function (name) { + if (appInsights.getCookieMgr !== undefined) { appInsights.getCookieMgr().get(name); } + }, + cookieMgrDel: function (name, path) { + if (appInsights.getCookieMgr !== undefined) { appInsights.getCookieMgr().del(name, path); } + }, + cookieMgrPurge: function (name, path) { + if (appInsights.getCookieMgr !== undefined) { appInsights.getCookieMgr().purge(name, path); } + }, + cookieMgrUnload: function (isAsync) { + if (appInsights.getCookieMgr !== undefined) { appInsights.getCookieMgr().unload(isAsync); } + }, + }; + """; + /// protected override void OnInitialized() { @@ -52,6 +104,11 @@ protected override void OnInitialized() // Application Insights Snippet // https://github.com/microsoft/ApplicationInsights-JS#snippet-setup-ignore-if-using-npm-setup script = $$$""""""; + + // Append blazorApplicationInsights setup as a second inline script. + // This avoids any dependency on _content/ static asset serving, which doesn't work + // reliably for .lib.module.js files in NuGet-distributed Razor class libraries. + script += $""; } /// @@ -62,13 +119,6 @@ protected override async Task OnAfterRenderAsync(bool firstRender) ApplicationInsights.InitJSRuntime(JSRuntime); - // Explicitly import the JS module to ensure window.blazorApplicationInsights is defined. - // The .lib.module.js auto-loading mechanism is not reliable for NuGet-distributed RCLs, - // so we import it explicitly here before making any interop calls. - await JSRuntime.InvokeAsync( - "import", - "./_content/Imagile.Framework.Blazor.ApplicationInsights/Imagile.Framework.Blazor.ApplicationInsights.lib.module.js"); - if (Config.Config != null) { try