Repository navigation
Browser: Migrate to static web assets for assets bundling, integrate with OverrideHtmlAssetPlaceholders - #22266
Conversation
…built-in assets packaging
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>
|
You can test this PR using the following package version. |
This comment was marked as outdated.
This comment was marked as outdated.
|
Apparently .NET 11 has a completely different internal format for NuGet package assets...again. - dotnet/sdk#53187 |
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
|
@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 In my testing I noticed full effect also requires |
|
And again, with .NET 11 as a minimum target I would drop latter commits, and only rely on |
NuGet packagesThe packages for this pull request have been published (workflow run):
|
|
cc @maraf |
MrJul
left a comment
There was a problem hiding this comment.
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!
…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>
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.jsfile 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.jsandstorage.jsare fingerprinted, so they can be cached immutably. This requires the app to opt in withOverrideHtmlAssetPlaceholdersand 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.jsand 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.Sdkand ships its JS as plain files.build/Avalonia.Browser.targetsthen defines the static web assets in the consuming app's build.This is what
Microsoft.NET.Sdk.BlazorWebAssemblydoes 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 URLBreaking changes
_framework/avalonia.jsto_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./avalonia-sw.jsand is now scoped to the app root. This only affects apps that setRegisterAvaloniaServiceWorker, which is opt-in and marked unstable.Fingerprinting is opt-in and does not change behavior for existing apps.