fix(nux3): use murmurhash for fetchKey (#1195)

This commit is contained in:
pooya parsa 2021-10-18 20:31:18 +02:00 committed by GitHub
parent 80883cfb8f
commit 4cff21005f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 3 deletions

View File

@ -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",

View File

@ -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<ResT>, opts)
return useAsyncData(opts.key, () => $fetch(url, opts) as Promise<ResT>, opts)
}
// TODO: More predictable universal hash
function generateKey (keys) {
return JSON.stringify(keys).replace(/[{":}=/,]|https?:\/\//g, '_').replace(/_+/g, '_')
return '$f' + murmurHashV3(JSON.stringify(keys))
}

View File

@ -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