Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): automatically generate unique keys for keyed composables #4955

Merged
merged 28 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
435a331
feat(nuxt): automatically generate unique keys for keyed composables
danielroe May 12, 2022
fcbb164
docs: update and fix typings
danielroe May 12, 2022
ca20190
fix: update lazy fetch types
danielroe May 12, 2022
8f709e3
fix: add acorn and estree-walker to dependencies
danielroe May 12, 2022
be98e92
refactor: rename magic keys -> composable keys
danielroe May 13, 2022
cf0e2da
perf: use regexp for early return
danielroe May 13, 2022
1c1bac4
fix: normalise id into relative path
danielroe May 13, 2022
52d531d
fix: improve readability
danielroe May 13, 2022
ae61515
perf: use hash map for generating short keys
danielroe May 13, 2022
231c5a5
fix: remove cjs from transformed extensions
danielroe May 13, 2022
bf02262
docs: apply suggestions
danielroe May 13, 2022
f818c8c
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe May 13, 2022
4b20d10
fix: remove changed files
danielroe May 13, 2022
88cf9f0
docs: update counter example
danielroe May 13, 2022
da9cf27
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe May 20, 2022
040acb2
refactor: simplify handling of fallback
danielroe May 20, 2022
bf63061
refactor: use `ohash` for hashing
danielroe Jun 8, 2022
92bd6dc
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe Jun 8, 2022
585c284
Merge branch 'main' into feat/magic-keys
danielroe Jun 9, 2022
4bb6cfa
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe Jun 17, 2022
9ced5f2
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe Jul 4, 2022
b3bde78
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe Jul 6, 2022
eab5ec3
fix playground,, asyncData with key, improve runtime checks and small…
pi0 Jul 6, 2022
502cc11
fix lint issue
pi0 Jul 6, 2022
f116ff6
fix: useState with explicit key
pi0 Jul 7, 2022
faab8b1
refactor: use unplugin's built-in parser
danielroe Jul 7, 2022
5964216
chore: update lockfile
danielroe Jul 7, 2022
4c42bc6
add workaround for autogenerated key
pi0 Jul 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
perf: use hash map for generating short keys
  • Loading branch information
danielroe committed May 13, 2022
commit ae61515cc60ddac3eaf2ad12654a91392219024f
8 changes: 5 additions & 3 deletions packages/nuxt/src/app/composables/asyncData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ export function useAsyncData<
// eslint-disable-next-line prefer-const
let [key, handler, options] = (typeof _key === 'string'
? [_key, _handler, _options]
: typeof _options === 'string'
? [_options, _key, {}]
: [fallback, _key, _options]
: typeof _handler === 'string'
? [_handler, _key, {}]
: typeof _options === 'string'
? [_options, _key, {}]
: [fallback, _key, _options]
) as [string, (ctx?: NuxtApp) => Promise<DataT>, AsyncDataOptions<DataT, Transform, PickKeys>]
// Validate arguments
if (typeof key !== 'string') {
Expand Down
19 changes: 15 additions & 4 deletions packages/vite/src/plugins/composable-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ export interface ComposableKeysOptions {
rootDir?: string
}

const hashMap: Record<string, string> = {}

function createKey (source: string) {
const hash = crypto.createHash('md5')
hash.update(source)
return hash.digest('base64').toString()
let outputLength = 3
do {
const hash = crypto.createHash('md5')
hash.update(source)
const key = hash.digest('base64').toString().slice(0, outputLength)
if (key in hashMap && hashMap[key] !== source) {
outputLength++
continue
}
hashMap[key] = source
return key
} while (true)
}

const keyedFunctions = [
Expand Down Expand Up @@ -49,7 +60,7 @@ export const composableKeysPlugin = (options: ComposableKeysOptions = {}): Plugi
const end = (node as any).end
s.appendLeft(
codeIndex + end - 1,
(node.arguments.length ? ', ' : '') + "'" + createKey(`${relativeID}-${codeIndex + end}`) + "'"
(node.arguments.length ? ', ' : '') + "'$" + createKey(`${relativeID}-${codeIndex + end}`) + "'"
)
}
}
Expand Down