Skip to content

Commit 953e8b5

Browse files
committed
Emit a separate comment instead of extending component comment
1 parent dff3493 commit 953e8b5

File tree

9 files changed

+34
-38
lines changed

9 files changed

+34
-38
lines changed

src/Components/Endpoints/src/DependencyInjection/WebAssemblyEnvironmentEmitter.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ internal class WebAssemblyEnvironmentEmitter(IHostEnvironment hostEnvironment)
99
{
1010
private bool wasEmittedAlready;
1111

12-
public string? GetEnvironmentOnce()
12+
public bool TryGetEnvironmentOnce(out string environment)
1313
{
1414
if (wasEmittedAlready)
1515
{
16-
return null;
16+
environment = string.Empty;
17+
return false;
1718
}
1819

1920
wasEmittedAlready = true;
20-
return hostEnvironment.EnvironmentName;
21+
environment = hostEnvironment.EnvironmentName;
22+
return true;
2123
}
2224
}

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Streaming.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ private void WriteComponentHtml(int componentId, TextWriter output, bool allowBo
271271
_httpContext.Response.Headers.CacheControl = "no-cache, no-store, max-age=0";
272272
}
273273

274+
if (marker.Type is ComponentMarker.WebAssemblyMarkerType or ComponentMarker.AutoMarkerType)
275+
{
276+
if (_httpContext.RequestServices.GetRequiredService<WebAssemblyEnvironmentEmitter>().TryGetEnvironmentOnce(out var environment))
277+
{
278+
output.Write($"<!--Blazor-WebAssemblyEnvironment:{environment}-->");
279+
}
280+
}
281+
274282
var serializedStartRecord = JsonSerializer.Serialize(marker, ServerComponentSerializationSettings.JsonSerializationOptions);
275283
output.Write("<!--Blazor:");
276284
output.Write(serializedStartRecord);

src/Components/Endpoints/src/Rendering/SSRRenderModeBoundary.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public ComponentMarker ToMarker(HttpContext httpContext, int sequence, object? c
165165
var marker = RenderMode switch
166166
{
167167
InteractiveServerRenderMode server => ComponentMarker.Create(ComponentMarker.ServerMarkerType, server.Prerender, _markerKey),
168-
InteractiveWebAssemblyRenderMode webAssembly => ComponentMarker.Create(ComponentMarker.WebAssemblyMarkerType, webAssembly.Prerender, _markerKey, GetEnvironmentOnce(httpContext)),
169-
InteractiveAutoRenderMode auto => ComponentMarker.Create(ComponentMarker.AutoMarkerType, auto.Prerender, _markerKey, GetEnvironmentOnce(httpContext)),
168+
InteractiveWebAssemblyRenderMode webAssembly => ComponentMarker.Create(ComponentMarker.WebAssemblyMarkerType, webAssembly.Prerender, _markerKey),
169+
InteractiveAutoRenderMode auto => ComponentMarker.Create(ComponentMarker.AutoMarkerType, auto.Prerender, _markerKey),
170170
_ => throw new UnreachableException($"Unknown render mode {RenderMode.GetType().FullName}"),
171171
};
172172

@@ -188,9 +188,6 @@ public ComponentMarker ToMarker(HttpContext httpContext, int sequence, object? c
188188
return marker;
189189
}
190190

191-
private static string? GetEnvironmentOnce(HttpContext httpContext)
192-
=> httpContext.RequestServices.GetRequiredService<WebAssemblyEnvironmentEmitter>().GetEnvironmentOnce();
193-
194191
private ComponentMarkerKey GenerateMarkerKey(int sequence, object? componentKey)
195192
{
196193
var componentTypeNameHash = _componentTypeNameHashCache.GetOrAdd(_componentType, TypeNameHash.Compute);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function setWebAssemblyOptions(initializersReady: Promise<Partial<WebAsse
6868
}
6969
}
7070

71-
export function startWebAssembly(components: RootComponentManager<WebAssemblyComponentDescriptor>, environment: string | undefined): Promise<void> {
71+
export function startWebAssembly(components: RootComponentManager<WebAssemblyComponentDescriptor>, environment: string | null | undefined): Promise<void> {
7272
if (startPromise !== undefined) {
7373
throw new Error('Blazor WebAssembly has already started.');
7474
}
@@ -78,7 +78,7 @@ export function startWebAssembly(components: RootComponentManager<WebAssemblyCom
7878
return startPromise;
7979
}
8080

81-
async function startCore(components: RootComponentManager<WebAssemblyComponentDescriptor>, environment: string | undefined, resolve, _) {
81+
async function startCore(components: RootComponentManager<WebAssemblyComponentDescriptor>, environment: string | null | undefined, resolve, _) {
8282
if (inAuthRedirectIframe()) {
8383
// eslint-disable-next-line @typescript-eslint/no-empty-function
8484
await new Promise(() => { }); // See inAuthRedirectIframe for explanation
@@ -206,12 +206,12 @@ export function waitForBootConfigLoaded(): Promise<MonoConfig> {
206206
return bootConfigPromise;
207207
}
208208

209-
export function loadWebAssemblyPlatformIfNotStarted(environment: string | undefined): Promise<void> {
209+
export function loadWebAssemblyPlatformIfNotStarted(environment: string | null | undefined): Promise<void> {
210210
platformLoadPromise ??= (async () => {
211211
await initializersPromise;
212212
const finalOptions = options ?? {};
213213
if (!finalOptions.environment) {
214-
finalOptions.environment = environment;
214+
finalOptions.environment = environment ?? undefined;
215215
}
216216
const existingConfig = options?.configureRuntime;
217217
finalOptions.configureRuntime = (config) => {

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

Lines changed: 3 additions & 2 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, findWebAssemblyEnvironment } from './Services/ComponentDescriptorDiscovery';
9+
import { WebAssemblyComponentDescriptor, discoverComponents, discoverWebAssemblyEnvironment } from './Services/ComponentDescriptorDiscovery';
1010
import { DotNet } from '@microsoft/dotnet-js-interop';
1111
import { InitialRootComponentsList } from './Services/InitialRootComponentsList';
1212
import { JSEventRegistry } from './Services/JSEventRegistry';
@@ -24,9 +24,10 @@ async function boot(options?: Partial<WebAssemblyStartOptions>): Promise<void> {
2424

2525
JSEventRegistry.create(Blazor);
2626
const webAssemblyComponents = discoverComponents(document, 'webassembly') as WebAssemblyComponentDescriptor[];
27+
const webAssemblyEnvironment = discoverWebAssemblyEnvironment(document);
2728

2829
const components = new InitialRootComponentsList(webAssemblyComponents);
29-
await startWebAssembly(components, findWebAssemblyEnvironment(webAssemblyComponents));
30+
await startWebAssembly(components, webAssemblyEnvironment);
3031
}
3132

3233
Blazor.start = boot;

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

Lines changed: 4 additions & 4 deletions
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, findWebAssemblyEnvironment, mergeDescriptors } from '../../Services/ComponentDescriptorDiscovery';
4+
import { AutoComponentDescriptor, ComponentDescriptor, ServerComponentDescriptor, WebAssemblyComponentDescriptor, canMergeDescriptors, discoverComponents, discoverWebAssemblyEnvironment, 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,7 +13,7 @@ let descriptorHandler: DescriptorHandler | null = null;
1313

1414
export interface DescriptorHandler {
1515
registerComponent(descriptor: ComponentDescriptor): void;
16-
setWebAssemblyEnvironment(webAssemblyEnvironment: string | undefined): void;
16+
setWebAssemblyEnvironment(webAssemblyEnvironment: string | null | undefined): void;
1717
}
1818

1919
export function attachComponentDescriptorHandler(handler: DescriptorHandler) {
@@ -22,7 +22,7 @@ export function attachComponentDescriptorHandler(handler: DescriptorHandler) {
2222

2323
export function registerAllComponentDescriptors(root: Node) {
2424
const descriptors = upgradeComponentCommentsToLogicalRootComments(root);
25-
const webAssemblyEnvironment = findWebAssemblyEnvironment(descriptors);
25+
const webAssemblyEnvironment = discoverWebAssemblyEnvironment(root);
2626
descriptorHandler?.setWebAssemblyEnvironment(webAssemblyEnvironment);
2727

2828
for (const descriptor of descriptors) {
@@ -171,7 +171,7 @@ function treatAsMatch(destination: Node, source: Node) {
171171
}
172172

173173
if (destinationRootDescriptor) {
174-
// Update the existing descriptor with hte new descriptor's data
174+
// Update the existing descriptor with the new descriptor's data
175175
mergeDescriptors(destinationRootDescriptor, sourceRootDescriptor);
176176

177177
const isDestinationInteractive = isInteractiveRootComponentElement(destinationAsLogicalElement);

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,10 @@ export function discoverComponents(root: Node, type: 'webassembly' | 'server' |
1515
const blazorServerStateCommentRegularExpression = /^\s*Blazor-Server-Component-State:(?<state>[a-zA-Z0-9+/=]+)$/;
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+/=]+)$/;
18+
const blazorWebAssemblyEnvironmentCommentRegularExpression = /^\s*Blazor-WebAssemblyEnvironment:(?<environment>[a-zA-Z0-9+/=]+)$/;
1819

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;
20+
export function discoverWebAssemblyEnvironment(root: Node): string | null | undefined {
21+
return discoverBlazorComment(root, blazorWebAssemblyEnvironmentCommentRegularExpression, 'environment');
2922
}
3023

3124
export function discoverServerPersistedState(node: Node): string | null | undefined {
@@ -394,5 +387,4 @@ type WebAssemblyMarkerData = {
394387
assembly: string;
395388
parameterDefinitions: string;
396389
parameterValues: string;
397-
environment: string | undefined;
398390
} & CommonMarkerData;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ export class WebRootComponentManager implements DescriptorHandler, RootComponent
111111
// may take a long time to load, so starting to load them now potentially reduces
112112
// the time to interactvity.
113113
if (descriptor.type === 'webassembly') {
114-
this.startLoadingWebAssemblyIfNotStarted(descriptor.environment);
114+
this.startLoadingWebAssemblyIfNotStarted();
115115
} else if (descriptor.type === 'auto') {
116116
// If the WebAssembly runtime starts downloading because an Auto component was added to
117117
// the page, we limit the maximum number of parallel WebAssembly resource downloads to 1
118118
// so that the performance of any Blazor Server circuit is minimally impacted.
119-
this.startLoadingWebAssemblyIfNotStarted(descriptor.environment, /* maxParallelDownloadsOverride */ 1);
119+
this.startLoadingWebAssemblyIfNotStarted(/* maxParallelDownloadsOverride */ 1);
120120
}
121121

122122
const ssrComponentId = this._nextSsrComponentId++;
@@ -131,14 +131,14 @@ export class WebRootComponentManager implements DescriptorHandler, RootComponent
131131
this.circuitMayHaveNoRootComponents();
132132
}
133133

134-
private async startLoadingWebAssemblyIfNotStarted(environment: string | undefined, maxParallelDownloadsOverride?: number) {
134+
private async startLoadingWebAssemblyIfNotStarted(maxParallelDownloadsOverride?: number) {
135135
if (hasStartedLoadingWebAssemblyPlatform()) {
136136
return;
137137
}
138138

139139
setWaitForRootComponents();
140140

141-
const loadWebAssemblyPromise = loadWebAssemblyPlatformIfNotStarted(environment ?? this._webAssemblyEnvironment);
141+
const loadWebAssemblyPromise = loadWebAssemblyPlatformIfNotStarted(this._webAssemblyEnvironment);
142142
const bootConfig = await waitForBootConfigLoaded();
143143

144144
if (maxParallelDownloadsOverride !== undefined) {

src/Shared/Components/ComponentMarker.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,15 @@ internal struct ComponentMarker
5757
// Serialized values of the component's parameters.
5858
public string? ParameterValues { get; set; }
5959

60-
// The WebHost Environment
61-
public string? Environment { get; set; }
62-
6360
#endregion
6461

65-
public static ComponentMarker Create(string type, bool prerendered, ComponentMarkerKey? key, string? environment = null)
62+
public static ComponentMarker Create(string type, bool prerendered, ComponentMarkerKey? key)
6663
{
6764
return new()
6865
{
6966
Type = type,
7067
PrerenderId = prerendered ? GeneratePrerenderId() : null,
7168
Key = key,
72-
Environment = environment
7369
};
7470
}
7571

0 commit comments

Comments
 (0)