diff --git a/.jules/bolt.md b/.jules/bolt.md index f74ef2f..213f4b0 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -1,3 +1,6 @@ ## 2024-06-20 - Unnecessary initial DOM updates for default language **Learning:** The simple static i18n implementation runs `node.textContent = dict[node.dataset.i18n]` for every translatable node on the initial script load, even when the HTML is already written in the target language (Korean). This creates unnecessary layout/paint operations and blocking time on the main thread for elements that don't need text changes. **Action:** Always check if the current value matches the desired value before updating the DOM (`node.textContent !== newText`), and add early exits when setting state to the same value to avoid redundant DOM traversal and writes. +## 2026-06-22 - Prevent redundant DOM queries on initial language set +**Learning:** The simple static i18n implementation was scanning the entire document to set translations even when the pre-rendered document language matched the user's preferred language. This happened because `currentLang` was initially `null`, causing the early return `if (currentLang === lang) return;` to fail on the initial page load. +**Action:** Always initialize language state variables using existing document attributes (`document.documentElement.lang`) if available, to ensure early-return checks correctly skip redundant work when the page is already in the target state. diff --git a/i18n.js b/i18n.js index 2393774..e801724 100644 --- a/i18n.js +++ b/i18n.js @@ -308,7 +308,7 @@ let langButtons = null; let metaDesc = null; let ogDesc = null; let footerLogo = null; -let currentLang = null; +let currentLang = document.documentElement.lang || "ko"; function setLanguage(lang) { if (currentLang === lang) return; // Skip if already in the requested language