Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Response caching: add StaticFileOptions to app.UseSpaStaticFiles() method #1424

Closed
ridicoulous opened this issue Dec 8, 2017 · 4 comments
Closed

Comments

@ridicoulous
Copy link

ridicoulous commented Dec 8, 2017

Hello. How about to add StaticFileOptions to UseSpaStaticFiles() method in Angular CLI based template for response caching? May be, I did it wrong, but this approach does not working and I think, problem is in UseSpaStaticFiles method:
app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = (context) => { var headers = context.Context.Response.GetTypedHeaders(); headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue { Public = true, MaxAge = TimeSpan.FromDays(365) }; } });

@SteveSandersonMS
Copy link
Member

Good idea - we'll implement this. Thanks!

@naveedahmed1
Copy link

This would be awesome, but will it allow us to set cache rules per url pattern basis?

@SteveSandersonMS can you please also share, when we have SSR enabled, are the files (app js bundles) being read from disk for each request? or are these cached and read from memory for subsequent requests? If the files are being read from disk for each request, wouldn't it be better if we cache the files in memory for subsequent requests?

Can you please also take a look at angular/universal#811 and suggest if anything could be done at aspnet/JavaScriptServices level?

@asadsahi
Copy link

@SteveSandersonMS @naveedahmed1 I think it would be really beneficial to have some sort of mechanism which can cache SPA resources (i.e static files and any pre-rendered responses).

As I understand feature request that Steve has agreed to will cache only static resources by having ResponseCaching option in UseSpaStaticFiles middleware.

On the other hand page loads (i.e document resulted in pre-rendering for each requested URL in browser address bar) have got no mechanism of caching right now. Is there a similar option that can be added in UseSPA middleware which under the hood can leverage asp.net core's caching features.

In comparison we have the option of ResponseCaching attributes on Controller action methods or use tag helpers in razor views in other templates.

@SteveSandersonMS
Copy link
Member

Now implemented. You can now pass a StaticFileOptions to UseSpaStaticFiles (which affects serving bundles) and/or set one on the new SpaOptions DefaultPageStaticFileOptions property (which affects serving the initial default page HTML).

will it allow us to set cache rules per url pattern basis

Yes, see OnPrepareResponse in @ridicoulous's original post above.

can you please also share, when we have SSR enabled, are the files (app js bundles) being read from disk for each request

No, Node loads them and parses/compiles them in memory as a module only once. It doesn't have to read modules from disk on every request.

Can you please also take a look at angular/universal#811

That's internal to Angular Universal - ASP.NET isn't involved in how it processes requests at that level.

I think it would be really beneficial to have some sort of mechanism which can cache SPA resources (i.e static files and any pre-rendered responses).

ASP.NET Core middleware lets you implement caching on any request (both server-side and client-side caching). https://docs.microsoft.com/en-us/aspnet/core/performance/caching/middleware?tabs=aspnetcore2x

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants