-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Blazor] Custom JS initializers #34798
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
632809c
Custom JS initializer sample
javiercn be2fed8
Option 1
javiercn 7aac021
tmp
javiercn 7a4d14c
undo changes on loader
javiercn 83c3257
Addresses feedback from Steve
javiercn b04e651
Cleanup
javiercn 013195f
Cleanups and Blazor server implementation
javiercn 2c59670
Webassembly fixes
javiercn 299ee4d
Add blazor server support
javiercn 583b0f8
Partial cleanup of the static web assets implementation
javiercn 449cb7e
Support for Blazor webview
javiercn d6d7a95
Fix tests
javiercn 2c98236
Add E2E tests
javiercn 64c3a1d
Sample JS module sample
javiercn de1973d
Address feedback
javiercn 74cd9d5
Enable dynamic import
javiercn 3b50ebb
Fix build
javiercn 77bb124
Fix bad merge
javiercn ca60dd2
Address missing feedback
javiercn 8835f8d
Fix build break
javiercn d05f595
Address feedback
javiercn eae8344
Make public API internal
javiercn 6db97d3
Add additional E2E test for file modules
javiercn 218f0fe
Temporarily supress a bunch of warnings
javiercn 63f6ed3
Add additional test
javiercn a474909
Update JS files
javiercn 7a9f95f
Update identity tests
javiercn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Components/Server/src/CircuitJavaScriptInitializationMiddleware.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.AspNetCore.Components.Server; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Microsoft.AspNetCore.Builder | ||
| { | ||
| internal class CircuitJavaScriptInitializationMiddleware | ||
| { | ||
| private readonly IList<string> _initializers; | ||
|
|
||
| // We don't need the request delegate for anything, however we need to inject it to satisfy the middleware | ||
| // contract. | ||
| public CircuitJavaScriptInitializationMiddleware(IOptions<CircuitOptions> options, RequestDelegate _) | ||
| { | ||
| _initializers = options.Value.JavaScriptInitializers; | ||
| } | ||
|
|
||
| public async Task InvokeAsync(HttpContext context) | ||
| { | ||
| await context.Response.WriteAsJsonAsync(_initializers); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Components/Server/src/Circuits/CircuitOptionsJavaScriptInitializersConfiguration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Server.Circuits | ||
| { | ||
| internal class CircuitOptionsJavaScriptInitializersConfiguration : IConfigureOptions<CircuitOptions> | ||
| { | ||
| private readonly IWebHostEnvironment _environment; | ||
|
|
||
| public CircuitOptionsJavaScriptInitializersConfiguration(IWebHostEnvironment environment) | ||
| { | ||
| _environment = environment; | ||
| } | ||
|
|
||
| public void Configure(CircuitOptions options) | ||
| { | ||
| var file = _environment.WebRootFileProvider.GetFileInfo($"{_environment.ApplicationName}.modules.json"); | ||
| if (file.Exists) | ||
| { | ||
| var initializers = JsonSerializer.Deserialize<string[]>(file.CreateReadStream()); | ||
| for (var i = 0; i < initializers.Length; i++) | ||
| { | ||
| var initializer = initializers[i]; | ||
| options.JavaScriptInitializers.Add(initializer); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Components/Web.JS/src/JSInitializers/JSInitializers.Server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { BootJsonData } from "../Platform/BootConfig"; | ||
| import { CircuitStartOptions } from "../Platform/Circuits/CircuitStartOptions"; | ||
| import { JSInitializer } from "./JSInitializers"; | ||
|
|
||
| export async function fetchAndInvokeInitializers(options: Partial<CircuitStartOptions>) : Promise<JSInitializer> { | ||
| const jsInitializersResponse = await fetch('_blazor/initializers', { | ||
| method: 'GET', | ||
| credentials: 'include', | ||
| cache: 'no-cache' | ||
| }); | ||
|
|
||
| const initializers: string[] = await jsInitializersResponse.json(); | ||
| const jsInitializer = new JSInitializer(); | ||
| await jsInitializer.importInitializersAsync(initializers, [options]); | ||
| return jsInitializer; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/Components/Web.JS/src/JSInitializers/JSInitializers.WebAssembly.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { BootJsonData } from "../Platform/BootConfig"; | ||
| import { WebAssemblyStartOptions } from "../Platform/WebAssemblyStartOptions"; | ||
| import { JSInitializer } from "./JSInitializers"; | ||
|
|
||
| export async function fetchAndInvokeInitializers(bootConfig: BootJsonData, options: Partial<WebAssemblyStartOptions>) : Promise<JSInitializer> { | ||
| const initializers = bootConfig.resources.libraryInitializers; | ||
| const jsInitializer = new JSInitializer(); | ||
| if (initializers) { | ||
| await jsInitializer.importInitializersAsync( | ||
| Object.keys(initializers), | ||
| [options, bootConfig.resources.extensions]); | ||
| } | ||
|
|
||
| return jsInitializer; | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Components/Web.JS/src/JSInitializers/JSInitializers.WebView.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { JSInitializer } from "./JSInitializers"; | ||
|
|
||
| export async function fetchAndInvokeInitializers() : Promise<JSInitializer> { | ||
| const jsInitializersResponse = await fetch('_framework/blazor.modules.json', { | ||
| method: 'GET', | ||
| credentials: 'include', | ||
| cache: 'no-cache' | ||
| }); | ||
|
|
||
| const initializers: string[] = await jsInitializersResponse.json(); | ||
| const jsInitializer = new JSInitializer(); | ||
| await jsInitializer.importInitializersAsync(initializers, []); | ||
| return jsInitializer; | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/Components/Web.JS/src/JSInitializers/JSInitializers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Blazor } from '../GlobalExports'; | ||
|
|
||
| type BeforeBlazorStartedCallback = (...args: unknown[]) => Promise<void>; | ||
| export type AfterBlazorStartedCallback = (blazor: typeof Blazor) => Promise<void>; | ||
| type BlazorInitializer = { beforeStart: BeforeBlazorStartedCallback, afterStarted: AfterBlazorStartedCallback }; | ||
|
|
||
| export class JSInitializer { | ||
| private afterStartedCallbacks: AfterBlazorStartedCallback[] = []; | ||
|
|
||
| async importInitializersAsync(initializerFiles: string[], initializerArguments: unknown[]): Promise<void> { | ||
| await Promise.all(initializerFiles.map(f => importAndInvokeInitializer(this, f))); | ||
|
|
||
| function adjustPath(path: string): string { | ||
| // This is the same we do in JS interop with the import callback | ||
| const base = document.baseURI; | ||
| path = base.endsWith('/') ? `${base}${path}` : `${base}/${path}`; | ||
| return path; | ||
| } | ||
|
|
||
| async function importAndInvokeInitializer(jsInitializer: JSInitializer, path: string): Promise<void> { | ||
| const adjustedPath = adjustPath(path); | ||
| const initializer = await import(/* webpackIgnore: true */ adjustedPath) as Partial<BlazorInitializer>; | ||
| if (initializer === undefined) { | ||
| return; | ||
| } | ||
| const { beforeStart: beforeStart, afterStarted: afterStarted } = initializer; | ||
| if (afterStarted) { | ||
| jsInitializer.afterStartedCallbacks.push(afterStarted); | ||
| } | ||
|
|
||
| if (beforeStart) { | ||
| return beforeStart(...initializerArguments); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async invokeAfterStartedCallbacks(blazor: typeof Blazor) { | ||
| await Promise.all(this.afterStartedCallbacks.map(callback => callback(blazor))); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.