Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Note requirement to include UseStaticWebAssets in Staging environment #26751

Closed
2 tasks
KurtP20 opened this issue Aug 16, 2022 · 12 comments · Fixed by #26759
Closed
2 tasks

Note requirement to include UseStaticWebAssets in Staging environment #26751

KurtP20 opened this issue Aug 16, 2022 · 12 comments · Fixed by #26759
Assignees
Labels
Blazor doc-enhancement Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

@KurtP20
Copy link

KurtP20 commented Aug 16, 2022

[EDIT by guardrex to change the metadata]

I bumped into a nasty error when switching from Devlopment so Staging environment: In Staging, static web assests (e.g. site.css and other java scripts) are not served automatically. You can try this by making a new Blazor Server Side App, switch to Staging and you will see, that site.css is not loaded.

The fix seems to be to include UseStaticWebAssets. Unfortunately, it took me quite a while to figure out where to include this, so a code example would be useful. I work off the .net 5 template, so in Program.cs I added
Host.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStaticWebAssets(); webBuilder.UseStartup<Startup>(); })

I also did not find a description of the side effects when including UseStaticWebAssets in all environments and I don't know how to access the Environment in ConfigureWebHostDefaults.

  • I think it would be good to mention/highlight the need to include UseStaticWebAssets on this page in the documentation and also in the xml documentation of ConfigureWebHostDefaults, as it mentions that static assets are included, but does not mention that this is only in development environment.

  • Please also elaborate on the side effect of using UseStaticWebAssets in all environments in the api documentation page.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@dotnet-bot dotnet-bot added ⌚ Not Triaged Source - Docs.ms Docs Customer feedback via GitHub Issue labels Aug 16, 2022
@Rick-Anderson
Copy link
Contributor

@guardrex please review

@guardrex
Copy link
Collaborator

guardrex commented Aug 16, 2022

Hello @KurtP20 ... Interesting! Yes, I just found out that this is a by-design feature 😄. See ...

dotnet/aspnetcore#39596 (comment)

I'm going to update the metadata and work this guidance into the Blazor Static Files topic, probably with a cross-link in the Blazor Environments topic.

UPDATE: Ok, all set. I'll get to this soon. I should be able to get it covered sometime this week. Thanks for the report!

@guardrex
Copy link
Collaborator

guardrex commented Aug 16, 2022

I'm going to take a closer look when I get back to this, but try it this way when using Minimal APIs (6.0 or later) ...

if (builder.Environment.IsEnvironment("Staging"))
{
    StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration);
}

I think what you did for 5.0 is fine. Actually, I just saw your remark ...

I don't know how to access the Environment in ConfigureWebHostDefaults.

Still tho ... what you did seems right. I'll need to investigate StaticWebAssetsLoader for 5.0.

@KurtP20
Copy link
Author

KurtP20 commented Aug 17, 2022

Thanks for updating the documentation!

In the .net 5 template I don't see right away where to include your code. But now I better understand the "side effects" and the reason of having to use UseStaticWebAssets, and since I do want to serve static context in all environments, I do not need to access the environment any more. Thanks for your help.

@guardrex
Copy link
Collaborator

guardrex commented Aug 17, 2022

I found this in a discussion for 5.0 apps ...

.ConfigureAppConfiguration((ctx, cb) => {
    if (ctx.HostingEnvironment.IsStaging()) {
        StaticWebAssetsLoader.UseStaticWebAssets(ctx.HostingEnvironment, ctx.Configuration);
    }
})

I'm reading the full discussion at ...

dotnet/aspnetcore#38212

@guardrex
Copy link
Collaborator

guardrex commented Aug 17, 2022

Looks like this ...

builder.WebHost.UseStaticWebAssets();

... should work for 7.0 or later. Confirmed! ... That seems to work fine in 7.0.

It looks like the workaround in 6.0 is simply ...

builder.WebHost.UseWebRoot("wwwroot").UseStaticWebAssets();

Confirmed! ... That seems fine in 6.0.

Ah! The fix was backported to 6.0 in dotnet/aspnetcore#41182, so ...

builder.WebHost.UseStaticWebAssets();

... works in both 6.0 and 7.0.

When I get the PR put together, I'll ping Safia to look and confirm that I have the guidance correct.

@KurtP20
Copy link
Author

KurtP20 commented Aug 17, 2022

... and in 5.0 it is

Host.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.UseStaticWebAssets();    
    webBuilder.UseStartup<Startup>();
})

Your suggestion

.ConfigureAppConfiguration((hostContext, builder) => 
{                    
    StaticWebAssetsLoader.UseStaticWebAssets(hostContext.HostingEnvironment, hostContext.Configuration);
});

does not work for me as parameter 1 cannot be converted from IHostEnvironment to IWebHostEnvironment, but one could probably simly call StaticWebAssetsLoader.UseStaticWebAssets without parameters. The first version is preferred as the call to ConfigureWebHostDefaults is already in the template and ConfigureAppConfiguration is not.

@guardrex
Copy link
Collaborator

guardrex commented Aug 17, 2022

Well, it wasn't my suggestion 😄 ... it was in the discussion remarks. Yes, that's right about the param. I did note it in passing. One my personal problems here is that I just set up a brand new PC, and I didn't get it set up with 5.0 hoping that I wouldn't need to. I'd rarely use it, as we're heading into 7.0 at this point. I may fire up the old PC to see what's what.

I'll try to work up the PR today. Let me see what's what with the schedule today, but I think I can knock this out first thing 🤞.

@guardrex
Copy link
Collaborator

guardrex commented Aug 17, 2022

I didn't really copy it exactly. They actually have ...

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureAppConfiguration((ctx, cb) => {
    if (ctx.HostingEnvironment.IsStaging()) {
        StaticWebAssetsLoader.UseStaticWebAssets(ctx.HostingEnvironment, ctx.Configuration);
    }
});

... and they claim that that works in 5.0. I'm just going by the discussion given my current lack of 5.0 on this shiny new 💻.

@KurtP20
Copy link
Author

KurtP20 commented Aug 17, 2022

I would not want to install 5.0 on a new machine either. It's just so much work to change from the 5 to the 6 template where app configuration is much different. The solution with ConfigureWebHostDefaults works and looks similar to builder.WebHost.ConfigureAppConfiguration.

@guardrex
Copy link
Collaborator

I can keep the default.

It's just that I don't like cluttering my machine with hundreds of megs of (mostly) dead files 💀 ... 5.0, it's so 2021! 😆

@KurtP20
Copy link
Author

KurtP20 commented Aug 17, 2022

I found these related issues 13190 and 13864 via this post, maybe you want to leave a comment at those issues as well?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Blazor doc-enhancement Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants