aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

AppendVersion FileVersionProvider querystring alternatives #2666

Closed benmccallum closed 9 years ago

benmccallum commented 9 years ago

Hey guys,

Just poking around the source code and really liking how TagHelpers (in the appropriate scenarios) have an asp-append-version attribute that will render a hash of the file contents to the end of the URL path like: myjsfile.js?v=8620562345.

Unfortunately, in the past I've had bad experiences with using querystrings for cache-busting.

Right now, I typically use a fingerprinting technique that changes the file path like so: myjsfile.ver-8620562345.js, matching a Google recommendation here. Of course the file doesn't really exist at that path, so I pair it with a <rewrite><rule> so it will correctly map to the real physical file.

Is something similar achievable out-of-the-box in mvc6? That'd be my preference, but not sure how to easily/cleanly plug-in the URL rewriting.

Another, less preferred solution, would be to, support a provider model with DI (given the class is named FileVersionProvider). That way a NuGet package could swap in the new provider and add the custom rewrite rule easily, though I'm not sure what rewrite config looks like in aspnet5.

Cheers, Ben

dpaquette commented 9 years ago

This is a good idea.

I thought I would point out a different approach as well. You could use a gulp task like gulp-rev (https://github.com/sindresorhus/gulp-rev) to append version numbers and output files to a wwwroot/dist folder as part of your gulp build.

Taking CSS files as an example, your css files would be output to wwwroot/dist/css and would have the format site-d41d8cd98f.css. To include the CSS you could then use the glob href:

<link rel="stylesheet" asp-href-include="dist/css/site-*.css">

So, your gulp task would automatically create new files with the version embedded in the name whenever the contents change and the link / script tag helpers would automatically pick up the new versions based on the pattern specified.

Regardless, I think it would be nice to have a FileVersionProvider that supports this too. The problem is making the URL rewriting work automatically. That part is a big of a pain but might be possible through some sort of static file middleware?

danroth27 commented 9 years ago

@madskristensen @DamianEdwards The gulp-rev approach sounds very compelling - should we just write up a doc on http://docs.asp.net that shows people how to do this?

benmccallum commented 9 years ago

I wasn't aware of the glob href, apologies, that is pretty cool! I use grunt/gulp to build assets so that approach would work for me. Thanks for bringing it to my attention. Documenting it and the grunt alternative as the recommended/standard asp.net5 approach makes sense.

I just ponder the value of the FileVersionProvider. I feel like it could be abused, or just be a go-to for people, that then causes them or following devs strife later. ie. When a client runs PageSpeed :p

danroth27 commented 9 years ago

aspnet/docs#308