dotnet / dotnet/aspnetcore

Unable to use JS Initializer for custom boot resource loader

Open
#54,358 2 comments 0 reactions 0 assignees View on GitHub
area-blazor bug feature-blazor-js-initializers help wanted
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

As described in [the docs](https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0#javascript-initializers)

> For Blazor Server, Blazor WebAssembly, and Blazor Hybrid apps:
> - beforeStart(options, extensions): Called before Blazor starts. For example, beforeStart is used to customize the loading process, logging level, and other options specific to the hosting model.
> - Client-side, beforeStart receives the Blazor options (options) and any extensions (extensions) added during publishing. For example, options can specify the use of a custom [boot resource loader](https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0#load-client-side-boot-resources).

This is not true as we can see from the code, the JS initializers are loaded when `moduleConfig.onConfigLoaded` is called, which is only called after line 186. Therefore, any attempt from JS initializers to set custom boot resource loader function is too late.
https://github.com/dotnet/aspnetcore/blob/87cde8081456ce60ad4d233361bcd07f90dc977b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts#L173-L197

### Expected Behavior

The JS initializers are loaded before the the call to `dotnet.create()` and `beforeStart` is executed before passing the `options.*` to `dotnet.with*()`

### Steps To Reproduce

I have created a minimal reproducible sample in the repository https://github.com/mingyaulee/BlazorJsInitializer
With a JS initializer file

https://github.com/mingyaulee/BlazorJsInitializer/blob/main/BlazorApp/MyLibraryInitializer.js

When loaded shows the following logs:
![image](https://github.com/dotnet/aspnetcore/assets/16444546/46a20143-ec93-4fab-8a45-3d8721a1f068)

### Exceptions (if any)

_No response_

### .NET Version

8.0.2

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.