Skip to content

Commit cb12ca0

Browse files
authored
Merge pull request #13 from thavelick/environment_aware_caching
Implement environment-aware PWA caching
2 parents 8970f96 + a403793 commit cb12ca0

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

CLAUDE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,11 @@ Before any commit:
119119
### CSS Guidelines
120120
- Always put styles in style.css, never inline
121121

122+
### PWA Caching Control
123+
Development caching behavior can be controlled via URL parameters:
124+
- Default: No caching on localhost/127.0.0.1/192.168.x (dev-friendly)
125+
- `?cache=force` - Enable caching on local development
126+
- `?cache=disable` - Disable caching in production (for testing)
127+
122128
### Implementation Planning Process
123129
When asked to plan an implementation, follow the standardized process documented in `PLANNING.md`.

public_html/search.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,25 @@ async function registerServiceWorker(windowObj = window) {
187187
}
188188
}
189189

190+
function shouldEnableCaching(windowObj = window) {
191+
const urlParams = new URLSearchParams(windowObj.location.search);
192+
const cacheParam = urlParams.get("cache");
193+
194+
// Explicit override via URL parameter
195+
if (cacheParam === "force") return true;
196+
if (cacheParam === "disable") return false;
197+
198+
// Default: disable caching on local development
199+
const isLocal =
200+
windowObj.location.hostname === "localhost" ||
201+
windowObj.location.hostname === "127.0.0.1" ||
202+
windowObj.location.hostname.startsWith("192.168.");
203+
204+
return !isLocal;
205+
}
206+
190207
function initializePWA(windowObj = window) {
191-
if (isServiceWorkerSupported(windowObj)) {
208+
if (isServiceWorkerSupported(windowObj) && shouldEnableCaching(windowObj)) {
192209
registerServiceWorker(windowObj);
193210
}
194211
}
@@ -310,5 +327,6 @@ if (typeof module !== "undefined" && module.exports) {
310327
showSaveMessage,
311328
setupSettingsEventListeners,
312329
initializeSettings,
330+
shouldEnableCaching,
313331
};
314332
}

public_html/service-worker-lib.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const STATIC_CACHE_NAME = "just-bangs-static-v6";
1+
const STATIC_CACHE_NAME = "just-bangs-static-v7";
22

33
const STATIC_ASSETS = [
44
"./",

tests/search.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
showSaveMessage,
1919
setupSettingsEventListeners,
2020
initializeSettings,
21+
shouldEnableCaching,
2122
} = require("../public_html/search.js");
2223

2324
describe("buildSearchUrl", () => {
@@ -532,6 +533,10 @@ describe("PWA Functions", () => {
532533
register: mockRegister,
533534
},
534535
},
536+
location: {
537+
search: "",
538+
hostname: "example.com",
539+
},
535540
};
536541

537542
initializePWA(mockWindow);
@@ -804,4 +809,32 @@ describe("Settings Functions", () => {
804809
expect(() => initializeSettings(mockWindow)).not.toThrow();
805810
});
806811
});
812+
813+
describe("shouldEnableCaching", () => {
814+
const testCases = [
815+
// [hostname, queryString, expected, description]
816+
["localhost", "?cache=force", true, "cache=force parameter returns true"],
817+
[
818+
"example.com",
819+
"?cache=disable",
820+
false,
821+
"cache=disable parameter returns false",
822+
],
823+
["localhost", "", false, "localhost hostname returns false"],
824+
["127.0.0.1", "", false, "127.0.0.1 hostname returns false"],
825+
["192.168.1.100", "", false, "192.168.x hostname returns false"],
826+
["example.com", "", true, "production hostname returns true"],
827+
];
828+
829+
test.each(testCases)(
830+
"hostname: %s, query: %s -> %s (%s)",
831+
(hostname, search, expected, _description) => {
832+
const mockWindow = {
833+
location: { hostname, search },
834+
};
835+
836+
expect(shouldEnableCaching(mockWindow)).toBe(expected);
837+
},
838+
);
839+
});
807840
});

0 commit comments

Comments
 (0)