Skip to content

Unable to use JS Initializer for custom boot resource loader #54358

@mingyaulee

Description

@mingyaulee

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

As described in the docs

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.

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.

async function createRuntimeInstance(options: Partial<WebAssemblyStartOptions>, onConfigLoaded?: (loadedConfig: MonoConfig) => void): Promise<void> {
const { dotnet } = await importDotnetJs(options);
const moduleConfig = prepareRuntimeConfig(options, onConfigLoaded);
if (options.applicationCulture) {
dotnet.withApplicationCulture(options.applicationCulture);
}
if (options.environment) {
dotnet.withApplicationEnvironment(options.environment);
}
if (options.loadBootResource) {
dotnet.withResourceLoader(options.loadBootResource);
}
const anyDotnet = (dotnet as any);
anyDotnet.withModuleConfig(moduleConfig);
if (options.configureRuntime) {
options.configureRuntime(dotnet);
}
runtime = await dotnet.create();
}

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

Exceptions (if any)

No response

.NET Version

8.0.2

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.feature-blazor-js-initializershelp wantedUp for grabs. We would accept a PR to help resolve this issue

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions