supposedly, when file included in bundle modified, url key supposed updated, forcing client clear cache.
however, have found unreliable process - reason, url key not changed when there many changes underlying files.
so wanted use assembly version in query string whenever release occurred, clients clear cache updated recent version. here have far:
custom transform modify query variables:
public class versionbustertransform : ibundletransform { public void process(bundlecontext context, bundleresponse response) { string query = string.format("{0}.{1}.{2}.{3}", global.properties.version.major, global.properties.version.minor, global.properties.version.release, global.properties.version.build); array.foreach(response.files.toarray(), x => x.includedvirtualpath = string.concat(x.includedvirtualpath, "?v=", query)); } }
registering files:
bundletable.enableoptimizations = (global.properties.deployment.environment != deploymentenvironment.development); var stylelibrary = new stylebundle(configbundles.styles); stylelibrary.include("~/content/styles/libraries/core-{version}.css", new cssrewriteurltransform()); stylelibrary.include("~/content/styles/libraries/icomoon/style.css", new cssrewriteurltransform()); stylelibrary.include("~/content/styles/site.css", new cssrewriteurltransform()); stylelibrary.transforms.add(new versionbustertransform()); bundletable.bundles.add(stylelibrary); var scriptlibrary = new scriptbundle(configbundles.scripts); scriptlibrary.include("~/content/scripts/libraries/modernizr-{version}.js"); scriptlibrary.include("~/content/scripts/libraries/bootstrap-{version}.js"); scriptlibrary.include("~/content/scripts/framework/yeack.js"); scriptlibrary.transforms.add(new jsminify()); scriptlibrary.transforms.add(new versionbustertransform()); bundletable.bundles.add(scriptlibrary);
method url:
public static string query(string path) { if (httpruntime.cache[path] == null) { var absolutepath = hostingenvironment.mappath(path); httpruntime.cache.insert(path, string.format("{0}?v={1}.{2}.{3}.{4}", path, global.properties.version.major, global.properties.version.minor, global.properties.version.release, global.properties.version.build), new cachedependency(absolutepath)); } return httpruntime.cache[path] string; }
header template:
@styles.render(configbundles.styles) @scripts.render(configbundles.scripts)
currently, when open site in development environment, following references printed in header:
<link href="/content/styles/libraries/core-3.1.0.css?v=0.3.5.0" rel="stylesheet"> <link href="/content/styles/libraries/icomoon/style.css?v=0.3.5.0" rel="stylesheet"> <link href="/content/styles/site.css?v=0.3.5.0" rel="stylesheet"> <script src="/content/scripts/libraries/modernizr-2.6.2.js?v=0.3.5.0"></script> <script src="/content/scripts/libraries/bootstrap-3.0.0.js?v=0.3.5.0"></script> <script src="/content/scripts/framework/yeack.js?v=0.3.5.0"></script>
however, in production, printing this:
<link href="/content/styles/css?v=smkl_qnlrzbycabc0ze--hpqjmwlxxss9p8gl7jtfsc1" rel="stylesheet"> <script src="/content/scripts/js?v=yvua47u8n_htavmoq5u1vzhyrgeh3qufsuypjronpbm1"></script>
why doesn't bundling transform have effect on printed in production?
Comments
Post a Comment