From 55e794dd8029d9c25e9faee6bd326c657ce48cb3 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 22 Feb 2025 21:04:41 +0000 Subject: [PATCH] fix(nuxt): use `ohash` to calculate legacy async data key without hash (#31087) --- .../nuxt/src/app/composables/component.ts | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index 345915db62..1f19505c69 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -1,6 +1,7 @@ import { getCurrentInstance, reactive, toRefs } from 'vue' import type { DefineComponent, defineComponent } from 'vue' import { useHead } from '@unhead/vue' +import { hash } from 'ohash' import type { NuxtApp } from '../nuxt' import { getNuxtAppCtx, useNuxtApp } from '../nuxt' import { useAsyncData } from './asyncData' @@ -9,13 +10,23 @@ import { createError } from './error' export const NuxtComponentIndicator = '__nuxt_component' +/* @__NO_SIDE_EFFECTS__ */ +function getFetchKey () { + const vm = getCurrentInstance()! + const route = useRoute() + const { _fetchKeyBase } = vm.proxy!.$options + return hash([ + _fetchKeyBase, + route.path, + route.query, + route.matched.findIndex(r => Object.values(r.components || {}).includes(vm.type)), + ]) +} + async function runLegacyAsyncData (res: Record | Promise>, fn: (nuxtApp: NuxtApp) => Promise>) { const nuxtApp = useNuxtApp() - const route = useRoute() - const vm = getCurrentInstance()! - const { fetchKey, _fetchKeyBase } = vm.proxy!.$options - const key = (typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey) || - ([_fetchKeyBase, route.fullPath, route.matched.findIndex(r => Object.values(r.components || {}).includes(vm.type))].join(':')) + const { fetchKey } = getCurrentInstance()!.proxy!.$options + const key = (typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey) || getFetchKey() const { data, error } = await useAsyncData(`options:asyncdata:${key}`, () => import.meta.server ? nuxtApp.runWithContext(() => fn(nuxtApp)) : fn(nuxtApp)) if (error.value) { throw createError(error.value)