Skip to content

Browser: Migrate to static web assets for assets bundling, integrate with OverrideHtmlAssetPlaceholders - #22266

Merged
MrJul merged 14 commits into
mainfrom
fixes/browser-assets-use-sdk
Sep 22, 2026
Merged

MrJul merged 14 commits into
mainfrom
fixes/browser-assets-use-sdk

Conversation

@maxkatz6

@maxkatz6 maxkatz6 commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

What does the pull request do?

Publishes Avalonia.Browser assets (avalonia.js, storage.js, and the service worker) through the SDK static web assets pipeline, so they are bundled in NuGet packages, fingerprinted and compressed, and work the same via PackageReference and ProjectReference.

What is the current behavior?

Before this PR we used hacks with manually generated StaticWebAssets.targets file.
It worked, but only from NuGet packages, and without any fingerprinting.

Avalonia was publishing its assets under _framework/ directory, which is reserved for .NET SDK files.

Additionally, sw.js file was incorrectly resolved (it was only used as an optional polyfill for file pickers on Firefox).

What is the updated/expected behavior with this PR?

Avalonia.Browser no longer ships hardcoded StaticWebAssets.props. Assets are published under
_content/Avalonia.Browser/, as expected by the SDK, and are compressed.

avalonia.js and storage.js are fingerprinted, so they can be cached immutably. This requires the app to opt in with OverrideHtmlAssetPlaceholders and an <script type="importmap"></script> placeholder in index.html.
Apps that do not opt in keep the plain names and keep working.

The service worker is published to the app root as avalonia-sw.js and resolved from the document root, not relatively to the current dotnet module.

How was the solution implemented (if it's not obvious)?

Ideally this PR would be implemented with Microsoft.NET.Sdk.StaticWebAssets, and twice less code. Just include static assets and call it a day.

It worked on .NET 11, but was broken in .NET 10 for packed apps - fingerprinting didn't work.
.NET 11 also has a completely different nupkg output for WASM, incompatible with .NET 10 apps dotnet/sdk#53187.

So, Avalonia.Browser stays on Microsoft.NET.Sdk and ships its JS as plain files.
build/Avalonia.Browser.targets then defines the static web assets in the consuming app's build.
This is what Microsoft.NET.Sdk.BlazorWebAssembly does too.

Since Avalonia uses TS files, with an additional build.js task to compile them into JS, some msbuild magic is still needed to pick up wwwroot after it is created during the build.

The service worker had to move to the app root. A worker is scoped to its own directory, and the save picker polyfill looks the registration up with getRegistration(), which matches against the document URL

Breaking changes

  1. Asset URLs move from _framework/avalonia.js to _content/Avalonia.Browser/avalonia.js. This is a breaking change only for apps that were directly accessing these files. Any other Avalonia app created from our templates is expected continue to work without changes. I tested local builds with current templates.
  2. The service worker moves to /avalonia-sw.js and is now scoped to the app root. This only affects apps that set RegisterAvaloniaServiceWorker, which is opt-in and marked unstable.

Fingerprinting is opt-in and does not change behavior for existing apps.

maxkatz6 and others added 6 commits September 20, 2026 15:46
The browser TFM now layers Microsoft.NET.Sdk.WebAssembly on top of Microsoft.NET.Sdk,
so Avalonia.Browser's javascript resolves from _content/Avalonia.Browser/.

The static web assets SDK only discovers wwwroot at the project root and offers no
property to relocate it, so define the assets under $(BrowserProjectFolder)wwwroot
directly. WasmExtraFilesToDeploy is kept for the legacy WasmApp model and is now
guarded so the two paths cannot both run.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0070077-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6
maxkatz6 marked this pull request as draft September 20, 2026 16:12
@maxkatz6

This comment was marked as outdated.

@maxkatz6

Copy link
Copy Markdown
Member Author

Apparently .NET 11 has a completely different internal format for NuGet package assets...again. - dotnet/sdk#53187

@maxkatz6 maxkatz6 changed the title Browser: Migrate to Microsoft.NET.Sdk.StaticWebAssets for assets bundling Browser: Migrate to static web assets for assets bundling Sep 22, 2026
maxkatz6 and others added 6 commits September 22, 2026 10:19
Avalonia.Browser no longer packs its javascript as static web assets.
build/Avalonia.Browser.targets defines them in the consuming app instead,
the same way Microsoft.NET.Sdk.BlazorWebAssembly handles blazor.webassembly.js.

A hard fingerprint renames the file, which plain static hosting needs,
but it does not survive dotnet pack on the .NET 10 SDK:
the packed props record the resolved name and drop the marker,
so no import map entry is emitted and package consumers request a file that was never renamed.
Evaluating the expression in the app keeps it intact.
.NET 11 fixes pack, but only for consumers on the .NET 11 SDK.

avalonia.js and storage.js are fingerprinted, sw.js and the source maps are not:
those load by URL through serviceWorker.register and sourceMappingURL,
which the import map does not rewrite.

The project drops back to Microsoft.NET.Sdk,
so it no longer emits staticwebassets.runtime.json and .endpoints.json next to the assembly.
A project reference carried those into the app output and made the wasm dev server
serve the app from the wrong manifest, 404ing the icu .dat and .pdb files.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1TywZ5LUqgagfuzPcxjf5
A renamed file resolves only through the import map,
which the SDK emits only when OverrideHtmlAssetPlaceholders is true.
Apps that do not opt in got renamed files and an empty import map placeholder,
so JSHost.ImportAsync requested a file that no longer existed.
$(StaticWebAssetsFingerprintContent)=false is now honoured too.

Prefer wwwroot/ over staticwebassets/ when locating the javascript.
esbuild wrote to staticwebassets/ before this branch and both are gitignored,
so a leftover directory would otherwise shadow the current output forever.

Skip the assets, and AVA3002, in design time builds.
Those do not build the project reference, so wwwroot may legitimately be absent.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1TywZ5LUqgagfuzPcxjf5
A service worker is scoped to its own directory,
and the save picker polyfill looks the registration up with getRegistration(),
which matches against the document URL.
From _content/Avalonia.Browser/ the lookup found nothing,
so every save fell back to buffering the whole file into a Blob
instead of streaming through the worker.

Rename the entry point to avalonia-sw.ts and place the output at the app root,
which is the only location whose default scope covers the document.
The polyfill builds its URLs from sw.scope, so nothing else has to change.

Registration was already broken before this branch for a different reason:
the path resolved to ./sw.js while the file shipped under _framework/,
so register() hit a 404 that nothing observed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1TywZ5LUqgagfuzPcxjf5
@maxkatz6
maxkatz6 marked this pull request as ready for review September 22, 2026 03:21
@maxkatz6

maxkatz6 commented Sep 22, 2026 •

Copy link
Copy Markdown
Member Author

@MrJul with some help of Claude, and cross-check of what Blazor does, I think this PR is ready.

Currently existing projects, like ones created from the 12.1 templates, are expected to work just fine. Without fingerprinting on avalonia files, which results in force-disabled caching.

To get benefits from this PR, user needs to opt-in OverrideHtmlAssetPlaceholders, like I made in AvaloniaUI/Avalonia.Templates#359. With both, all Avalonia assets will have randomized suffix on the filename, and caching enabled.

In my testing I noticed full effect also requires dotnet publish + serve, not just dotnet run. Plain dotnet run was force disabling caching for all JS files, I guess for debugging purposes.

@maxkatz6 maxkatz6 changed the title Browser: Migrate to static web assets for assets bundling Browser: Migrate to static web assets for assets bundling, integrate with OverrideHtmlAssetPlaceholders Sep 22, 2026
@maxkatz6

Copy link
Copy Markdown
Member Author

And again, with .NET 11 as a minimum target I would drop latter commits, and only rely on Microsoft.NET.Sdk.StaticWebAssets to include all files and endpoints.
But we are not there yet.

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

NuGet packages

The packages for this pull request have been published (workflow run):

@pavelsavara

Copy link
Copy Markdown

cc @maraf

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's a shame that the migration to Microsoft.NET.Sdk.StaticWebAssets didn't work on .NET 10, but that's how it is. Next time :)

LGTM!

@MrJul
MrJul added this pull request to the merge queue Sep 22, 2026
Merged via the queue into main with commit 6564835 Sep 22, 2026
11 checks passed
@MrJul
MrJul deleted the fixes/browser-assets-use-sdk branch September 22, 2026 18:47
grokys pushed a commit to Evan260/Avalonia that referenced this pull request Oct 7, 2026
…with OverrideHtmlAssetPlaceholders (AvaloniaUI#22266)

* Use `Microsoft.NET.Sdk.StaticWebAssets` in Avalonia.Browser, rely on built-in assets packaging

* Update ControlCatalog.Browser to use OverrideHtmlAssetPlaceholders + fingerprint

* Fix default BrowserPlatformOptions.FrameworkAssetPathResolver behavior

* Remove outdated ShouldIncludeNativeSkiaSharp/ShouldIncludeNativeHarfBuzzSharp

* Update BuildTests.Browser

* Support static web assets in single project browser apps

The browser TFM now layers Microsoft.NET.Sdk.WebAssembly on top of Microsoft.NET.Sdk,
so Avalonia.Browser's javascript resolves from _content/Avalonia.Browser/.

The static web assets SDK only discovers wwwroot at the project root and offers no
property to relocate it, so define the assets under $(BrowserProjectFolder)wwwroot
directly. WasmExtraFilesToDeploy is kept for the legacy WasmApp model and is now
guarded so the two paths cannot both run.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Restore WasmExtraFilesToDeploy fallback

* Remove unused runtimeconfig.template.json

* Fingerprint Avalonia.Browser javascript from the app build

Avalonia.Browser no longer packs its javascript as static web assets.
build/Avalonia.Browser.targets defines them in the consuming app instead,
the same way Microsoft.NET.Sdk.BlazorWebAssembly handles blazor.webassembly.js.

A hard fingerprint renames the file, which plain static hosting needs,
but it does not survive dotnet pack on the .NET 10 SDK:
the packed props record the resolved name and drop the marker,
so no import map entry is emitted and package consumers request a file that was never renamed.
Evaluating the expression in the app keeps it intact.
.NET 11 fixes pack, but only for consumers on the .NET 11 SDK.

avalonia.js and storage.js are fingerprinted, sw.js and the source maps are not:
those load by URL through serviceWorker.register and sourceMappingURL,
which the import map does not rewrite.

The project drops back to Microsoft.NET.Sdk,
so it no longer emits staticwebassets.runtime.json and .endpoints.json next to the assembly.
A project reference carried those into the app output and made the wasm dev server
serve the app from the wrong manifest, 404ing the icu .dat and .pdb files.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1TywZ5LUqgagfuzPcxjf5

* Only fingerprint Avalonia javascript when an import map is generated

A renamed file resolves only through the import map,
which the SDK emits only when OverrideHtmlAssetPlaceholders is true.
Apps that do not opt in got renamed files and an empty import map placeholder,
so JSHost.ImportAsync requested a file that no longer existed.
$(StaticWebAssetsFingerprintContent)=false is now honoured too.

Prefer wwwroot/ over staticwebassets/ when locating the javascript.
esbuild wrote to staticwebassets/ before this branch and both are gitignored,
so a leftover directory would otherwise shadow the current output forever.

Skip the assets, and AVA3002, in design time builds.
Those do not build the project reference, so wwwroot may legitimately be absent.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1TywZ5LUqgagfuzPcxjf5

* Serve the Avalonia service worker from the app root

A service worker is scoped to its own directory,
and the save picker polyfill looks the registration up with getRegistration(),
which matches against the document URL.
From _content/Avalonia.Browser/ the lookup found nothing,
so every save fell back to buffering the whole file into a Blob
instead of streaming through the worker.

Rename the entry point to avalonia-sw.ts and place the output at the app root,
which is the only location whose default scope covers the document.
The polyfill builds its URLs from sw.scope, so nothing else has to change.

Registration was already broken before this branch for a different reason:
the path resolved to ./sw.js while the file shipped under _framework/,
so register() hit a 404 that nothing observed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V1TywZ5LUqgagfuzPcxjf5

* Remove unused modulepreload

* Fix duplicated _AvaloniaBrowserStaticWebAsset for AvaloniaSingleProject.targets

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants