Skip to content

Instantly share code, notes, and snippets.

@lukekarrys
Last active December 7, 2016 18:16
Show Gist options
  • Save lukekarrys/8b105bf6a40c3e68b656 to your computer and use it in GitHub Desktop.
Save lukekarrys/8b105bf6a40c3e68b656 to your computer and use it in GitHub Desktop.
Cachebusting

I created this gist so at my current work, I could pass around this patch file to get local cachebusting working for development.

diff -rupN cms/lib/utils.js /Users/lkarrys/cms/lib/utils.js
--- cms/lib/utils.js 2011-09-28 14:32:30.000000000 -0700
+++ /Users/lkarrys/cms/lib/utils.js 2011-09-28 15:31:00.000000000 -0700
@@ -1,3 +1,5 @@
+var cacheBust = getCacheBust();
+
function fileAsString(node) {
if (typeof(node) == "undefined" || !node.isNodeType("nt:file"))
return false;
@@ -61,16 +63,16 @@ function registerComponent(siteRoot, jcr
j = null;
for (j in globalJsPaths) {
- js.push(siteRoot + globalJsPaths[j]);
+ js.push(siteRoot + globalJsPaths[j] + cacheBust);
}
for (j in globalCssPaths) {
- css.push(siteRoot + globalCssPaths[j]);
+ css.push(siteRoot + globalCssPaths[j] + cacheBust);
}
for (j in cssPaths) {
- css.push(cssPaths[j]);
+ css.push(cssPaths[j] + cacheBust);
}
for (j in jsPaths) {
- js.push(jsPaths[j]);
+ js.push(jsPaths[j] + cacheBust);
}
var partial = fileAsString(component.getNode("body.html"));
Handlebars.registerPartial(component["componentId"], partial);
@@ -112,6 +114,26 @@ function mergePropertiesPrefix(ctx, pref
return obj3;
}
+function getCacheBust() {
+ var cacheBust = "",
+ cacheBustOperator = "?";
+
+ if (isDevelopment()) {
+ cacheBust = cacheBustOperator + "cb=" + new Date().getTime();
+ }
+
+ return cacheBust;
+}
+
+function isDevelopment() {
+ var currentUrl = request.getRequestURL().toString();
+ return (
+ currentUrl.indexOf('://localhost:80') > -1 ||
+ currentUrl.indexOf('.local:80') > -1 ||
+ /.*(172|192|10|127)\.\d{1,3}\.\d{1,3}\.\d{1,3}:80.*/.test(currentUrl)
+ );
+}
+
function returnArrayOfChildNodes(node) {
var retArr = [];
for (var key in node) {
diff -rupN cms/page/html.esp /Users/lkarrys/cms/page/html.esp
--- cms/page/html.esp 2011-09-28 14:32:30.000000000 -0700
+++ /Users/lkarrys/cms/page/html.esp 2011-09-28 15:31:17.000000000 -0700
@@ -97,15 +97,15 @@
<%}
for (i in globalJsPaths) {
- js.push(siteRoot + globalJsPaths[i]);
+ js.push(siteRoot + globalJsPaths[i] + cacheBust);
}
for (i in themeJsPaths) {
- js.push(siteRoot + themeJsPaths[i]);
+ js.push(siteRoot + themeJsPaths[i] + cacheBust);
}
for (i in layoutJsPaths) {
- js.push(siteRoot + layoutJsPaths[i]);
+ js.push(siteRoot + layoutJsPaths[i] + cacheBust);
}
// register components
@@ -134,15 +134,15 @@
<%}
for (i in globalCssPaths) {
- css.push(siteRoot + globalCssPaths[i]);
+ css.push(siteRoot + globalCssPaths[i] + cacheBust);
}
for (i in layoutCssPaths) {
- css.push(siteRoot + layoutCssPaths[i]);
+ css.push(siteRoot + layoutCssPaths[i] + cacheBust);
}
for (i in themeCssPaths) {
- css.push(siteRoot + themeCssPaths[i]);
+ css.push(siteRoot + themeCssPaths[i] + cacheBust);
}
context = mergeProperties(context,
#!/bin/sh
curl -s -S https://raw.github.com/gist/8b105bf6a40c3e68b656/38dc8b580809bb4c866206f9c90f42ccc11b92d6/cachebust.diff > ~/__8b105bf6a40c3e68b656__.diff
cd /Volumes/localhost/apps/cms/ && patch -p1 < ~/__8b105bf6a40c3e68b656__.diff
rm -f ~/__8b105bf6a40c3e68b656__.diff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment