-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Add new webworker template item
#65037
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
base: main
Are you sure you want to change the base?
Conversation
The webworker item template generates a .csproj file into the content/ folder during build. NuGet was discovering and trying to restore this generated project, but it failed because: 1. The package Microsoft.AspNetCore.Components.WebAssembly version 11.0.0-ci doesn't exist in NuGet feeds (it's built in this repo) 2. The generated project was being evaluated before the package was built Fix: Add Web.ItemTemplates/content/**/*.proj to ProjectToExclude in eng/Build.props, matching the existing exclusion for Web.ProjectTemplates. Also simplified Directory.Build.props in the content folder to remove the sources.props import which is no longer needed since the projects are excluded from build.
The ByteOrderMarkTest requires all .razor files in templates to have UTF-8 BOM (Byte Order Mark) for proper encoding detection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new dotnet new webworker template that enables WebWorker support for Blazor WebAssembly applications, allowing compute-intensive .NET code to run off the main UI thread for improved responsiveness. The template provides both an infrastructure-only mode (--empty) and a full mode with demo code showcasing GitHub API integration.
Changes:
- New WebWorker item template with client library, JavaScript worker infrastructure, and optional demo files
- Integration with BlazorWeb template to optionally include WorkerClient project
- Comprehensive README documentation and localization for 13 languages
Reviewed changes
Copilot reviewed 32 out of 32 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/.template.config/template.json | Template configuration defining parameters, symbols, and post-actions |
| src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/WorkerClient.cs | C# client for communicating with WebWorker via JSInterop |
| src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/wwwroot/worker.js | JavaScript WebWorker entry point for loading .NET runtime |
| src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/wwwroot/worker-client.js | JavaScript client for managing worker lifecycle and message passing |
| src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/Worker/GitHubWorker.cs | Demo worker class showing GitHub API data processing |
| src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/Pages/WebWorkerDemo.razor.cs | Demo Blazor page comparing WebWorker vs UI thread performance |
| src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/Models/GitHubModels.cs | Model classes for GitHub API responses |
| src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/.template.config/template.json | Updated to reference WorkerClient project |
| src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1.sln | Solution file with conditional WorkerClient project inclusion |
| src/ProjectTemplates/Web.ItemTemplates/Microsoft.DotNet.Web.ItemTemplates.csproj | Build configuration for generating WorkerClient.csproj |
| eng/Build.props | Updated to exclude template content projects from build |
| src/ProjectTemplates/Web.ItemTemplates/.gitignore | Ignore generated .csproj files |
| Multiple localization files | Translated template descriptions for 13 languages |
src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/Worker/GitHubWorker.cs
Outdated
Show resolved
Hide resolved
...rojectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/.template.config/template.json
Outdated
Show resolved
Hide resolved
...Templates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/WorkerClient.cs
Show resolved
Hide resolved
...Templates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/WorkerClient.cs
Outdated
Show resolved
Hide resolved
src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/Pages/WebWorkerDemo.razor.cs
Outdated
Show resolved
Hide resolved
src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1.sln
Outdated
Show resolved
Hide resolved
...rojectTemplates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/README.md
Outdated
Show resolved
Hide resolved
src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/Pages/WebWorkerDemo.razor.cs
Show resolved
Hide resolved
...Templates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/WorkerClient.cs
Outdated
Show resolved
Hide resolved
pavelsavara
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall, thanks!
src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/README.md
Outdated
Show resolved
Hide resolved
...rojectTemplates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/README.md
Outdated
Show resolved
Hide resolved
.../Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/wwwroot/worker-client.js
Outdated
Show resolved
Hide resolved
| /** | ||
| * Creates and initializes the WebWorker with event handlers. | ||
| */ | ||
| function createWorker() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Someone may want to have multiple workers. Should we return worker ID ? And then require such id as first parameter to all client calls ?
probably, this is overkill and should keep it simple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javiercn?
I'm voting for simplicity, adding id is not a big change and my rule for this template was: add only bits that are used. I made an exception only for termination method because without it, the method set did not look complete.
...mplates/Web.ItemTemplates/content/WebWorker/WebWorkerTemplate.WorkerClient/wwwroot/worker.js
Show resolved
Hide resolved
src/ProjectTemplates/Web.ItemTemplates/content/WebWorker/Worker/GitHubWorker.cs
Show resolved
Hide resolved
| @@ -0,0 +1,19 @@ | |||
| { | |||
| "author": "Microsoft", | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question:
Is it okay to add AI-created translations? That's how I got them. If not, what's the process?
dotnet new webworkeradds a new project with WebWorker support to existing blazor applicationsBackground
Blazor WASM applications with heavy computing had to rely on the server requests. Computing on UI thread interfered with UI rendering and affected UX. In net10 we added an article with a sample application to make offloading the heavy work to WebWorker easier. In net11 we decided that we should add an official support for a template of c# library that would be added to existing WASM projects. It is this PR.
Usage
Typically, the template should be called in existing WASM application (e.g. in
.Clientproject of Blazor Web app). It has two modes:--emptythat adds only the WebWorker project with aREADMEon how to use the API and default that additionally edits the existing WASM application by adding sample files. See: demo option.Demo option
The template item with
--emptyoption adds a directory with a library project that contains aREADMEfile with API description and quick start tutorial.The template item without
--emptyoption, additionally tries to update the existing blazor application:Pagesdirectory (creates directory if does not exist)AllowUnsafeBlocks=trueproperty that requires user's consent (opens a dialog that can be skipped with--allow-scripts Yes)Demo page fetches issues from a chosen repository with GitHub API. User can do it in
WebWorkerand in UI thread, while clicking the counter button and they can compare the statistics for time spent in fetch/JSON deserialization/data processing methods. Clicking affects UI thread results but cannot affectWebWorkerresults. Because GitHub API has its limitations without authentication, we provide an option to pass token to elevate the limits.Demo uses following WebWorker APIs:
InitializeAsync()- loads the worker javascript file that initializes another .NET runtimeInvokeStringAsync()- invokes worker methods that return string. In our case the response is JSON that can be deserialized toGitHubMetricsSetProgressCallback- to inform UI thread about the state of work in theWebWorker we can callJSImported method:globalThis.postProgressfromWebWorker. UI thread has to have a callback set to the progress events.Fixes dotnet/runtime#95452