Comments (4)
Before I used this lib I've always done that via the asp-version tag.
asp-append-version="true"
is a framework thing. It has no effect in the case of bundles because the lib handles cache busting/versioning on its own. You should just remove these attributes.
I'm already using the UseDefaults but I'm not seeing the hash or timestamp versioning in my HTML.
A few notes on your configuration:
.UseHashVersioning()
is unnecessary as it's already done by.UseDefaults(_env)
as I mentioned in my previous comment.- Additionally, you don't need to sprinkle
.EnableMinification()
everywhere. It's sufficient to do that on top level byservice.AddBundling().EnableMinification()
. Usually you don't even need that call because.UseDefaults(_env)
already enables minification for production environments (see the implementation).
Anyway, your configuration seems ok regarding cache busting. I'm puzzled as to why you get that output. It should be like this:
<script src="/static/site.min.vWJeOEIj1qlekEAapOf6Fbk50Z7aq-1bbxjs24O65Uxc.js"></script>
It looks as if the lib's custom tag helper doesn't get executed. Does your _ViewImports.cshtml
contain the following lines?
@using Karambolo.AspNetCore.Bundling.ViewHelpers
@addTagHelper *, Karambolo.AspNetCore.Bundling
from bundling.
Ah, my bad!
I forgot the _ViewImports.cshtml
part!
It works perfectly now including caching and refreshing upon updates!
Again, thanks for your help and time!
from bundling.
First of all, thanks for the kind words. I'm glad you have found the lib useful. (I'm afraid though there's still a long way to go until perfection. :)
EnableCacheHeader
simply causes a Cache-Control: max-age=...
HTTP header to be added to the response when a bundle is requested by the client browser. (You can see the actual implementation here.) This Cache-Control
configuration instructs the browser to store the bundle in its own cache and serve it from there until the cached bundle expires (expiration time is determined by max-age
). That is, the browser won't contact the server until expiration happens. At the same time, the server cannot invalidate the client browser's cache unless contacted.
I can see two possible ways out of this trap:
-
Maybe there's another
Cache-Control
configuration which suits your requirements. (You can read up on controlling client cache e.g. here.) If so, you just need to remove theEnableCacheHeader
call and pass aBundlingOptions
instance toapp.UseBundling
. InBundlingOptions.OnPrepareResponse
you can specify a delegate by means of which you can add arbitrary HTTP headers (including a customCache-Control
header) to the response. -
More likely, you need cache busting. As a matter of fact, if you use the default configuration (
UseDefaults
), cache busting must be already enabled. Otherwise, you can enable it byUseHashVersioning
orUseTimestampVersioning
. The former is the default and it's based on bundle content hash. The latter is based on bundle creation time. If the built-in ones don't work out for some reason, it's possible to implement a custom versioning strategy:- Implement the
IBundleVersionProvider
interface. If you're unsure, just check out the built-in implementations:HashBundleVersionProvider
orTimestampBundleVersionProvider
- Register your custom version provider by adding it the service collection in
Startup.ConfigureServices
AFTER the call to AddBundling:services.Replace(ServiceDescriptor.Singleton<IBundleVersionProvider, MyCustomBundleVersionProvider>());
- Implement the
I hope this helps. Just let me know if you need further information.
from bundling.
Thanks for your time and explanation.
I've read the part about cache-busting. I understand that I need to find a way to bust the cache on the user machine. Before I used this lib I've always done that via the asp-version
tag.
The second solution sounded more logical to me. I like to give my users a fast site with caching.
I'm already using the UseDefaults
but I'm not seeing the hash or timestamp versioning in my HTML.
services.AddBundling()
.UseDefaults(_env)
.UseWebMarkupMin()
.UseHashVersioning()
.EnableCacheHeader(TimeSpan.FromDays(730))
.EnableMinification();`
app.UseBundling(
new BundlingOptions
{
RequestPath = "/static"
},
bundler =>
{
bundler.AddCss("/site.min.css")
.Include("/lib/bootstrap/dist/css/bootstrap.css")
.Include("/css/site.css")
.EnableMinification();
bundler.AddJs("/site.min.js")
.Include("/lib/bootstrap/dist/js/bootstrap.js")
.Include("/js/site.js")
.EnableMinification();
}
);
<script asp-append-version="true" src="~/static/site.min.js"></script>
Output:
<script src=/static/site.min.js></script>
Am I doing the cache-busting wrong?
from bundling.
Related Issues (16)
- How to apply transforms to a Bundle like we used to do in Asp.Net MVC 4? HOT 2
- Cannot access node_modules folder HOT 1
- Vuejs HOT 5
- .Net Core 3 issues HOT 2
- Does it work with .NET Framework 4.7? HOT 5
- Using CDN HOT 1
- Tree shaking in EcmaScript HOT 2
- EcmaScript - NullReferenceException when variable is not declared HOT 1
- Render individual includes HOT 8
- Add option for generating source maps
- Issue when script file has been modified outside of Visual studio HOT 5
- Negative number invalid for list HOT 1
- JS: Multiple mobile versions give syntax errors after bundeling. HOT 5
- asp-append-version tag doesn't seem to work with bundled script HOT 3
- Are there plans to update bundling tools to target .NET 7? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bundling.