IvanJosipovic / BlazorApplicationInsights

Application Insights for Blazor web applications
https://BlazorApplicationInsights.netlify.app
MIT License
189 stars 26 forks source link

[IDEA] Configuration inconsistencies, init process consolidation #321

Open hakenr opened 1 month ago

hakenr commented 1 month ago

Let's assume IsWasmStandalone="false" (the default).

Currently, the configuration is expected to be provided through pre-rendering (the ApplicationInsightsInit component emits the script tag with the configuration included). However, the instructions and samples still direct you to provide the connection string in the .Client project as well. The connection string provided to the client is not used unless you manually call AppInsights.UpdateCfg().

This should be consolidated.

My proposal:

  1. Provide the script with the initial configuration from the server if static server-side rendering/pre-rendering occurs.
  2. Modify the script to prevent multiple loads (see this example for inspiration). This change would allow removing the if (!IsWasmStandalone) check from ApplicationInsightsInit.razor and enable it to always initialize.
  3. In ApplicationInsightsInit.razor.cs:OnAfterRender(), under firstRender call UpdateCfg() with merging logic to preserve the server-rendered connection string, ensuring it's not overwritten on the client side, where a duplicate connection string configuration won't be needed.

cc @jirikanda