Skip to content

Commit

Permalink
Minor: Allow Blazor start options to be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyaulee committed Oct 15, 2023
1 parent d8e1783 commit 3404c19
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ Using an `app.js`, you can set the following properties in the `BlazorBrowserExt
```javascript
globalThis.BlazorBrowserExtension.ImportBrowserPolyfill = false;
globalThis.BlazorBrowserExtension.StartBlazorBrowserExtension = false;
globalThis.BlazorBrowserExtension.BrowserExtension.InitializeAsync("Production");
globalThis.BlazorBrowserExtension.BrowserExtension.InitializeAsync({
environment: "Production"
});
```

## Build and load extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Blazor.BrowserExtension" Version="0.10.*" />
<PackageReference Include="Blazor.BrowserExtension" Version="0.11.*" />
<!--#if (IsNet5) -->
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.*" PrivateAssets="all" />
Expand Down
15 changes: 6 additions & 9 deletions src/Blazor.BrowserExtension/content/dist/CoreInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BrowserExtension {
this.Config = config;
}

async InitializeAsync(environment) {
async InitializeAsync(blazorStartOptions) {
// import JsBind.Net JS
await import(`${this.Url}content/JsBind.Net/JsBindNet.js`);

Expand All @@ -24,22 +24,19 @@ class BrowserExtension {
const blazorScript = globalThis.document.createElement("script");
blazorScript.src = `${this.Url}framework/blazor.webassembly.js`;
blazorScript.defer = true;
// Blazor is set to not auto start, so that we can start it with different environment name
// Blazor is set to not auto start, so that we can start it with different start options
blazorScript.setAttribute("autostart", "false");
await this.AppendElementToDocumentAsync(blazorScript);

// Start Blazor
const startOption = {
};

if (environment) {
startOption.environment = environment;
if (this.Config.EnvironmentName && !blazorStartOptions.environment) {
blazorStartOptions.environment = this.Config.EnvironmentName;
}

if (this.Mode === BrowserExtensionModes.ContentScript || this.Config.CompressionEnabled) {
startOption.loadBootResource = this._loadBootResource.bind(this);
blazorStartOptions.loadBootResource = this._loadBootResource.bind(this);
}
const blazorStart = globalThis.Blazor.start(startOption);
const blazorStart = globalThis.Blazor.start(blazorStartOptions);
if (blazorStart && blazorStart instanceof Promise) {
await blazorStart;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export default class BrowserExtension {

/**
* Initializes the browser extension.
* @param {string} environment Environment name
* @param {object} blazorStartOptions Blazor WebAssembly start options. Refer to https://github.com/dotnet/aspnetcore/blob/main/src/Components/Web.JS/src/Platform/WebAssemblyStartOptions.ts
* @returns {Promise<BrowserExtension>}
*/
async InitializeAsync(environment) {
async InitializeAsync(blazorStartOptions) {
// import JsBind.Net JS
await import(`${this.Url}content/JsBind.Net/JsBindNet.js`);

Expand All @@ -36,22 +36,19 @@ export default class BrowserExtension {
const blazorScript = globalThis.document.createElement("script");
blazorScript.src = `${this.Url}framework/blazor.webassembly.js`;
blazorScript.defer = true;
// Blazor is set to not auto start, so that we can start it with different environment name
// Blazor is set to not auto start, so that we can start it with different start options
blazorScript.setAttribute("autostart", "false");
await this.AppendElementToDocumentAsync(blazorScript);

// Start Blazor
const startOption = {
};

if (environment) {
startOption.environment = environment;
if (this.Config.EnvironmentName && !blazorStartOptions.environment) {
blazorStartOptions.environment = this.Config.EnvironmentName;
}

if (this.Mode === BrowserExtensionModes.ContentScript || this.Config.CompressionEnabled) {
startOption.loadBootResource = this._loadBootResource.bind(this);
blazorStartOptions.loadBootResource = this._loadBootResource.bind(this);
}
const blazorStart = globalThis.Blazor.start(startOption);
const blazorStart = globalThis.Blazor.start(blazorStartOptions);
if (blazorStart && blazorStart instanceof Promise) {
await blazorStart;
}
Expand Down

0 comments on commit 3404c19

Please sign in to comment.