adams85 / bundling

A library for optimizing and bundling web assets of ASP.NET Core applications.
MIT License
44 stars 7 forks source link

asp-append-version tag doesn't seem to work with bundled script #18

Closed ericraider33 closed 2 years ago

ericraider33 commented 2 years ago

I would like to add a version number to the URL of my bundled scripts. In my project, I'm using the asp-append-version tag, which calculates a hash and appends to the script. For all my non-bundled scripts, this tag works fine. However, when I use with a bundled script, nothing is added to the URL. See content below.

Is there a work around or something built into the bundler for append a version? I want to make sure that users' browsers don't use cached bundles when a new, updated version is available.

Source: `

`

Output: `

`
ericraider33 commented 2 years ago

As a footnote, when I changed from run-time bundling to design-time bundling, everything works fine. So for now, I'll just roll with the design-time stuff.

adams85 commented 2 years ago

However, when I use with a bundled script, nothing is added to the URL. See content below.

asp-append-version is a base ASP.NET Core tag helper attribute, it won't work with bundles produced by this library.

Is there a work around or something built into the bundler for append a version? I want to make sure that users' browsers don't use cached bundles when a new, updated version is available.

Bundle versioning and cache busting is built into the library and supported out of the box. As a matter of fact, cache busting is automatically enabled in Production environments by default. You can however override this behavior using the EnableCacheBusting method. E.g. if you want to enable it regardless of the runtime environment, just make an EnableCacheBusting(true) call on the builder returned by AddBundling.

It's also worth mentioning that you can also control

By default hash-based versions are used and appended to the path part.

One more thing to note: instead of asp-append-version you can use the bundling-add-version attribute to override the global versioning settings for individual bundles.

As a footnote, when I changed from run-time bundling to design-time bundling, everything works fine. So for now, I'll just roll with the design-time stuff.

All the above also goes for design-time bundling (in case you configure the bundle services properly and call the InitializeBundling method as shown in the quick start template project).

In the case of design-time bundling asp-append-version accidentally works, however I recommend choosing the library's "idiomatic" way of versioning because that provides more control and lets you switch between design-time and run-time bundling easily.

ericraider33 commented 2 years ago

Excellent reply. Thanks for the help...