(performance) fetch回数を減らす

This commit is contained in:
kakkokari-gtyih 2023-12-23 22:21:33 +09:00
parent c7d4147296
commit d7805f7c4c
6 changed files with 14 additions and 6 deletions

View File

@ -0,0 +1,8 @@
export function useGAsyncData(...params: Parameters<typeof useAsyncData>) {
if (!params[2]) {
params[2] = {};
}
params[2].getCachedData = (key) => useNuxtData(key).data.value ?? null;
return useAsyncData(...params);
}

View File

@ -12,12 +12,12 @@ const route = useRoute();
const { locale } = useI18n();
const navigation = ref();
const asideNavKey = ref(0);
const { data } = await useAsyncData(`navigation_${locale.value}`, () => fetchContentNavigation(queryContent(`/${locale.value}/docs/`)));
const { data } = await useGAsyncData(`navigation_${locale.value}`, () => fetchContentNavigation(queryContent(`/${locale.value}/docs/`)));
navigation.value = data.value;
watch(locale, async (to) => {
console.log('locale changed');
const { data } = await useAsyncData(`navigation_${to}`, () => fetchContentNavigation(queryContent(`/${to}/docs/`)));
const { data } = await useGAsyncData(`navigation_${to}`, () => fetchContentNavigation(queryContent(`/${to}/docs/`)));
navigation.value = data.value;
asideNavKey.value++;
});

View File

@ -66,7 +66,7 @@ onBeforeRouteLeave((to) => {
//
const runtimeConfig = useRuntimeConfig();
const { data } = await useAsyncData(`blog-${route.params.slug}`, () => queryContent<MiBlogParsedContent>(`/blog/${route.params.slug}`).findOne());
const { data } = await useGAsyncData(`blog-${route.params.slug}`, () => queryContent<MiBlogParsedContent>(`/blog/${route.params.slug}`).findOne());
if (!data.value) {
throw createError({ statusCode: 404, statusMessage: 'page not found', fatal: true });

View File

@ -38,7 +38,7 @@ const route = useRoute();
const localeState = useState('miHub_blog_originalLocale', () => locale.value);
localeState.value = locale.value;
const { data } = await useAsyncData('blog', () => queryContent('blog').only(['_path', 'navTitle', 'title', 'date']).sort({ date: -1 }).find());
const { data } = await useGAsyncData('blog', () => queryContent('blog').only(['_path', 'navTitle', 'title', 'date']).sort({ date: -1 }).find());
const localePath = useGLocalePath();
route.meta.title = t('_blog.title');

View File

@ -64,7 +64,7 @@ definePageMeta({
const route = useRoute();
const slugs = (route.params.slug as string[]).filter((v) => v !== '');
const { data } = await useAsyncData(`docs-${locale.value}-${slugs.join('-')}`, () => queryContent<MiDocsParsedContent>(`/${locale.value}/docs/${slugs.join('/')}`).findOne());
const { data } = await useGAsyncData(`docs-${locale.value}-${slugs.join('-')}`, () => queryContent<MiDocsParsedContent>(`/${locale.value}/docs/${slugs.join('/')}`).findOne());
if (!data.value) {
throw createError({ statusCode: 404, statusMessage: 'page not found', fatal: true });

View File

@ -36,7 +36,7 @@ defineI18nRoute(false);
const route = useRoute();
const { data } = await useAsyncData('global-ns', () => queryContent(`/ns`).findOne());
const { data } = await useGAsyncData('global-ns', () => queryContent(`/ns`).findOne());
if (!data.value) {
throw createError({ statusCode: 404, statusMessage: 'page not found', fatal: true });