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

[Blazor] Finalize behavior of JavaScript initializers for Blazor Web apps #50187

Closed
MackinnonBuck opened this issue Aug 18, 2023 · 1 comment
Closed
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Milestone

Comments

@MackinnonBuck
Copy link
Member

MackinnonBuck commented Aug 18, 2023

Open questions with JavaScript initializers:

  • Should we update the arguments to beforeStart to make it easier to detect which .NET runtime (Server/WebAssembly) is starting?
  • In the case of Blazor WebAssembly, should we wait to invoke beforeStart until we know for sure that Blazor WebAssembly is starting? Currently, beforeStart gets invoked during the Blazor WebAssembly loading procedure, but this doesn't always indicate that Blazor WebAssembly will be used (consider auto mode).
  • In the case of Blazor Server, should the afterStarted callback run each time a new circuit is created? We plan on closing circuits automatically if all Blazor Server components get removed from the page.
  • Should beforeStart execute immediately after Blazor.start() is called when using blazor.web.js? Currently, beforeStart only executes just before Blazor WebAssembly or Blazor Server starts up.
    • Or, alternatively, should the module be imported as soon as Blazor.start() gets called, but beforeStart only gets called when Blazor Server or WebAssembly starts?
  • Should we add arguments to the afterStarted callback to indicate which .NET runtime started?
@MackinnonBuck
Copy link
Member Author

After discussing offline, this is what the current thinking is:

Legacy initializers (beforeStart, afterStarted) will not get invoked in Blazor Web apps by default. However, they can be enabled by specifying a new option that gets passed to Blazor.start().

We will add new beforeRuntimeStart and afterRuntimeStarted callbacks that accept arguments allowing the callback to differentiate between the Blazor Server and Blazor WebAssembly cases. These callbacks will only be executed once per runtime (Server/WebAssembly).

We will also add a new API on the options object passed to Blazor.start() that lets developers listen to the Blazor Server circuit starting, since that can happen multiple times after Blazor Server initialization.

@mkArtakMSFT mkArtakMSFT modified the milestones: 8.0-rc2, 8.0 Sep 29, 2023
@wtgodbe wtgodbe modified the milestones: 8.0, 8.0.0 Oct 3, 2023
mkArtakMSFT pushed a commit that referenced this issue Oct 5, 2023
Updates the initializers in Blazor to include platform specific variants that can be tailored to each runtime.

## Description

* Initializers in Blazor WebAssembly and Server allowed library authors and applications to define JS modules that would be automatically imported by the JS Blazor runtime when it booted up.
* These JS modules were used to tweak the configuration of a given Blazor Runtime or perform additional initialization of JS libraries on the page.
* Since Blazor Web can have multiple runtimes running simultaneously on the page at the same time (server and webassembly), libraries can run into issues like initializing the wrong runtime, running initialization multiple times, or having to wait until we initialize a runtime to apply custom styles and other logic that might be necessary for the correct function of their libraries.
* This PR addresses that by defining runtime specific extension points that libraries can use to tweak a concrete runtime when it boots as well as an initial extension point to target Blazor Web specific scenarios that don't involve a runtime.

Fixes #50187

## Customer Impact

* Existing libraries with initializers won't be initialized until an interactive component is rendered on the page.
* Initialization logic might run more than once for existing libraries, causing all sorts of unpredictable side effects.
  * For example, double initialization.

## Regression?

- [ ] Yes
- [X] No

It's not a regression in the sense that Blazor Web is a new scenario, but a feature that library authors depend on (for example the Fluent UI Blazor components library does) will not work reliably in this scenario.

## Risk

- [ ] High
- [ ] Medium
- [X] Low

The fix is an extension to the existing mechanism to give library authors a path forward. The previous mechanism is disabled by default in Blazor Web scenarios and this fix gives library authors a path forward to make the feature work reliably here. In addition to that, with this fix app developers are not blocked on library authors updating their packages to work on Blazor web, as they can author adapters for the initializers within their apps that use the extension defined by the fix.

## Verification

- [ ] Manual (required)
- [X] Automated

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [X] N/A

----

## When servicing release/2.1

- [ ] Make necessary changes in eng/PatchConfig.props
SteveSandersonMS pushed a commit that referenced this issue Oct 5, 2023
Updates the initializers in Blazor to include platform specific variants that can be tailored to each runtime.

## Description

* Initializers in Blazor WebAssembly and Server allowed library authors and applications to define JS modules that would be automatically imported by the JS Blazor runtime when it booted up.
* These JS modules were used to tweak the configuration of a given Blazor Runtime or perform additional initialization of JS libraries on the page.
* Since Blazor Web can have multiple runtimes running simultaneously on the page at the same time (server and webassembly), libraries can run into issues like initializing the wrong runtime, running initialization multiple times, or having to wait until we initialize a runtime to apply custom styles and other logic that might be necessary for the correct function of their libraries.
* This PR addresses that by defining runtime specific extension points that libraries can use to tweak a concrete runtime when it boots as well as an initial extension point to target Blazor Web specific scenarios that don't involve a runtime.

Fixes #50187

## Customer Impact

* Existing libraries with initializers won't be initialized until an interactive component is rendered on the page.
* Initialization logic might run more than once for existing libraries, causing all sorts of unpredictable side effects.
  * For example, double initialization.

## Regression?

- [ ] Yes
- [X] No

It's not a regression in the sense that Blazor Web is a new scenario, but a feature that library authors depend on (for example the Fluent UI Blazor components library does) will not work reliably in this scenario.

## Risk

- [ ] High
- [ ] Medium
- [X] Low

The fix is an extension to the existing mechanism to give library authors a path forward. The previous mechanism is disabled by default in Blazor Web scenarios and this fix gives library authors a path forward to make the feature work reliably here. In addition to that, with this fix app developers are not blocked on library authors updating their packages to work on Blazor web, as they can author adapters for the initializers within their apps that use the extension defined by the fix.

## Verification

- [ ] Manual (required)
- [X] Automated

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [X] N/A

----

## When servicing release/2.1

- [ ] Make necessary changes in eng/PatchConfig.props
@ghost ghost locked as resolved and limited conversation to collaborators Nov 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

No branches or pull requests

4 participants