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

Created on 8 Dec 2017  路  4Comments  路  Source: aspnet/JavaScriptServices

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) }; } });

Most helpful comment

Good idea - we'll implement this. Thanks!

All 4 comments

Good idea - we'll implement this. Thanks!

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 https://github.com/angular/universal/issues/811 and suggest if anything could be done at aspnet/JavaScriptServices level?

@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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Sampath-Lokuge picture Sampath-Lokuge  路  4Comments

ZeekoZhu picture ZeekoZhu  路  3Comments

justinyoo picture justinyoo  路  3Comments

dantheman999301 picture dantheman999301  路  4Comments

raberana picture raberana  路  3Comments