Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_content js files not being loaded correctly leading to JSInterop errors #61

Open
KieranMoffetAS opened this issue Oct 7, 2021 · 0 comments

Comments

@KieranMoffetAS
Copy link

KieranMoffetAS commented Oct 7, 2021

Hi

I am attempting to load js files from an external component package (mudblazor) . The web app is initialized from a calling application, a console application.

Framework: netcoreapp3.1

_Host.cshtml

<script src="_framework/blazor.server.js" autostart="true"></script>
<script src="_content/mudblazor/mudblazor.min.js"></script>

I discovered some issues related to the .StaticWebAssets.xml not being copied when the main project was built or run using F5. A post-build step now always copies this file.

I discovered that I needed to add this to the CreateHostBuilder method in the Program file

webBuilder.ConfigureAppConfiguration((ctx, cb) =>
{
if (!ctx.HostingEnvironment.IsDevelopment()){
StaticWebAssetsLoader.UseStaticWebAssets(
ctx.HostingEnvironment,
ctx.Configuration);
}
});
With these changes it all works fine, both in debug and release

One item I can't understand is that the StaticWebAssets.xml links back to the install directory of the nuget package. I tried various different ways of linking to the js file (MudBlazor.min.js) which I set to be copied as both content and an embedded resource. However no other method of loading the file worked as it resulted in a JSInterop error

Microsoft.JSInterop.JSException: Could not find 'domService' in 'window'.
Error: Could not find 'domService' in 'window'.
at http://localhost:5006/_framework/blazor.server.js:8:35211
at Array.forEach ()
at p (http://localhost:5006/_framework/blazor.server.js:8:35171)
at http://localhost:5006/_framework/blazor.server.js:8:35881
at new Promise ()
at e.beginInvokeJSFromDotNet (http://localhost:5006/_framework/blazor.server.js:8:35854)
at http://localhost:5006/_framework/blazor.server.js:1:20247
at Array.forEach ()
at e.invokeClientMethod (http://localhost:5006/_framework/blazor.server.js:1:20217)
at e.processIncomingData (http://localhost:5006/_framework/blazor.server.js:1:18028)
at Microsoft.JSInterop.JSRuntime.InvokeWithDefaultCancellation[T](String identifier, Object[] args)
at MudBlazor.MudDrawer.UpdateHeight()
at MudBlazor.MudDrawer.OnAfterRenderAsync(Boolean firstRender)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost: Error: Unhandled exception in circuit 'ZwuqTd7cgtzxRSfhWvHSMqEazFgFlRSPmKJ1lzNwXBY'.

So it appears the only way is to link using the StaticWebAssets file.

This works on a local machine however it would appear that for deployments the staticassets folder from the nuget install folder needs to be distributed and the StaticWebAssets.xml file updated to point at the deployed folder

This does not seem correct. It is a workaround and sustainable moving forward.

What am I missing? Any pointers would be gratefully accepted.

Thanks in advance
Kieran

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant