aspnet / StaticFiles

[Archived] Middleware for handling requests for file system resources including files and directories. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
114 stars 72 forks source link

OnPrepareResponse: Is this behavior a bug or by-design? #170

Closed guardrex closed 7 years ago

guardrex commented 7 years ago

I was just taking a look at OnPrepareResponse trying setup a Cache-Control header with an immutable directive only for a group of static assets in a special folder, ImmutableResourcesFolder, and on a special path, /immutable ...

app.UseStaticFiles(); // for wwwroot assets

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), 
            @"ImmutableResourcesFolder")),
    RequestPath = new PathString("/immutable"),
    OnPrepareResponse = ctx =>
    {
        ctx.Context.Response.Headers.Append("Cache-Control", "max-age=36500000,immutable");
    }
});

... but all static files, including the ones in wwwroot, are getting the Cache-Control header set from the OnPrepareResponse. For example ...

capture

I expected that only the assets requested on my /immutable path would get my special Cache-Control header. Is this behavior a bug or by-design (and I'm setting it up incorrectly)?

Note: There's an issue now to document OnPrepareResponse at https://github.com/aspnet/Docs/issues/2625.

My test app for this is a netcoreapp2.0/1.2.0-* nightly packages app. Environment ...

.NET Command Line Tools (1.0.0-rc4-004706)

Product Information:
 Version:            1.0.0-rc4-004706
 Commit SHA-1 hash:  e38bc4950c

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\1.0.0-rc4-004706
Tratcher commented 7 years ago

That shouldn't be happening. @mikaelm12 can you repro this?

mikaelm12 commented 7 years ago

I'll take a look

JunTaoLuo commented 7 years ago

Took a quick look and I cannot reproduce the issue. Seems like the immutable cache headers are only added when the second static files middleware is serving the response. I'm using an older dotnet cli though but I don't think that should be a problem. @GuardRex is there anything else in your Configure?

Tratcher commented 7 years ago

A full repro app would help

guardrex commented 7 years ago

I'll try again. I see disabled code for ...

/*
app.Use(async (context, next) =>
{
    context.Response.Headers.Add("Cache-Control", "max-age=36500000,immutable");
    await next.Invoke();
});
*/

... as long as it was commented at the time I tested, it's fine. Let me try again and make sure that was the case.

guardrex commented 7 years ago

My bad. I screwed the pooch :dog2: on this one. I was doing a lot of messing around that day with this, and I must have run the test app with that code enabled.

Sorry guys.