Skip to content

Commit 84ee08c

Browse files
authored
Fix: npm dev/prod environment handling (sugarlabs#5696)
* fix development and production environment handling * Add cache disabling in Planet for dev mode * Add env.js file to support static hosting of MB
1 parent 15f04dc commit 84ee08c

File tree

5 files changed

+72
-30
lines changed

5 files changed

+72
-30
lines changed

env.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// File for static Musicblocks hosting e.g. https://sugarlabs.github.io/musicblocks/
2+
window.MB_ENV = "production";
3+
window.MB_IS_DEV = false;

index.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@
107107
});
108108
</script>
109109
<script src="lib/astring.min.js" defer></script>
110-
111110
<script src="lib/acorn.min.js" defer></script>
111+
<script src="env.js"></script>
112112

113113

114114
<script data-main="js/loader" src="lib/require.js" defer></script>
@@ -577,13 +577,15 @@
577577
<script>
578578
// Register service worker only in production (not on localhost)
579579
if ("serviceWorker" in navigator) {
580-
// Detect if running on localhost for development
581-
const isLocalDev = window.location.hostname === "localhost" ||
582-
window.location.hostname === "127.0.0.1";
580+
583581
// Localhost SW is disabled to avoid caching; set allowLocalPwa to test installs.
584582
const allowLocalPwa = localStorage.getItem("allowLocalPwa") === "true";
585583

586-
if (!isLocalDev || allowLocalPwa) {
584+
// Detect if running on localhost for development
585+
const runtimeEnv = window.MB_ENV
586+
const isDevMode = runtimeEnv !== "production";
587+
588+
if (!isDevMode || allowLocalPwa) {
587589
// Production: Register service worker for offline mode
588590
if (navigator.serviceWorker.controller) {
589591
console.debug(
@@ -606,7 +608,7 @@
606608
} else {
607609
// Development: Skip service worker for instant updates
608610
console.log(
609-
"🔥 [DEV MODE] Service worker disabled on localhost for instant code updates"
611+
"🔥 [DEV MODE] Service worker disabled for instant code updates"
610612
);
611613
console.log(
612614
"Set localStorage.allowLocalPwa = 'true' to enable PWA install testing on localhost."

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ const app = express();
77
// Detect environment (default to development for safety)
88
const isDev = process.env.NODE_ENV !== "production";
99

10+
// runtime environment for browser
11+
app.get("/env.js", (req, res) => {
12+
res.type("application/javascript");
13+
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate");
14+
res.setHeader("Pragma", "no-cache");
15+
res.setHeader("Expires", "0");
16+
res.setHeader("Surrogate-Control", "no-store");
17+
res.send(
18+
`window.MB_ENV=${JSON.stringify(process.env.NODE_ENV || "development")};` +
19+
`window.MB_IS_DEV=${JSON.stringify(isDev)};`
20+
);
21+
});
22+
1023
// Enable compression for all responses
1124
app.use(
1225
compression({

planet/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<script type="text/javascript" src="libs/jquery-3.7.1.min.js"></script>
2525
<script type="text/javascript" src="libs/materialize.min.js"></script>
2626
<script type="text/javascript" src="libs/localforage.min.js"></script>
27+
<script type="text/javascript" src="../env.js"></script>
2728

2829
<script type="text/javascript" src="js/helper.js"></script>
2930
<script type="text/javascript" src="js/StringHelper.js"></script>

planet/js/ServerInterface.js

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ServerInterface {
2828
this.ServerURL = "https://musicblocks.sugarlabs.org/planet-server/index.php";
2929
this.ConnectionFailureData = { success: false, error: "ERROR_CONNECTION_FAILURE" };
3030
this.APIKey = "3f2d3a4c-c7a4-4c3c-892e-ac43784f7381";
31+
this.disablePlanetCache = this.shouldDisablePlanetCache();
3132

3233
// Initialize RequestManager for rate limiting and retry logic
3334
this.requestManager = new RequestManager({
@@ -48,6 +49,20 @@ class ServerInterface {
4849
this.cacheInitialized = false;
4950
}
5051

52+
/**
53+
* Determines whether Planet caching should be disabled.
54+
* @returns {boolean}
55+
*/
56+
shouldDisablePlanetCache() {
57+
const runtimeEnv = window.MB_ENV;
58+
59+
if (runtimeEnv === "production") {
60+
return false;
61+
}
62+
63+
return true;
64+
}
65+
5166
/**
5267
* Initializes the cache manager
5368
* @returns {Promise<boolean>}
@@ -149,23 +164,24 @@ class ServerInterface {
149164
* Gets project details with caching support
150165
*/
151166
async getProjectDetails(ProjectID, callback) {
152-
// Try cache first
153-
await this.initCache();
154-
const cached = await this.cacheManager.getMetadata(ProjectID);
155-
156-
if (cached) {
157-
// eslint-disable-next-line no-console
158-
console.debug("[ServerInterface] Returning cached metadata for:", ProjectID);
159-
callback({ success: true, data: cached });
160-
return;
167+
if (!this.disablePlanetCache) {
168+
// Try cache first
169+
await this.initCache();
170+
const cached = await this.cacheManager.getMetadata(ProjectID);
171+
172+
if (cached) {
173+
// eslint-disable-next-line no-console
174+
console.debug("[ServerInterface] Returning cached metadata for:", ProjectID);
175+
callback({ success: true, data: cached });
176+
return;
177+
}
161178
}
162-
163179
// Fetch from server
164180
const obj = { action: "getProjectDetails", ProjectID: ProjectID };
165181

166182
this.throttledRequest(obj, async result => {
167183
// Cache successful responses
168-
if (result && result.success && result.data) {
184+
if (!this.disablePlanetCache && result && result.success && result.data) {
169185
await this.cacheManager.cacheMetadata(ProjectID, result.data);
170186
}
171187
callback(result);
@@ -190,23 +206,24 @@ class ServerInterface {
190206
* Downloads full project data with caching support
191207
*/
192208
async downloadProject(ProjectID, callback) {
193-
// Try cache first
194-
await this.initCache();
195-
const cached = await this.cacheManager.getProject(ProjectID);
196-
197-
if (cached) {
198-
// eslint-disable-next-line no-console
199-
console.debug("[ServerInterface] Returning cached project:", ProjectID);
200-
callback({ success: true, data: cached });
201-
return;
209+
if (!this.disablePlanetCache) {
210+
// Try cache first
211+
await this.initCache();
212+
const cached = await this.cacheManager.getProject(ProjectID);
213+
214+
if (cached) {
215+
// eslint-disable-next-line no-console
216+
console.debug("[ServerInterface] Returning cached project:", ProjectID);
217+
callback({ success: true, data: cached });
218+
return;
219+
}
202220
}
203-
204221
// Fetch from server
205222
const obj = { action: "downloadProject", ProjectID: ProjectID };
206223

207224
this.throttledRequest(obj, async result => {
208225
// Cache successful responses
209-
if (result && result.success && result.data) {
226+
if (!this.disablePlanetCache && result && result.success && result.data) {
210227
await this.cacheManager.cacheProject(ProjectID, result.data);
211228
}
212229
callback(result);
@@ -271,8 +288,14 @@ class ServerInterface {
271288
* Initializes the server interface
272289
*/
273290
async init() {
274-
await this.initCache();
291+
if (!this.disablePlanetCache) {
292+
await this.initCache();
293+
}
275294
// eslint-disable-next-line no-console
276-
console.debug("[ServerInterface] Initialized with rate limiting and caching");
295+
console.debug(
296+
`[ServerInterface] Initialized with rate limiting and ${
297+
this.disablePlanetCache ? "cache disabled (dev mode)" : "caching"
298+
}`
299+
);
277300
}
278301
}

0 commit comments

Comments
 (0)