From c3d5fdaa1fe8b84f9d3df2e7faa5b98e6bc07f18 Mon Sep 17 00:00:00 2001 From: jemikanegara Date: Thu, 24 Oct 2024 01:23:01 +0800 Subject: [PATCH] support globalseo-translation-cache --- extractOptionsFromScript.js | 42 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/extractOptionsFromScript.js b/extractOptionsFromScript.js index 0c376ff..7c3fa04 100644 --- a/extractOptionsFromScript.js +++ b/extractOptionsFromScript.js @@ -282,26 +282,24 @@ 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 injectedCacheElement = document.getElementById("globalseo-translation-cache"); + if (injectedCacheElement) { + try { + const stringifiedCache = injectedCacheElement.textContent; + const parsedCache = JSON.parse(stringifiedCache); + + window.translationCache = { + ...window.translationCache, + [window.location.pathname]: { + ...window.translationCache[window.location.pathname], + ...parsedCache[window.location.pathname] + } + } + } catch(error) { + // do nothing + } } // handle cache coming from server side rendering @@ -315,9 +313,9 @@ function extractOptionsFromScript(window, optsArgs = { ...parsedThisScriptTranslationCache[window.location.pathname] } } - - window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache)); } + + window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache)); } catch (e) { console.log("Error parsing translation cache", e) }