From c6325a0abbadf2814677378b4c0102a557badecd Mon Sep 17 00:00:00 2001 From: Victor Perez Date: Mon, 12 Feb 2024 21:04:22 -0300 Subject: [PATCH 1/2] Fix for calculating the Url Base in a wasm app served in a path different from the WebSite Root (e.g. using the href attribute element of the index.html base element) --- .../BroadcastChannelService.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Blazor.BroadcastChannel/BroadcastChannelService.cs b/src/Blazor.BroadcastChannel/BroadcastChannelService.cs index 08bab84..c1fee13 100644 --- a/src/Blazor.BroadcastChannel/BroadcastChannelService.cs +++ b/src/Blazor.BroadcastChannel/BroadcastChannelService.cs @@ -1,4 +1,5 @@ -using Microsoft.JSInterop; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; namespace Blazor.BroadcastChannel { @@ -6,9 +7,13 @@ internal class BroadcastChannelService : IBroadcastChannelService { private readonly Lazy> _moduleTask; - public BroadcastChannelService(IJSRuntime jsRuntime) + public BroadcastChannelService(IJSRuntime jsRuntime, NavigationManager NavManager) { - _moduleTask = new(() => jsRuntime.InvokeAsync("import", "/_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js")); + + string path = new Uri(NavManager.BaseUri).AbsolutePath + "_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js"; + + //_moduleTask = new(() => jsRuntime.InvokeAsync("import", "/_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js")); + _moduleTask = new(() => jsRuntime.InvokeAsync("import", path)); } public async Task CreateOrJoinAsync(string channelName) From 35c0845f27eebe04ca7b8ed8631088917f24461b Mon Sep 17 00:00:00 2001 From: Victor Perez Date: Wed, 21 Feb 2024 17:28:08 -0300 Subject: [PATCH 2/2] The most significant changes involve the modification of the `BroadcastChannelService` class constructor and the adjustment of how the `path` variable is set. The `NavigationManager` parameter has been removed from the constructor of the `BroadcastChannelService` class. The `path` variable, which was previously set using the `BaseUri` property of the `NavigationManager` instance, is now directly set to a specific string. Changes: 1. The `BroadcastChannelService` class constructor has been modified by removing the `NavigationManager` parameter. This change simplifies the constructor and removes the dependency on the `NavigationManager`. 2. The `path` variable setting has been changed. Instead of using the `BaseUri` property of the `NavigationManager` instance, it is now directly set to the string `./_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js`. This change makes the `path` setting more straightforward and less dependent on other variables. 3. A commented out line of code that sets the `_moduleTask` variable has been removed. The `_moduleTask` is still set in the same way, but now it uses the `path` variable instead of a hardcoded string. This change makes the code cleaner and easier to understand. References to the code changes can be found in the `BroadcastChannelService` class and the method where the `path` variable is set. --- src/Blazor.BroadcastChannel/BroadcastChannelService.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Blazor.BroadcastChannel/BroadcastChannelService.cs b/src/Blazor.BroadcastChannel/BroadcastChannelService.cs index c1fee13..a2b2a6d 100644 --- a/src/Blazor.BroadcastChannel/BroadcastChannelService.cs +++ b/src/Blazor.BroadcastChannel/BroadcastChannelService.cs @@ -7,12 +7,10 @@ internal class BroadcastChannelService : IBroadcastChannelService { private readonly Lazy> _moduleTask; - public BroadcastChannelService(IJSRuntime jsRuntime, NavigationManager NavManager) - { - - string path = new Uri(NavManager.BaseUri).AbsolutePath + "_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js"; + public BroadcastChannelService(IJSRuntime jsRuntime) + { + string path = "./_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js"; - //_moduleTask = new(() => jsRuntime.InvokeAsync("import", "/_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js")); _moduleTask = new(() => jsRuntime.InvokeAsync("import", path)); }