Skip to content

Commit

Permalink
dont update local storage on dom change if nothing really changed
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed May 15, 2024
1 parent 773f8ac commit 02d8f9c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { isBrowser, getWeployOptions, setWeployOptions, setWeployActiveLang } = require('./utils/configs.js');
const { isBrowser, getWeployOptions, setWeployOptions, setWeployActiveLang, setIsTranslationInitialized, getIsTranslationInitialized } = require('./utils/configs.js');
const checkIfTranslatable = require('./utils/translation/checkIfTranslatable.js');
const allWeployLanguagesList = require('./utils/languages/allWeployLanguagesList.js');
const { fetchLanguageList } = require('./utils/languages/fetchLanguageList.js');
Expand Down Expand Up @@ -181,7 +181,7 @@ function processTextNodes(textNodes = [], language = "", apiKey = "") {
}
});

if (isBrowser()) window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache));
if (isBrowser() && !getIsTranslationInitialized()) window.localStorage.setItem("translationCachePerPage", JSON.stringify(window.translationCache));
resolve(undefined);
}
});
Expand All @@ -194,7 +194,9 @@ function modifyHtmlStrings(rootElement, language, apiKey) {

const validTextNodes = filterValidTextNodes(textNodes) || [];

await processTextNodes(validTextNodes, language, apiKey).catch(reject).finally(() => {
await processTextNodes(validTextNodes, language, apiKey).then(() => {
setIsTranslationInitialized(true);
}).catch(reject).finally(() => {
window.weployTranslating = false;
renderWeploySelectorState();
});
Expand Down
27 changes: 27 additions & 0 deletions utils/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,39 @@ function setWeployActiveLang(language) {
}
}

/** Is Translation Initialized */
var isTranslationInitialized;

function getIsTranslationInitialized() {
if (isBrowser()){
if (!window.isTranslationInitialized) {
setIsTranslationInitialized(null)
}
return window.isTranslationInitialized;
} else {
if (!isTranslationInitialized) {
setIsTranslationInitialized(null)
}
return isTranslationInitialized;
}
}

function setIsTranslationInitialized(value) {
if (isBrowser()) {
window.isTranslationInitialized = value
} else {
isTranslationInitialized = value
}
}

module.exports = {
isBrowser,
getWeployOptions,
setWeployOptions,
getWeployActiveLang,
setWeployActiveLang,
getIsTranslationInitialized,
setIsTranslationInitialized,
API_URL,
CDN_URL,
KV_URL,
Expand Down
6 changes: 3 additions & 3 deletions utils/languages/getSelectedLanguage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getWeployOptions, setWeployActiveLang } = require("../configs");
const { getWeployOptions, setWeployActiveLang, getIsTranslationInitialized } = require("../configs");
const { fetchLanguageList } = require("./fetchLanguageList");

//@deprecated
Expand Down Expand Up @@ -31,7 +31,7 @@ async function getLanguageFromLocalStorage() {
const localStorageLang = localStorage.getItem("language");

if (paramsLang && (paramsLang != localStorageLang)) {
localStorage.setItem("language", paramsLang);
if (!getIsTranslationInitialized()) localStorage.setItem("language", paramsLang);
setWeployActiveLang(paramsLang);
return paramsLang;
}
Expand Down Expand Up @@ -62,7 +62,7 @@ function saveDefaultLanguageToLocalStorage(availableLangs = [], useBrowserLang =
const langInAvailableLangsOrFirst = langInAvailableLangs?.lang || availableLangs[0].lang // If the language is not in the available languages, use the first available language
const langToSave = useBrowserLang ? langInAvailableLangsOrFirst : availableLangs[0].lang // If useBrowserLang is true, use the language from the browser, otherwise use the first available language
// Save the language to local storage
localStorage.setItem("language", langToSave);
if (!getIsTranslationInitialized()) localStorage.setItem("language", langToSave);
setWeployActiveLang(langToSave);
}

Expand Down

0 comments on commit 02d8f9c

Please sign in to comment.