Skip to content

Commit 1423d50

Browse files
author
Ryan Nowak
committed
Improve initialization and SignalR configuration
Fixes: #6887 and #6887 and #5624 Adds support for calling Blazor.start({...}) and passing in a configuration object. For now all you can configure is the SignalR HubConnectionBuilder. This is a priority right now because we want to make configuring SignalR's logging accessible.
1 parent ff31390 commit 1423d50

File tree

13 files changed

+65
-23
lines changed

13 files changed

+65
-23
lines changed

src/Components/Blazor/Templates/src/content/BlazorHosted-CSharp/BlazorHosted-CSharp.Client/wwwroot/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
<body>
1212
<app>Loading...</app>
1313

14-
<script src="_framework/components.webassembly.js"></script>
14+
<script src="_framework/components.webassembly.js" start></script>
1515
</body>
1616
</html>

src/Components/Blazor/Templates/src/content/BlazorStandalone-CSharp/wwwroot/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
<body>
1212
<app>Loading...</app>
1313

14-
<script src="_framework/components.webassembly.js"></script>
14+
<script src="_framework/components.webassembly.js" start></script>
1515
</body>
1616
</html>

src/Components/Blazor/testassets/HostedInAspNet.Client/wwwroot/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
<body>
88
<app>Loading...</app>
99
<script src="customJsFileForTests.js"></script>
10-
<script src="_framework/components.webassembly.js"></script>
10+
<script src="_framework/components.webassembly.js" start></script>
1111
</body>
1212
</html>

src/Components/Blazor/testassets/Microsoft.AspNetCore.Blazor.E2EPerformance/wwwroot/blazor-frame.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
</script>
2020

2121
<app>Loading...</app>
22-
<script src="_framework/components.webassembly.js"></script>
22+
<script src="_framework/components.webassembly.js" start></script>
2323
</body>
2424
</html>

src/Components/Blazor/testassets/StandaloneApp/wwwroot/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
<body>
1212
<app>Loading...</app>
1313

14-
<script src="_framework/components.webassembly.js"></script>
14+
<script src="_framework/components.webassembly.js" start></script>
1515
</body>
1616
</html>

src/Components/Browser.JS/src/Boot.Server.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ import { MessagePackHubProtocol } from '@aspnet/signalr-protocol-msgpack';
55
import { OutOfProcessRenderBatch } from './Rendering/RenderBatch/OutOfProcessRenderBatch';
66
import { internalFunctions as uriHelperFunctions } from './Services/UriHelper';
77
import { renderBatch } from './Rendering/Renderer';
8-
import { fetchBootConfigAsync, loadEmbeddedResourcesAsync } from './BootCommon';
8+
import { fetchBootConfigAsync, loadEmbeddedResourcesAsync, autoStartIfApplicable } from './BootCommon';
99
import { CircuitHandler } from './Platform/Circuits/CircuitHandler';
1010
import { AutoReconnectCircuitHandler } from './Platform/Circuits/AutoReconnectCircuitHandler';
1111

12-
async function boot() {
12+
let started = false;
13+
14+
async function boot(options?: any) {
15+
16+
if (started) {
17+
throw new Error('Blazor has already started.');
18+
}
19+
started = true;
20+
1321
const circuitHandlers: CircuitHandler[] = [ new AutoReconnectCircuitHandler() ];
1422
window['Blazor'].circuitHandlers = circuitHandlers;
1523

@@ -18,7 +26,9 @@ async function boot() {
1826
return loadEmbeddedResourcesAsync(bootConfig);
1927
});
2028

21-
const initialConnection = await initializeConnection(circuitHandlers);
29+
// pass options.configureSignalR to configure the signalR.HubConnectionBuilder
30+
const configureSignalR = (options || {}).configureSignalR
31+
const initialConnection = await initializeConnection(configureSignalR, circuitHandlers);
2232

2333
// Ensure any embedded resources have been loaded before starting the app
2434
await embeddedResourcesPromise;
@@ -29,7 +39,7 @@ async function boot() {
2939
);
3040

3141
window['Blazor'].reconnect = async () => {
32-
const reconnection = await initializeConnection(circuitHandlers);
42+
const reconnection = await initializeConnection(configureSignalR, circuitHandlers);
3343
if (!(await reconnection.invoke<Boolean>('ConnectCircuit', circuitId))) {
3444
return false;
3545
}
@@ -41,14 +51,18 @@ async function boot() {
4151
circuitHandlers.forEach(h => h.onConnectionUp && h.onConnectionUp());
4252
}
4353

44-
async function initializeConnection(circuitHandlers: CircuitHandler[]): Promise<signalR.HubConnection> {
54+
async function initializeConnection(configureSignalR: (builder: signalR.HubConnectionBuilder) => void, circuitHandlers: CircuitHandler[]): Promise<signalR.HubConnection> {
4555
const hubProtocol = new MessagePackHubProtocol();
4656
(hubProtocol as any).name = 'blazorpack';
47-
const connection = new signalR.HubConnectionBuilder()
57+
const connectionBuilder = new signalR.HubConnectionBuilder()
4858
.withUrl('_blazor')
49-
.withHubProtocol(hubProtocol)
50-
.configureLogging(signalR.LogLevel.Information)
51-
.build();
59+
.withHubProtocol(hubProtocol);
60+
61+
if (configureSignalR) {
62+
configureSignalR(connectionBuilder);
63+
}
64+
65+
const connection = connectionBuilder.build();
5266

5367
connection.on('JS.BeginInvokeJS', DotNet.jsCallDispatcher.beginInvokeJSFromDotNet);
5468
connection.on('JS.RenderBatch', (browserRendererId: number, renderId: number, batchData: Uint8Array) => {
@@ -93,4 +107,5 @@ function unhandledError(connection: signalR.HubConnection, err: Error) {
93107
}
94108
}
95109

96-
boot();
110+
window['Blazor'].start = boot;
111+
autoStartIfApplicable(boot);

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import { getAssemblyNameFromUrl } from './Platform/Url';
66
import { renderBatch } from './Rendering/Renderer';
77
import { SharedMemoryRenderBatch } from './Rendering/RenderBatch/SharedMemoryRenderBatch';
88
import { Pointer } from './Platform/Platform';
9-
import { fetchBootConfigAsync, loadEmbeddedResourcesAsync } from './BootCommon';
9+
import { fetchBootConfigAsync, loadEmbeddedResourcesAsync, autoStartIfApplicable } from './BootCommon';
10+
11+
let started = false;
12+
13+
async function boot(options?: any) {
14+
15+
if (started) {
16+
throw new Error('Blazor has already started.');
17+
}
18+
started = true;
1019

11-
async function boot() {
1220
// Configure environment for execution under Mono WebAssembly with shared-memory rendering
1321
const platform = Environment.setPlatform(monoPlatform);
1422
window['Blazor'].platform = platform;
@@ -43,4 +51,5 @@ async function boot() {
4351
platform.callEntryPoint(mainAssemblyName, bootConfig.entryPoint, []);
4452
}
4553

46-
boot();
54+
window['Blazor'].start = boot;
55+
autoStartIfApplicable(boot);

src/Components/Browser.JS/src/BootCommon.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,13 @@ interface BootJsonData {
3737
jsReferences: string[];
3838
linkerEnabled: boolean;
3939
}
40+
41+
// calls Blazor.start() if the script was added with <script src="..." start></script>
42+
export function autoStartIfApplicable(start: (options?: any)=> Promise<void>) {
43+
if (document &&
44+
document.currentScript &&
45+
document.currentScript.hasAttribute("start") &&
46+
document.currentScript.getAttribute("start") !== "false") {
47+
start();
48+
}
49+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { platform } from './Environment';
21
import { navigateTo, internalFunctions as uriHelperInternalFunctions } from './Services/UriHelper';
32
import { internalFunctions as httpInternalFunctions } from './Services/Http';
43
import { attachRootComponentToElement } from './Rendering/Renderer';
5-
import { Pointer } from './Platform/Platform';
64

75
// Make the following APIs available in global scope for invocation from JS
86
window['Blazor'] = {
@@ -13,4 +11,4 @@ window['Blazor'] = {
1311
http: httpInternalFunctions,
1412
uriHelper: uriHelperInternalFunctions
1513
}
16-
};
14+
};

src/Components/test/testassets/BasicTestApp/wwwroot/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
var src = location.hash === '#server'
2727
? 'components.server.js'
2828
: 'components.webassembly.js';
29-
document.write('<script src="_framework/' + src + '"><' + '/script>');
29+
document.write('<script src="_framework/' + src + '" start><' + '/script>');
3030
})();
3131
</script>
3232
</body>

src/Components/test/testassets/ComponentsApp.Server/Pages/Index.cshtml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,12 @@
1515
<app>@(await Html.RenderComponentAsync<App>())</app>
1616

1717
<script src="_framework/components.server.js"></script>
18+
<script>
19+
Blazor.start({
20+
configureSignalR: function (builder) {
21+
builder.configureLogging(2); // LogLevel.Information
22+
}
23+
});
24+
</script>
1825
</body>
1926
</html>

src/Components/test/testassets/TestServer/Pages/PrerenderedHost.cshtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
function loadBootScript(event) {
2020
event.srcElement.disabled = true;
2121
var scriptElem = document.createElement('script');
22+
scriptElem.onload = function () {
23+
Blazor.start();
24+
};
2225
scriptElem.src = '_framework/components.server.js';
2326
document.body.appendChild(scriptElem);
2427
}

src/ProjectTemplates/Web.ProjectTemplates/content/RazorComponentsWeb-CSharp/Pages/Host.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
<body>
2525
<app>@(await Html.RenderComponentAsync<App>())</app>
2626

27-
<script src="_framework/components.server.js"></script>
27+
<script src="_framework/components.server.js" start></script>
2828
</body>
2929
</html>

0 commit comments

Comments
 (0)