-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Passing options to UseStaticFiles breaks Blazor Server's static file service #19578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Does it work if you add a second instance of UseStaticFiles with options rather than modifying the existing one? |
@Tratcher It does! I am not sure where I currently am on the amazed <-> appalled spectrum... |
😁 UseStaticFiles is designed to support multiple instances if you pass in the options directly. UseStaticFiles() with no parameters pulls the options from DI which is likely where Blazor is tweaking the settings. |
@Tratcher Thanks - that does make sense, though I didn't expect it at all, and it feels like a bit of a gotcha. Would it perhaps be possible for Blazor to detect that it has never been given the chance to run its static file config code and explode somehow early in the run? Or, rather than co-opting (hijacking) the UseStaticFiles to serve-up the manifest resources, could there just be a separate bit of middleware: |
Yeah, blazor should be adding their own static file handler like UseBlazorStaticResources rather than messing with the default. |
We've moved this issue to the Backlog milestone. This means that it is not going to happen for the coming release. We will reassess the backlog following the current release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. |
I just tried adding another call to app.UseStaticFiles();
FileExtensionContentTypeProvider provider = new FileExtensionContentTypeProvider();
provider.Mappings[".webmanifest"] = "application/manifest+json";
app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider = provider
}); Not keen on this, but at least it works! |
I've just ran in to this which is very confusing behaviour. I can confirm that @benm-eras workaround is working as expected. |
I've encountered the same problem. The solution also comes in this other message which proposes two alternatives: The first is the ideal solution. The second is a hack that I would not recommend. Example: // ConfigureServices method
services.Configure<StaticFileOptions>(options => {
FileExtensionContentTypeProvider contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings[".gpx"] = "application/gpx+xml";
options.ContentTypeProvider = contentTypeProvider;
});
// Configure method
app.UseStaticFiles(); |
I just wanted to add static asset caching and stumbled upon this problem. |
What a phenomenal time-sink this bug turned out to be for me. Glad there is a suggested workaround, but definitely wish this had been more obvious from the beginning. |
Thanks for contacting us. We're moving this issue to the |
I've ran into this issue today when I was trying to set the response HTTP headers for static files (.css, .js) that I'm serving as part of my Blazor website. I've followed the documentation https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0#set-http-response-headers but it took me a couple of hours before I realized what the issue was and stumbled upon this bug report. Adding multiple calls to app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
// custom response cache logic
}
});
app.UseStaticFiles(); |
Thanks for contacting us. We're moving this issue to the |
You guys should fix this, because it's a ridiculous bug and Blazor should never have broken the defaults like it did. As @Tratcher observed two and a half years ago. In terms of trying to encourage people to adopt MS tech, I think fixing these sort of festering sores of massive wasted-time for professional developers would give you far more bang-for-buck than all the "write a web app in one line of code" stuff which seems to get so much effort. |
I agree with Will. More effort has gone into punting this over and over
than there would have been exhausted in just fixing it. You make a mess,
you clean it up. Why is this so hard to understand?
…On Tue, Sep 20, 2022 at 2:22 AM Will Dean ***@***.***> wrote:
You guys should fix this, because it's a ridiculous bug and Blazor should
never have broken the defaults like it did. As @Tratcher
<https://github.com/Tratcher> observed two and a half years ago.
In terms of trying to encourage people to adopt MS tech, I think fixing
these sort of festering sores of massive wasted-time for professional
developers would give you far more bang-for-buck than all the "write a web
app in one line of code" stuff which seems to get so much effort.
—
Reply to this email directly, view it on GitHub
<#19578 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADBFFKF7ZAAS2H3ZY2FN643V7FQ4BANCNFSM4LBRGV2Q>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Note that this issue can be resolved by using the options pattern in ConfigureServices:
and then call UseStaticFiles() in the standard way (without any parameters) in Configure. |
Describe the bug
If you pass any kind of
StaticFileOptions
object toapp.UseStaticFiles()
, then Blazor Server will no longer be able to serveblazor.server.js
, breaking Blazor.To Reproduce
app.UseStaticFiles()
call in Startup.Configure to readapp.UseStaticFiles(new StaticFileOptions())
What appears to be happening is that if you don't pass any configuration to
UseStaticFiles
, then the framework correctly callsConfigureStaticFilesOptions.PostConfigure
after the pipeline is built. This configures the static file middleware to serve the Blazor JS from manifest resources.As soon as there's any configuration passed to
UseStaticFiles
, thenConfigureStaticFilesOptions
is no longer called. It doesn't matter what specific properties are in theStaticFileOptions
object - it's just the presence of the object which causes the problem.I will debug this further if it's helpful, but I suspect someone more familiar with the
IPostConfigureOptions
mechanism will know what's going on instantly.Maybe there's some work-around, but if so then it would be helpful if it could be somehow more obvious, because "I added a static file extension and Blazor stopped working" is somewhat surprising.
I think this may be same issue as #18222, which was perhaps rather confusingly titled and ended up being closed, but I think there is a real issue here.
Further technical details
The text was updated successfully, but these errors were encountered: