diff --git a/composables/useGAsyncData.ts b/composables/useGAsyncData.ts new file mode 100644 index 00000000..dd97a2f4 --- /dev/null +++ b/composables/useGAsyncData.ts @@ -0,0 +1,8 @@ +export function useGAsyncData(...params: Parameters) { + if (!params[2]) { + params[2] = {}; + } + params[2].getCachedData = (key) => useNuxtData(key).data.value ?? null; + + return useAsyncData(...params); +} \ No newline at end of file diff --git a/layouts/docs.vue b/layouts/docs.vue index d5703e15..2e2bcbd9 100644 --- a/layouts/docs.vue +++ b/layouts/docs.vue @@ -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++; }); diff --git a/pages/blog/[slug].vue b/pages/blog/[slug].vue index 56dde500..1179b3c8 100644 --- a/pages/blog/[slug].vue +++ b/pages/blog/[slug].vue @@ -66,7 +66,7 @@ onBeforeRouteLeave((to) => { // ▲他言語からやってきたときに正しいパスに戻す▲ const runtimeConfig = useRuntimeConfig(); -const { data } = await useAsyncData(`blog-${route.params.slug}`, () => queryContent(`/blog/${route.params.slug}`).findOne()); +const { data } = await useGAsyncData(`blog-${route.params.slug}`, () => queryContent(`/blog/${route.params.slug}`).findOne()); if (!data.value) { throw createError({ statusCode: 404, statusMessage: 'page not found', fatal: true }); diff --git a/pages/blog/index.vue b/pages/blog/index.vue index 9fbc6d99..5bf09f72 100644 --- a/pages/blog/index.vue +++ b/pages/blog/index.vue @@ -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'); diff --git a/pages/docs/[...slug].vue b/pages/docs/[...slug].vue index 3c334db8..97cdc215 100644 --- a/pages/docs/[...slug].vue +++ b/pages/docs/[...slug].vue @@ -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(`/${locale.value}/docs/${slugs.join('/')}`).findOne()); +const { data } = await useGAsyncData(`docs-${locale.value}-${slugs.join('-')}`, () => queryContent(`/${locale.value}/docs/${slugs.join('/')}`).findOne()); if (!data.value) { throw createError({ statusCode: 404, statusMessage: 'page not found', fatal: true }); diff --git a/pages/ns.vue b/pages/ns.vue index cefbed55..ad69221d 100644 --- a/pages/ns.vue +++ b/pages/ns.vue @@ -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 });