BlazorStatic icon indicating copy to clipboard operation
BlazorStatic copied to clipboard

dotnet 9 Assets

Open kijanawoodard opened this issue 1 year ago • 4 comments

Has anyone, by chance, figured out how to use the dotnet static assets feature that compresses and fingerprints static files. <link rel="stylesheet" href="@Assets["app.css"]" />

Using @Assets works fine locally because aspnet is running. The html has a fingerprint injected into the filename: <link rel="stylesheet" href="app.4kywy7iv6g.css">

But only the original file gets copied to output from wwwroot.

Is there any way to get the fingerprinted file copied to output?

For the moment, I removed the @Assets calls.

kijanawoodard avatar Feb 16 '25 05:02 kijanawoodard

I see the issue. I will look in to that, but for now do you think it is possible to use the Assets somehow to add additional files. I mean - can we access the Assets in program.cs?

tesar-tech avatar Feb 17 '25 19:02 tesar-tech

I haven't seen a way to get a handle on the generated assets files.

My thinking is this is more of an aspnet issue in not providing the relevant hooks, but was hoping maybe you had already found a solution.

kijanawoodard avatar Feb 17 '25 20:02 kijanawoodard

The server part is necessary to "translate" fingerprinted assets (like app.xyzfingerprint.css) to physical files (app.css). So, when the browser requests the fingerprinted file, it receives the "normal" one. Thus, there is no physical file named app.xyzfingerprint.css.

Reference:
ASP.NET Core Blazor Static Files

An interesting read is the PR that introduces this functionality:
GitHub: dotnet/aspnetcore#56045

There is special handling of assets for Blazor WebAssembly. The <ImportMap> tag provides a dictionary mapping fingerprints to their corresponding physical assets on the client. Additionally, the script _framework/resource-collection.QbVKs.js is loaded when the Blazor WebAssembly part of the app is initialized.

This resource collection script is my hope for compatibility with BlazorStatic, but:

Blazor WebAssembly standalone doesn't work with the new assets. The likely reason is that it lacks the server component responsible for translating fingerprints into physical asset paths. But then I don't fully understand the resource collection script - It seems to me it should somehow replace the server capability with the js.

Standalone Blazor Wasm would also benefit from fingerprinting the assets. It could be done during publish.

...

Have you got any new discoveries on the topic?

tesar-tech avatar Mar 14 '25 08:03 tesar-tech

I haven't made any progress on this topic.

Just for context: My use case is Blazor Server with Blazor Static to produce a static site that I host on cloudflare pages.

kijanawoodard avatar Mar 15 '25 21:03 kijanawoodard