From 18edde64c343ebb32cea8c2dbbf9a884900ad37b Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 6 Feb 2026 17:42:35 +0800 Subject: [PATCH] fix: sync home filter dropdown state to URL history --- frontend_nuxt/pages/index.vue | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/frontend_nuxt/pages/index.vue b/frontend_nuxt/pages/index.vue index b0f06fc56..e8e0ea650 100644 --- a/frontend_nuxt/pages/index.vue +++ b/frontend_nuxt/pages/index.vue @@ -202,6 +202,28 @@ const selectedTagsSet = (tags) => { .map((v) => (isNaN(v) ? v : Number(v))) } +const normalizeCategoryFromQuery = (category) => { + if (category == null || category === '') return '' + const raw = Array.isArray(category) ? category[0] : category + const decoded = decodeURIComponent(raw) + return isNaN(decoded) ? decoded : Number(decoded) +} + +const normalizeTagsFromQuery = (tags) => { + if (tags == null || tags === '') return [] + const raw = Array.isArray(tags) ? tags.join(',') : tags + return raw + .split(',') + .filter((v) => v) + .map((v) => decodeURIComponent(v)) + .map((v) => (isNaN(v) ? v : Number(v))) +} + +const arraysShallowEqual = (a = [], b = []) => { + if (a.length !== b.length) return false + return a.every((v, idx) => String(v) === String(b[idx])) +} + /** 初始化:仅在客户端首渲染时根据路由同步一次 **/ onMounted(() => { const { category, tags } = route.query @@ -239,6 +261,32 @@ watch( }, ) +// 从筛选器变更回写到 URL,确保浏览器历史可回退到上一个筛选状态。 +watch([selectedCategory, selectedTags], async ([category, tags]) => { + const routeCategory = normalizeCategoryFromQuery(route.query.category) + const routeTags = normalizeTagsFromQuery(route.query.tags) + + const categoryChanged = String(category ?? '') !== String(routeCategory ?? '') + const tagsChanged = !arraysShallowEqual(tags || [], routeTags) + if (!categoryChanged && !tagsChanged) return + + const nextQuery = { ...route.query } + + if (category == null || category === '') { + delete nextQuery.category + } else { + nextQuery.category = encodeURIComponent(String(category)) + } + + if (!Array.isArray(tags) || tags.length === 0) { + delete nextQuery.tags + } else { + nextQuery.tags = tags.map((v) => encodeURIComponent(String(v))).join(',') + } + + await navigateTo({ path: '/', query: nextQuery }) +}) + /** 选项加载(分类/标签名称回填) **/ const loadOptions = async () => { if (selectedCategory.value && !isNaN(selectedCategory.value)) {