Skip to content

Commit

Permalink
fix cache init
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed Oct 19, 2024
1 parent e7b5247 commit 7fa01b3
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions extractOptionsFromScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,42 +245,41 @@ function extractOptionsFromScript(window, optsArgs = {
const parsedTranslationCache = JSON.parse(translationCache);
if (parsedTranslationCache && typeof parsedTranslationCache === "object") {
window.translationCache = parsedTranslationCache;
// const injectedCacheElement = document.getElementById("globalseo-translation-cache");
// if (!thisScriptTranslationCache && injectedCacheElement) {
// try {
// const stringifiedCache = injectedCacheElement.textContent;
// const parsedCache = JSON.parse(stringifiedCache);
// window.translationCache = {
// ...window.translationCache,
// [window.location.pathname]: {
// ...parsedCache[window.location.pathname]
// }
// }

// // set to local storage
// // if (!window.isWorker && !window.activeSubdomain) {
// window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache));
// // }
// } catch(error) {
// // do nothing
// }
// }
}

const thisScriptTranslationCache = window.translationScriptTag.getAttribute(DATA_TRANSLATION_CACHE);
if (thisScriptTranslationCache) {
try {
const parsedThisScriptTranslationCache = JSON.parse(thisScriptTranslationCache);
window.translationCache = {
...window.translationCache,
...parsedThisScriptTranslationCache
}

window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache));
} catch (e) {
console.log("Error parsing this script translation cache", e)
// handle cache coming from server side rendering
const thisScriptTranslationCache = window.translationScriptTag.getAttribute(DATA_TRANSLATION_CACHE);
if (thisScriptTranslationCache) {
const parsedThisScriptTranslationCache = JSON.parse(thisScriptTranslationCache);
window.translationCache = {
...window.translationCache,
[window.location.pathname]: {
...window.translationCache[window.location.pathname],
...parsedThisScriptTranslationCache[window.location.pathname]
}
}

const injectedCacheElement = document.getElementById("globalseo-translation-cache");
if (!thisScriptTranslationCache && injectedCacheElement) {
try {
const stringifiedCache = injectedCacheElement.textContent;
const parsedCache = JSON.parse(stringifiedCache);
window.translationCache = {
...window.translationCache,
[window.location.pathname]: {
...parsedCache[window.location.pathname]
}
}

// set to local storage
// if (!window.isWorker && !window.activeSubdomain) {
window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache));
// }
} catch(error) {
// do nothing
}
}
window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache));
}
} catch (e) {
console.log("Error parsing translation cache", e)
Expand Down

0 comments on commit 7fa01b3

Please sign in to comment.