Giter Club home page Giter Club logo

Comments (4)

adams85 avatar adams85 commented on July 23, 2024 1

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 by service.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.

iJoris avatar iJoris commented on July 23, 2024 1

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.

adams85 avatar adams85 commented on July 23, 2024

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:

  1. 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 the EnableCacheHeader call and pass a BundlingOptions instance to app.UseBundling. In BundlingOptions.OnPrepareResponse you can specify a delegate by means of which you can add arbitrary HTTP headers (including a custom Cache-Control header) to the response.

  2. 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 by UseHashVersioning or UseTimestampVersioning. 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 or TimestampBundleVersionProvider
    • 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>());

I hope this helps. Just let me know if you need further information.

from bundling.

iJoris avatar iJoris commented on July 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.