Skip to content

Fix WebWorker template failing in published Blazor WASM apps#65885

Merged
ilonatommy merged 9 commits intomainfrom
ilonatommy/fix-65820
Mar 24, 2026
Merged

Fix WebWorker template failing in published Blazor WASM apps#65885
ilonatommy merged 9 commits intomainfrom
ilonatommy/fix-65820

Conversation

@ilonatommy
Copy link
Copy Markdown
Member

@ilonatommy ilonatommy commented Mar 20, 2026

Web Workers do not inherit the page's import map, causing fingerprinted asset resolution to fail.

During dotnet publish, Blazor WebAssembly apps fingerprint framework JavaScript files. The non-fingerprinted files do not exist in the published output. index.html contains an ES Module Import Map that maps the old names to the new ones. It did not fail on dotnet run (WebWorkerTemplate_CanInvokeMethods) because then we do not fingerprint.

Web Workers are separate JavaScript execution contexts. Per HTML spec they do not inherit parent page's import map. There is an issue with proposal of fixing it: whatwg/html#8173. but without much activity.

Description

dotnet-web-worker-client.js - Uses import.meta.resolve() on the main thread to resolve _framework/dotnet.js through the browser's native import map resolution.
The resolved (fingerprinted) URL is passed to the Worker via postMessage. No DOM parsing or JSON manipulation needed.

dotnet-web-worker.js - Replaces static import { dotnet } from '../../_framework/dotnet.js' with message-driven initialization using dynamic import() with the resolved URL.

import.meta.resolve() is supported in Chrome 105+, Firefox 106+, Safari 16.4+.

This PR is adding tests that run published app.

Fixes #65820

@ilonatommy ilonatommy added this to the .NET 11 Planning milestone Mar 20, 2026
@ilonatommy ilonatommy self-assigned this Mar 20, 2026
Copilot AI review requested due to automatic review settings March 20, 2026 12:13
@ilonatommy ilonatommy added the area-blazor Includes: Blazor, Razor Components label Mar 20, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the WebWorker project template so it works in published Blazor WebAssembly apps where framework JS files are fingerprinted and resolved via the page’s import map (which workers don’t inherit).

Changes:

  • Switch worker startup from a static module import to an init message that provides the resolved dotnet.js URL, then dynamically import() it in the worker.
  • Add client-side import map parsing to resolve the fingerprinted dotnet.js URL and send it to the worker during creation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/ProjectTemplates/Web.ProjectTemplates/content/WebWorker-CSharp/wwwroot/dotnet-web-worker.js Defers .NET runtime loading until an init message provides the correct dotnet.js URL; uses dynamic import.
src/ProjectTemplates/Web.ProjectTemplates/content/WebWorker-CSharp/wwwroot/dotnet-web-worker-client.js Resolves dotnet.js via the document import map and posts it to the worker as part of initialization.

@ilonatommy ilonatommy force-pushed the ilonatommy/fix-65820 branch from d81bf17 to c4a84c1 Compare March 20, 2026 12:37
@ilonatommy ilonatommy marked this pull request as draft March 20, 2026 13:58
@ilonatommy ilonatommy enabled auto-merge (squash) March 23, 2026 13:38
ilonatommy added a commit that referenced this pull request Mar 23, 2026
Re-quarantine RazorViews_AreUpdatedOnChange and RazorPages_AreUpdatedOnChange
in RazorBuildTest. These were unquarantined in #65864 but are still flaking
across unrelated PRs (e.g. #65885).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
wtgodbe added a commit that referenced this pull request Mar 23, 2026
Re-quarantine RazorViews_AreUpdatedOnChange and RazorPages_AreUpdatedOnChange
in RazorBuildTest. These were unquarantined in #65864 but are still flaking
across unrelated PRs (e.g. #65885).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: William Godbe <wigodbe@microsoft.com>
@ilonatommy
Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@ilonatommy ilonatommy merged commit d14f00b into main Mar 24, 2026
25 checks passed
@ilonatommy ilonatommy deleted the ilonatommy/fix-65820 branch March 24, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components

Projects

None yet

5 participants