Skip to content

Commit

Permalink
Fix for JavaScript module path to handle app not being hosted at the …
Browse files Browse the repository at this point in the history
…root of the website (#3)

* 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)

* 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.
  • Loading branch information
victorperez2911 authored Feb 22, 2024
1 parent 4494351 commit 6a47312
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Blazor.BroadcastChannel/BroadcastChannelService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace Blazor.BroadcastChannel
{
Expand All @@ -7,8 +8,10 @@ internal class BroadcastChannelService : IBroadcastChannelService
private readonly Lazy<ValueTask<IJSObjectReference>> _moduleTask;

public BroadcastChannelService(IJSRuntime jsRuntime)
{
_moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>("import", "/_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js"));
{
string path = "./_content/Blazor.BroadcastChannel/Blazor.BroadcastChannel.js";

_moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>("import", path));
}

public async Task<IBroadcastChannel> CreateOrJoinAsync(string channelName)
Expand Down

0 comments on commit 6a47312

Please sign in to comment.