feat(SSR): 为搜索页启用SSR

This commit is contained in:
2025-09-28 16:40:46 +08:00
parent 9f2f7f6984
commit 1ac9c76c3e

View File

@ -101,12 +101,24 @@
const localePath = useLocalePath();
// 搜索相关
const loading = ref(true);
const { search } = useMeilisearch();
const keyword = ref('');
const activeRequestId = ref(0);
const sections = ref<SearchSection[]>([]);
const {
data: sections,
pending: loading,
error,
refresh,
} = await useAsyncData(
() => `search-${route.query.query ?? ''}`,
async () => {
const q = String(route.query.query ?? '').trim();
if (!q) return [];
return await search(q, { limit: 12 });
}
);
// 本地化+空Section过滤
const filteredSections = computed(() =>
sections.value
@ -137,7 +149,7 @@
return map;
});
// 分控制
// 分控制
const currentPage = ref(1);
const hasResults = computed(() =>
@ -165,7 +177,6 @@
return;
}
loading.value = true;
try {
const results = await search(trimmed, { limit: 12 });
if (requestId === activeRequestId.value) {
@ -178,10 +189,6 @@
if (requestId === activeRequestId.value) {
sections.value = [];
}
} finally {
if (requestId === activeRequestId.value) {
loading.value = false;
}
}
};
@ -204,12 +211,8 @@
);
onMounted(() => {
if (typeof route.query.query === 'string' && route.query.query.trim()) {
if (typeof route.query.query === 'string' && route.query.query.trim())
keyword.value = route.query.query;
performSearch(route.query.query);
} else {
loading.value = false;
}
});
useHead(() => ({