Skip to content

Commit dff3493

Browse files
committed
Find environment from all component descriptors
and use it instead of relying on first component in blazor.web
1 parent 5a1040d commit dff3493

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/Components/Web.JS/src/Boot.WebAssembly.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Blazor } from './GlobalExports';
66
import { shouldAutoStart } from './BootCommon';
77
import { WebAssemblyStartOptions } from './Platform/WebAssemblyStartOptions';
88
import { setWebAssemblyOptions, startWebAssembly } from './Boot.WebAssembly.Common';
9-
import { WebAssemblyComponentDescriptor, discoverComponents } from './Services/ComponentDescriptorDiscovery';
9+
import { WebAssemblyComponentDescriptor, discoverComponents, findWebAssemblyEnvironment } from './Services/ComponentDescriptorDiscovery';
1010
import { DotNet } from '@microsoft/dotnet-js-interop';
1111
import { InitialRootComponentsList } from './Services/InitialRootComponentsList';
1212
import { JSEventRegistry } from './Services/JSEventRegistry';
@@ -26,18 +26,7 @@ async function boot(options?: Partial<WebAssemblyStartOptions>): Promise<void> {
2626
const webAssemblyComponents = discoverComponents(document, 'webassembly') as WebAssemblyComponentDescriptor[];
2727

2828
const components = new InitialRootComponentsList(webAssemblyComponents);
29-
await startWebAssembly(components, findEnvironment(webAssemblyComponents));
30-
}
31-
32-
function findEnvironment(webAssemblyComponents: WebAssemblyComponentDescriptor[]): string | undefined {
33-
for (let index = 0; index < webAssemblyComponents.length; index++) {
34-
const component = webAssemblyComponents[index];
35-
if (component.environment) {
36-
return component.environment;
37-
}
38-
}
39-
40-
return undefined;
29+
await startWebAssembly(components, findWebAssemblyEnvironment(webAssemblyComponents));
4130
}
4231

4332
Blazor.start = boot;

src/Components/Web.JS/src/Rendering/DomMerging/DomSync.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
import { AutoComponentDescriptor, ComponentDescriptor, ServerComponentDescriptor, WebAssemblyComponentDescriptor, canMergeDescriptors, discoverComponents, mergeDescriptors } from '../../Services/ComponentDescriptorDiscovery';
4+
import { AutoComponentDescriptor, ComponentDescriptor, ServerComponentDescriptor, WebAssemblyComponentDescriptor, canMergeDescriptors, discoverComponents, findWebAssemblyEnvironment, mergeDescriptors } from '../../Services/ComponentDescriptorDiscovery';
55
import { isInteractiveRootComponentElement } from '../BrowserRenderer';
66
import { applyAnyDeferredValue } from '../DomSpecialPropertyUtil';
77
import { LogicalElement, getLogicalChildrenArray, getLogicalNextSibling, getLogicalParent, getLogicalRootDescriptor, insertLogicalChild, insertLogicalChildBefore, isLogicalElement, toLogicalElement, toLogicalRootCommentElement } from '../LogicalElements';
@@ -13,6 +13,7 @@ let descriptorHandler: DescriptorHandler | null = null;
1313

1414
export interface DescriptorHandler {
1515
registerComponent(descriptor: ComponentDescriptor): void;
16+
setWebAssemblyEnvironment(webAssemblyEnvironment: string | undefined): void;
1617
}
1718

1819
export function attachComponentDescriptorHandler(handler: DescriptorHandler) {
@@ -21,6 +22,8 @@ export function attachComponentDescriptorHandler(handler: DescriptorHandler) {
2122

2223
export function registerAllComponentDescriptors(root: Node) {
2324
const descriptors = upgradeComponentCommentsToLogicalRootComments(root);
25+
const webAssemblyEnvironment = findWebAssemblyEnvironment(descriptors);
26+
descriptorHandler?.setWebAssemblyEnvironment(webAssemblyEnvironment);
2427

2528
for (const descriptor of descriptors) {
2629
descriptorHandler?.registerComponent(descriptor);

src/Components/Web.JS/src/Services/ComponentDescriptorDiscovery.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ const blazorServerStateCommentRegularExpression = /^\s*Blazor-Server-Component-S
1616
const blazorWebAssemblyStateCommentRegularExpression = /^\s*Blazor-WebAssembly-Component-State:(?<state>[a-zA-Z0-9+/=]+)$/;
1717
const blazorWebInitializerCommentRegularExpression = /^\s*Blazor-Web-Initializers:(?<initializers>[a-zA-Z0-9+/=]+)$/;
1818

19+
export function findWebAssemblyEnvironment(components: ComponentDescriptor[]): string | undefined {
20+
for (let index = 0; index < components.length; index++) {
21+
const component = components[index];
22+
const webAssemblyComponent = component as WebAssemblyComponentDescriptor;
23+
if (webAssemblyComponent.environment) {
24+
return webAssemblyComponent.environment;
25+
}
26+
}
27+
28+
return undefined;
29+
}
30+
1931
export function discoverServerPersistedState(node: Node): string | null | undefined {
2032
return discoverBlazorComment(node, blazorServerStateCommentRegularExpression);
2133
}

src/Components/Web.JS/src/Services/WebRootComponentManager.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export class WebRootComponentManager implements DescriptorHandler, RootComponent
6363

6464
private _circuitInactivityTimeoutId: any;
6565

66+
private _webAssemblyEnvironment: string | undefined;
67+
6668
// Implements RootComponentManager.
6769
// An empty array becuase all root components managed
6870
// by WebRootComponentManager are added and removed dynamically.
@@ -94,6 +96,10 @@ export class WebRootComponentManager implements DescriptorHandler, RootComponent
9496
this.rootComponentsMayRequireRefresh();
9597
}
9698

99+
public setWebAssemblyEnvironment(webAssemblyEnvironment: string | undefined): void {
100+
this._webAssemblyEnvironment = webAssemblyEnvironment;
101+
}
102+
97103
public registerComponent(descriptor: ComponentDescriptor) {
98104
if (this._seenDescriptors.has(descriptor)) {
99105
return;
@@ -132,7 +138,7 @@ export class WebRootComponentManager implements DescriptorHandler, RootComponent
132138

133139
setWaitForRootComponents();
134140

135-
const loadWebAssemblyPromise = loadWebAssemblyPlatformIfNotStarted(environment);
141+
const loadWebAssemblyPromise = loadWebAssemblyPlatformIfNotStarted(environment ?? this._webAssemblyEnvironment);
136142
const bootConfig = await waitForBootConfigLoaded();
137143

138144
if (maxParallelDownloadsOverride !== undefined) {
@@ -182,7 +188,7 @@ export class WebRootComponentManager implements DescriptorHandler, RootComponent
182188
this.startLoadingWebAssemblyIfNotStarted(environment);
183189

184190
if (!hasStartedWebAssembly()) {
185-
await startWebAssembly(this, environment);
191+
await startWebAssembly(this, environment ?? this._webAssemblyEnvironment);
186192
}
187193
}
188194

0 commit comments

Comments
 (0)