From f900a45c81390f124814a901a871907031dd93ad Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Fri, 1 Aug 2025 11:18:06 +0800 Subject: [PATCH 1/2] feat: cache menu categories and tags --- .../src/components/MenuComponent.vue | 78 +++++++++++++------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/open-isle-cli/src/components/MenuComponent.vue b/open-isle-cli/src/components/MenuComponent.vue index 547f16b2c..d6bf2b8e4 100644 --- a/open-isle-cli/src/components/MenuComponent.vue +++ b/open-isle-cli/src/components/MenuComponent.vue @@ -172,33 +172,61 @@ export default { await updateCount() }) - try { - this.isLoadingCategory = true - fetch(`${API_BASE_URL}/api/categories`).then( - res => { - if (res.ok) { - res.json().then(data => { - this.categories = data.slice(0, 10) - }) - } - this.isLoadingCategory = false - } - ) - } catch { /* ignore */ } + const CAT_CACHE_KEY = 'menu-categories' + const TAG_CACHE_KEY = 'menu-tags' - try { - this.isLoadingTag = true - fetch(`${API_BASE_URL}/api/tags?limit=10`).then( - res => { - if (res.ok) { - res.json().then(data => { - this.tags = data - }) - } - this.isLoadingTag = false + const cachedCategories = localStorage.getItem(CAT_CACHE_KEY) + if (cachedCategories) { + try { + this.categories = JSON.parse(cachedCategories) + } catch { /* ignore */ } + } + + const cachedTags = localStorage.getItem(TAG_CACHE_KEY) + if (cachedTags) { + try { + this.tags = JSON.parse(cachedTags) + } catch { /* ignore */ } + } + + const fetchCategories = () => { + fetch(`${API_BASE_URL}/api/categories`).then(res => { + if (res.ok) { + res.json().then(data => { + this.categories = data.slice(0, 10) + localStorage.setItem(CAT_CACHE_KEY, JSON.stringify(this.categories)) + }) } - ) - } catch { /* ignore */ } + this.isLoadingCategory = false + }) + } + + const fetchTags = () => { + fetch(`${API_BASE_URL}/api/tags?limit=10`).then(res => { + if (res.ok) { + res.json().then(data => { + this.tags = data + localStorage.setItem(TAG_CACHE_KEY, JSON.stringify(this.tags)) + }) + } + this.isLoadingTag = false + }) + } + + this.isLoadingCategory = !cachedCategories + this.isLoadingTag = !cachedTags + + if (cachedCategories) { + setTimeout(fetchCategories, 1500) + } else { + fetchCategories() + } + + if (cachedTags) { + setTimeout(fetchTags, 1500) + } else { + fetchTags() + } }, methods: { cycleTheme, From caa255b882e4ed50c6d3230044c5df8c91d140fc Mon Sep 17 00:00:00 2001 From: tim Date: Fri, 1 Aug 2025 11:24:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8Dmenu=E6=AF=8F?= =?UTF-8?q?=E6=AC=A1=E9=83=BD=E5=88=B7=E6=96=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- open-isle-cli/src/components/MenuComponent.vue | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/open-isle-cli/src/components/MenuComponent.vue b/open-isle-cli/src/components/MenuComponent.vue index d6bf2b8e4..fd3ca8af7 100644 --- a/open-isle-cli/src/components/MenuComponent.vue +++ b/open-isle-cli/src/components/MenuComponent.vue @@ -167,7 +167,7 @@ export default { notificationState.unreadCount = 0 } } - await updateCount() + watch(() => authState.loggedIn, async () => { await updateCount() }) @@ -189,6 +189,9 @@ export default { } catch { /* ignore */ } } + this.isLoadingCategory = !cachedCategories + this.isLoadingTag = !cachedTags + const fetchCategories = () => { fetch(`${API_BASE_URL}/api/categories`).then(res => { if (res.ok) { @@ -213,9 +216,6 @@ export default { }) } - this.isLoadingCategory = !cachedCategories - this.isLoadingTag = !cachedTags - if (cachedCategories) { setTimeout(fetchCategories, 1500) } else { @@ -227,6 +227,8 @@ export default { } else { fetchTags() } + + await updateCount() }, methods: { cycleTheme,