dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.49k stars 10.04k forks source link

Static Web Assets with Finger Printing in Razor Class Libraries not supported? #58976

Open Tornhoof opened 1 day ago

Tornhoof commented 1 day ago

Is there an existing issue for this?

Describe the bug

Having a Razor Class Library with the new Static Web Assets with Fingerprinting enabled does not load the bundled and fingerprinted main asset ProjectName.bundle..scp.css, instead it tries to load the ProjectName.styles.css which does not exist. (404) If I use MapStaticAssets with the endpoints.json, the status code is 200, but the result is length 0.

Background I have multiple blazor WebApp as Razor Class libraries distributed as nuget packages, which are reused multiple times in different main solutions (which are not blazor apps,but usually simple web api apps with rest apis). In .NET 8 times it was already not possible to use "ProjectName.styles.css" logic, but I had to manually reference "ProjectName.bundle.scp.css" and other bundled css (like for quickgrid).

But now with fingerprinting, the bundle has no fixed name anymore. Fingerprinting can be disabled via the rather undocumented csproj setting "StaticWebAssetFingerprintingEnabled" (thanks to @ViRuSTriNiTy who told me that in Discussions https://github.com/dotnet/aspnetcore/discussions/58910), but that does not disable fingerprinting with externally packaged nugets like Quickgrid.

Expected Behavior

The assets are properly loaded, even if the hosting application is not a blazor app, but the Blazor App is packaged as a nuget package.

There is an api/way to reference assets with fingerprinting in razor class libraries or it is properly documented that you still need to reference the un-fingerprinted one, because the endpoint for it exists.

Steps To Reproduce

  1. Clone https://github.com/Tornhoof/BlazorClassLibRepro
  2. Run in Visual Studio, entrypoint is the Runner.csproj
  3. It should open a browser for https://localhost:7003/app
  4. The assets are not properly loaded

Notes:

Exceptions (if any)

No response

.NET Version

9.0.100

Anything else?

No response

ViRuSTriNiTy commented 1 day ago

Same here, except that fingerprinting is actually doing its thing but a wrong fingerprint is used.

javiercn commented 1 day ago

@ViRuSTriNiTy can you file a separate issue and provide a repro?

javiercn commented 1 day ago

@Tornhoof thanks for contacting us.

I have multiple blazor WebApp as Razor Class libraries distributed as nuget packages

This is not a supported scenario.

But now with fingerprinting, the bundle has no fixed name anymore. Fingerprinting can be disabled via the rather undocumented csproj setting "StaticWebAssetFingerprintingEnabled" (thanks to @ViRuSTriNiTy who told me that in Discussions https://github.com/dotnet/aspnetcore/discussions/58910), but that does not disable fingerprinting with externally packaged nugets like Quickgrid.

The .bundle.scp.css files have always been an implementation detail, not something that your app should directly reference or depend on.

With all that out of the way, have you used the `Assets["path"] syntax in your app? That will translate the non-fingerprinted name to the fingerprinted name (same thing as what happens in the regular templates).

Tornhoof commented 1 day ago

This is not a supported scenario.

Can you please elaborate, is bundling/css isolation/Static webassets not supported or are Blazor Web Apps from a different route than wwwroot not supported, i.e. where wwwroot is owned by an SPA and blazor is /myblazorapp.

The .bundle.scp.css files have always been an implementation detail, not something that your app should directly reference or depend on.

You might want to make that very clear here: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-9.0

With all that out of the way

Yes that's the state of the repro, depending on MapStaticAssets(manifest), if I use the manifest of the rcl there It will resolve the fingerprinted files, but they are length 0. Without the manifest they are not resolved, which makes sense if the whole thing only works with blazor as the entry point. I expect that using a manifest breaks after publishing anyway.

Looks like I need to get rid of bundling/css isolation etc and put everything back into app.css directly.