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] Ensure WebAssembly startup options are initialized just once #59905

Merged
merged 5 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Components/Web.JS/src/Boot.Web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { LogLevel } from './Platform/Logging/Logger';
import { resolveOptions } from './Platform/Circuits/CircuitStartOptions';
import { JSInitializer } from './JSInitializers/JSInitializers';
import { enableFocusOnNavigate } from './Rendering/FocusOnNavigate';
import { WebAssemblyStartOptions } from './Platform/WebAssemblyStartOptions';

let started = false;
let rootComponentManager: WebRootComponentManager;
Expand Down Expand Up @@ -91,10 +92,11 @@ function onInitialDomContentLoaded(options: Partial<WebStartOptions>) {
// so we do the same here.
const initialCircuitOptions = resolveOptions(options?.circuit || {});
options.circuit = initialCircuitOptions;
options.webAssembly = options.webAssembly || ({} as WebAssemblyStartOptions);
const logger = new ConsoleLogger(initialCircuitOptions.logLevel);
const initializersPromise = fetchAndInvokeInitializers(options, logger);
setCircuitOptions(resolveConfiguredOptions(initializersPromise, initialCircuitOptions));
setWebAssemblyOptions(resolveConfiguredOptions(initializersPromise, options?.webAssembly || {}));
setWebAssemblyOptions(resolveConfiguredOptions(initializersPromise, options.webAssembly));

registerAllComponentDescriptors(document);
rootComponentManager.onDocumentUpdated();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public void InitializersRunsModernCallbacksByDefaultWhenPresent(bool streaming,
var url = $"{ServerPathBase}/initializers?streaming={streaming}&wasm={webassembly}&server={server}";
Navigate(url);

if (webassembly)
{
((IJavaScriptExecutor)Browser).ExecuteScript("sessionStorage.setItem('block-webassembly-settings', 'true')");
}

foreach (var callback in expectedInvokedCallbacks)
{
Browser.Exists(By.Id(callback));
Expand All @@ -56,6 +61,11 @@ public void InitializersRunsClassicInitializersWhenEnabled(bool streaming, bool
var url = $"{ServerPathBase}/initializers?streaming={streaming}&wasm={webassembly}&server={server}";
Navigate(url);

if (webassembly)
{
((IJavaScriptExecutor)Browser).ExecuteScript("sessionStorage.setItem('block-webassembly-settings', 'true')");
}

foreach (var callback in expectedCallbacks)
{
Browser.Exists(By.Id(callback));
Expand All @@ -82,12 +92,12 @@ public static TheoryData<bool, bool, bool, string[]> InitializerTestData()
{
{ false, false, false, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started","modern-before-web-start", "modern-after-web-started"] },
{ false, false, true, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-server-start", "classic-and-modern-after-server-started", "classic-and-modern-circuit-opened", "modern-before-web-start", "modern-after-web-started", "modern-before-server-start", "modern-after-server-started", "modern-circuit-opened" ] },
{ false, true, false, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-web-assembly-start", "classic-and-modern-after-web-assembly-started", "modern-before-web-start", "modern-after-web-started", "modern-before-web-assembly-start", "modern-after-web-assembly-started"] },
{ false, true, true, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-server-start", "classic-and-modern-circuit-opened", "classic-and-modern-after-server-started", "classic-and-modern-before-web-assembly-start", "classic-and-modern-after-web-assembly-started", "modern-before-web-start", "modern-after-web-started", "modern-before-server-start", "modern-circuit-opened", "modern-after-server-started", "modern-before-web-assembly-start", "modern-after-web-assembly-started"] },
{ false, true, false, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-web-assembly-start", "classic-and-modern-after-web-assembly-started", "server--classic-and-modern-before-web-assembly-start", "server--classic-and-modern-after-web-assembly-started", "modern-before-web-start", "modern-after-web-started", "modern-before-web-assembly-start", "modern-after-web-assembly-started"] },
{ false, true, true, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-server-start", "classic-and-modern-circuit-opened", "classic-and-modern-after-server-started", "classic-and-modern-before-web-assembly-start", "classic-and-modern-after-web-assembly-started", "server--classic-and-modern-before-web-assembly-start", "server--classic-and-modern-after-web-assembly-started", "modern-before-web-start", "modern-after-web-started", "modern-before-server-start", "modern-circuit-opened", "modern-after-server-started", "modern-before-web-assembly-start", "modern-after-web-assembly-started"] },
{ true, false, false, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started","modern-before-web-start", "modern-after-web-started"] },
{ true, false, true, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-server-start", "classic-and-modern-after-server-started", "classic-and-modern-circuit-opened", "modern-before-web-start", "modern-after-web-started", "modern-before-server-start", "modern-after-server-started", "modern-circuit-opened" ] },
{ true, true, false, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-web-assembly-start", "classic-and-modern-after-web-assembly-started", "modern-before-web-start", "modern-after-web-started", "modern-before-web-assembly-start", "modern-after-web-assembly-started"] },
{ true, true, true, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-server-start", "classic-and-modern-circuit-opened", "classic-and-modern-after-server-started", "classic-and-modern-before-web-assembly-start", "classic-and-modern-after-web-assembly-started", "modern-before-web-start", "modern-after-web-started", "modern-before-server-start", "modern-circuit-opened", "modern-after-server-started", "modern-before-web-assembly-start", "modern-after-web-assembly-started"] },
{ true, true, false, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-web-assembly-start", "classic-and-modern-after-web-assembly-started", "server--classic-and-modern-before-web-assembly-start", "server--classic-and-modern-after-web-assembly-started", "modern-before-web-start", "modern-after-web-started", "modern-before-web-assembly-start", "modern-after-web-assembly-started"] },
{ true, true, true, ["classic-and-modern-before-web-start", "classic-and-modern-after-web-started", "classic-and-modern-before-server-start", "classic-and-modern-circuit-opened", "classic-and-modern-after-server-started", "classic-and-modern-before-web-assembly-start", "classic-and-modern-after-web-assembly-started", "server--classic-and-modern-before-web-assembly-start", "server--classic-and-modern-after-web-assembly-started", "modern-before-web-start", "modern-after-web-started", "modern-before-server-start", "modern-circuit-opened", "modern-after-server-started", "modern-before-web-assembly-start", "modern-after-web-assembly-started"] },
};

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function afterServerStarted() {
appendElement('classic-and-modern-after-server-started', 'Classic and modern "afterServerStarted"');
}

// Duplicated in Components.TestServer\wwwroot\Components.TestServer.lib.module.js
function appendElement(id, text) {
var content = document.getElementById('initializers-content');
if (!content) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
const enableClassicInitializers = sessionStorage.getItem('enable-classic-initializers') === 'true';
const suppressEnhancedNavigation = sessionStorage.getItem('suppress-enhanced-navigation') === 'true';
const blockLoadBootResource = sessionStorage.getItem('block-load-boot-resource') === 'true';
const blockWebassemblySettings = sessionStorage.getItem('block-webassembly-settings') === 'true';
sessionStorage.removeItem('suppress-enhanced-navigation');
sessionStorage.removeItem('block-load-boot-resource');
sessionStorage.removeItem('enable-classic-initializers');
sessionStorage.removeItem('block-webassembly-settings');

let loadBootResourceUnblocked = null;
if (blockLoadBootResource) {
Expand All @@ -66,6 +68,35 @@

let maxParallelResourceDownloadCount = 0;
let currentParallelResourceDownloadCount = 0;
let webAssemblySettings = {
loadBootResource: function (type, name, defaultUri, integrity) {
switch (type) {
case 'manifest':
case 'dotnetjs':
case 'dotnetwasm':
// note this is resolved to absolute URL using document.baseURI
return `WasmMinimal/_framework/${name}`;
default:
// The following allows us to arbitrarily delay the loading of WebAssembly resources.
// This is useful for guaranteeing that auto mode components will fall back on
// using Blazor server.
currentParallelResourceDownloadCount++;
return fetch(`${document.baseURI}WasmMinimal/_framework/${name}?`, {
method: "GET",
}).then(async (response) => {
if (currentParallelResourceDownloadCount > maxParallelResourceDownloadCount) {
maxParallelResourceDownloadCount = currentParallelResourceDownloadCount;
window['__aspnetcore__testing__max__parallel__resource__download__count'] = maxParallelResourceDownloadCount;
}
currentParallelResourceDownloadCount--;
await loadBootResourceUnblocked;
return response;
});
}
},
};
if (blockWebassemblySettings)
webAssemblySettings = null;

Blazor.start({
ssr: {
Expand All @@ -75,33 +106,7 @@
circuitInactivityTimeoutMs: 100,
},
enableClassicInitializers: enableClassicInitializers,
webAssembly: {
loadBootResource: function (type, name, defaultUri, integrity) {
switch (type) {
case 'manifest':
case 'dotnetjs':
case 'dotnetwasm':
// note this is resolved to absolute URL using document.baseURI
return `WasmMinimal/_framework/${name}`;
default:
// The following allows us to arbitrarily delay the loading of WebAssembly resources.
// This is useful for guaranteeing that auto mode components will fall back on
// using Blazor server.
currentParallelResourceDownloadCount++;
return fetch(`${document.baseURI}WasmMinimal/_framework/${name}?`, {
method: "GET",
}).then(async (response) => {
if (currentParallelResourceDownloadCount > maxParallelResourceDownloadCount) {
maxParallelResourceDownloadCount = currentParallelResourceDownloadCount;
window['__aspnetcore__testing__max__parallel__resource__download__count'] = maxParallelResourceDownloadCount;
}
currentParallelResourceDownloadCount--;
await loadBootResourceUnblocked;
return response;
});
}
},
},
webAssembly: webAssemblySettings,
circuit: {
reconnectionOptions: {
// It's easier to test the reconnection logic if we wait a bit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function beforeWebAssemblyStart() {
appendElement('server--classic-and-modern-before-web-assembly-start', 'Server project Classic and modern "beforeWebAssemblyStart"');
}

export function afterWebAssemblyStarted() {
appendElement('server--classic-and-modern-after-web-assembly-started', 'Server project Classic and modern "afterWebAssemblyStarted"');
}

// Duplicated in BasicTestApp\wwwroot\JSInitializers\ClassicAndModern\BasicTestApp.lib.module.js
function appendElement(id, text) {
var content = document.getElementById('initializers-content');
if (!content) {
return;
}
var element = document.createElement('p');
element.id = id;
element.innerText = text;
content.appendChild(element);
}
Loading