diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index 6405714e85..c13711eaa3 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -34,6 +34,7 @@ "hookable": "^5.0.0", "ignore": "^5.1.8", "mlly": "^0.2.10", + "murmurhash-es": "^0.1.1", "nuxi": "3.0.0", "ohmyfetch": "^0.3.1", "pathe": "^0.2.0", diff --git a/packages/nuxt3/src/app/composables/fetch.ts b/packages/nuxt3/src/app/composables/fetch.ts index 0b949d55d7..1430ddb28b 100644 --- a/packages/nuxt3/src/app/composables/fetch.ts +++ b/packages/nuxt3/src/app/composables/fetch.ts @@ -1,5 +1,6 @@ import type { FetchOptions } from 'ohmyfetch' import type { $Fetch } from '@nuxt/nitro' +import { murmurHashV3 } from 'murmurhash-es' import type { AsyncDataOptions, _Transform, KeyOfRes } from './asyncData' import { useAsyncData } from './asyncData' @@ -35,10 +36,9 @@ export function useFetch< opts.key = generateKey(keys) } - return useAsyncData('$f' + opts.key, () => $fetch(url, opts) as Promise, opts) + return useAsyncData(opts.key, () => $fetch(url, opts) as Promise, opts) } -// TODO: More predictable universal hash function generateKey (keys) { - return JSON.stringify(keys).replace(/[{":}=/,]|https?:\/\//g, '_').replace(/_+/g, '_') + return '$f' + murmurHashV3(JSON.stringify(keys)) } diff --git a/yarn.lock b/yarn.lock index fd34fa95e5..415a0e584f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13086,6 +13086,13 @@ fsevents@~2.3.2: languageName: node linkType: hard +"murmurhash-es@npm:^0.1.1": + version: 0.1.1 + resolution: "murmurhash-es@npm:0.1.1" + checksum: 3f5aa267161b21ad5b52f4b240ecd40014f21256114fad4b196202a8079ab08af8cdf5e7136caa591846232edc4222ba790ce300b8a6de6085e68e229da26766 + languageName: node + linkType: hard + "mustache@npm:^2.3.0": version: 2.3.2 resolution: "mustache@npm:2.3.2" @@ -13815,6 +13822,7 @@ fsevents@~2.3.2: hookable: ^5.0.0 ignore: ^5.1.8 mlly: ^0.2.10 + murmurhash-es: ^0.1.1 nuxi: 3.0.0 ohmyfetch: ^0.3.1 pathe: ^0.2.0