Skip to content

Commit ce00138

Browse files
Fix(loader): Remove aggressive cache-busting and fix race conditions
1 parent 5be9048 commit ce00138

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

js/loader.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,17 @@ requirejs.config({
4141
});
4242

4343
requirejs(["i18next", "i18nextHttpBackend"], function (i18next, i18nextHttpBackend) {
44+
45+
function getLanguage() {
46+
let lang = navigator.language;
47+
if (localStorage.languagePreference) {
48+
lang = localStorage.languagePreference;
49+
}
50+
return lang || "enUS";
51+
}
52+
4453
function updateContent() {
45-
console.log("updateContent() called"); // Debugging line
54+
console.log("updateContent() called");
4655
const elements = document.querySelectorAll("[data-i18n]");
4756

4857
elements.forEach(element => {
@@ -56,15 +65,17 @@ requirejs(["i18next", "i18nextHttpBackend"], function (i18next, i18nextHttpBacke
5665
return new Promise((resolve, reject) => {
5766
i18next.use(i18nextHttpBackend).init(
5867
{
59-
lng: "en",
68+
lng: getLanguage(),
6069
fallbackLng: "en",
6170
keySeparator: false,
6271
nsSeparator: false,
6372
interpolation: {
6473
escapeValue: false
6574
},
6675
backend: {
67-
loadPath: "locales/{{lng}}.json?v=" + Date.now()
76+
// REMOVED: Date.now() cache buster.
77+
// Using a static version or relying on browser cache is better for performance.
78+
loadPath: "locales/{{lng}}.json"
6879
}
6980
},
7081
function (err, t) {
@@ -74,7 +85,6 @@ requirejs(["i18next", "i18nextHttpBackend"], function (i18next, i18nextHttpBacke
7485
} else {
7586
console.log("i18next initialized");
7687
window.i18next = i18next;
77-
console.log("i18next Store:", i18next.store.data);
7888
resolve(i18next);
7989
}
8090
}
@@ -94,6 +104,11 @@ requirejs(["i18next", "i18nextHttpBackend"], function (i18next, i18nextHttpBacke
94104
try {
95105
await initializeI18next();
96106

107+
// Setup language change listener
108+
i18next.on("languageChanged", function () {
109+
updateContent();
110+
});
111+
97112
if (document.readyState === "loading") {
98113
document.addEventListener("DOMContentLoaded", function () {
99114
updateContent();
@@ -102,26 +117,14 @@ requirejs(["i18next", "i18nextHttpBackend"], function (i18next, i18nextHttpBacke
102117
console.log("DOM already loaded, updating content immediately");
103118
updateContent();
104119
}
105-
} catch (error) {
106-
console.error("Error initializing i18next:", error);
107-
}
108-
}
109120

110-
main().then(() => {
111-
requirejs(["utils/utils", "activity/activity"]);
112-
});
121+
// Load application logic after i18n is ready
122+
requirejs(["utils/utils", "activity/activity"]);
113123

114-
i18next.changeLanguage(lang, (err, t) => {
115-
if (err) {
116-
console.error("Error changing language:", err);
117-
return;
124+
} catch (error) {
125+
console.error("Error initializing app:", error);
118126
}
119-
updateContent();
120-
});
127+
}
121128

122-
i18next.on("languageChanged", function () {
123-
updateContent();
124-
});
129+
main();
125130
});
126-
127-
// requirejs(["utils/utils", "activity/activity"]);

0 commit comments

Comments
 (0)