Commit 3e2beebb48
Unsigned
Layout: unified · split
docs/.vitepress/theme/components/Search.vue +78 −16
| @@ -651,10 +651,16 @@ interface UrlSearchWorkerRequest { | ||
| 651 | 651 | pageOrderEntries: [string, number][] |
| 652 | 652 | } |
| 653 | 653 | |
| 654 | interface PreloadMetadataWorkerRequest { | |
| 655 | type: 'preload-metadata' | |
| 656 | pageOrderEntries: [string, number][] | |
| 657 | } | |
| 658 | ||
| 654 | 659 | type SearchWorkerRequest = |
| 655 | 660 | | LoadIndexWorkerRequest |
| 656 | 661 | | TextSearchWorkerRequest |
| 657 | 662 | | UrlSearchWorkerRequest |
| 663 | | PreloadMetadataWorkerRequest | |
| 658 | 664 | |
| 659 | 665 | const activePageFilter = ref<string | null>(null) |
| 660 | 666 | const searchWorkerReady = ref(false) |
| @@ -664,6 +670,10 @@ const searchWorkerConfigKey = computed(() => | ||
| 664 | 670 | let searchWorker: Worker | undefined |
| 665 | 671 | let searchWorkerRequestId = 0 |
| 666 | 672 | let loadedWorkerIndexKey = '' |
| 673 | let preloadedSearchDataKey = '' | |
| 674 | let preloadSearchDataRequest: | |
| 675 | | { key: string; promise: Promise<void> } | |
| 676 | | undefined | |
| 667 | 677 | const searchWorkerRequests = new Map< |
| 668 | 678 | number, |
| 669 | 679 | { |
| @@ -676,6 +686,49 @@ function getPageOrderEntries(): [string, number][] { | ||
| 676 | 686 | return [...pageMeta.entries()].map(([key, meta]) => [key, meta.order]) |
| 677 | 687 | } |
| 678 | 688 | |
| 689 | async function preloadSearchData( | |
| 690 | localeIndexValue: string, | |
| 691 | indexVersion: number, | |
| 692 | configKey: string | |
| 693 | ) { | |
| 694 | const workerIndexKey = `${localeIndexValue}\n${indexVersion}\n${configKey}` | |
| 695 | if (preloadedSearchDataKey === workerIndexKey) return | |
| 696 | if (preloadSearchDataRequest?.key === workerIndexKey) { | |
| 697 | await preloadSearchDataRequest.promise | |
| 698 | return | |
| 699 | } | |
| 700 | ||
| 701 | const promise = (async () => { | |
| 702 | if (loadedWorkerIndexKey !== workerIndexKey) { | |
| 703 | const indexJson = (await searchIndexData.value[localeIndexValue]?.()) | |
| 704 | ?.default | |
| 705 | await postSearchWorker<void>({ | |
| 706 | type: 'load-index', | |
| 707 | localeIndex: localeIndexValue, | |
| 708 | indexJson, | |
| 709 | indexVersion, | |
| 710 | config: getMiniSearchWorkerConfig() | |
| 711 | }) | |
| 712 | loadedWorkerIndexKey = workerIndexKey | |
| 713 | } | |
| 714 | ||
| 715 | await postSearchWorker<void>({ | |
| 716 | type: 'preload-metadata', | |
| 717 | pageOrderEntries: getPageOrderEntries() | |
| 718 | }) | |
| 719 | preloadedSearchDataKey = workerIndexKey | |
| 720 | })() | |
| 721 | ||
| 722 | preloadSearchDataRequest = { key: workerIndexKey, promise } | |
| 723 | try { | |
| 724 | await promise | |
| 725 | } finally { | |
| 726 | if (preloadSearchDataRequest?.promise === promise) { | |
| 727 | preloadSearchDataRequest = undefined | |
| 728 | } | |
| 729 | } | |
| 730 | } | |
| 731 | ||
| 679 | 732 | function stripNonCloneable(value: unknown): unknown { |
| 680 | 733 | if (typeof value === 'function' || typeof value === 'symbol') return undefined |
| 681 | 734 | if (!value || typeof value !== 'object') return value |
| @@ -774,6 +827,8 @@ onBeforeUnmount(() => { | ||
| 774 | 827 | searchWorker?.terminate() |
| 775 | 828 | searchWorker = undefined |
| 776 | 829 | loadedWorkerIndexKey = '' |
| 830 | preloadedSearchDataKey = '' | |
| 831 | preloadSearchDataRequest = undefined | |
| 777 | 832 | searchWorkerReady.value = false |
| 778 | 833 | for (const request of searchWorkerRequests.values()) { |
| 779 | 834 | request.reject(new Error('Search worker was terminated')) |
| @@ -781,6 +836,27 @@ onBeforeUnmount(() => { | ||
| 781 | 836 | searchWorkerRequests.clear() |
| 782 | 837 | }) |
| 783 | 838 | |
| 839 | watch( | |
| 840 | () => | |
| 841 | [ | |
| 842 | searchWorkerReady.value, | |
| 843 | showSearch.value, | |
| 844 | localeIndex.value, | |
| 845 | searchIndexVersion.value, | |
| 846 | searchWorkerConfigKey.value | |
| 847 | ] as const, | |
| 848 | async ([ready, isOpen, localeIndexValue, indexVersion, configKey]) => { | |
| 849 | if (!ready || !isOpen) return | |
| 850 | ||
| 851 | try { | |
| 852 | await preloadSearchData(localeIndexValue, indexVersion, configKey) | |
| 853 | } catch (error) { | |
| 854 | console.error('[search] search data preload failed', error) | |
| 855 | } | |
| 856 | }, | |
| 857 | { immediate: true } | |
| 858 | ) | |
| 859 | ||
| 784 | 860 | watch( |
| 785 | 861 | () => |
| 786 | 862 | [ |
| @@ -1320,22 +1396,8 @@ watchDebounced( | ||
| 1320 | 1396 | textSearchLoading.value = Boolean(searchQuery) |
| 1321 | 1397 | |
| 1322 | 1398 | try { |
| 1323 | const workerIndexKey = | |
| 1324 | `${localeIndexValue}\n${indexVersion}\n${configKey}` | |
| 1325 | if (loadedWorkerIndexKey !== workerIndexKey) { | |
| 1326 | const indexJson = (await searchIndexData.value[localeIndexValue]?.()) | |
| 1327 | ?.default | |
| 1328 | if (canceled) return | |
| 1329 | await postSearchWorker<void>({ | |
| 1330 | type: 'load-index', | |
| 1331 | localeIndex: localeIndexValue, | |
| 1332 | indexJson, | |
| 1333 | indexVersion, | |
| 1334 | config: getMiniSearchWorkerConfig() | |
| 1335 | }) | |
| 1336 | if (canceled) return | |
| 1337 | loadedWorkerIndexKey = workerIndexKey | |
| 1338 | } | |
| 1399 | await preloadSearchData(localeIndexValue, indexVersion, configKey) | |
| 1400 | if (canceled) return | |
| 1339 | 1401 | } catch (error) { |
| 1340 | 1402 | if (!canceled) { |
| 1341 | 1403 | console.error('[search] text search index preload failed', error) |
docs/.vitepress/theme/workers/searchWorker.ts +9
| @@ -52,10 +52,17 @@ interface UrlSearchRequest { | ||
| 52 | 52 | pageOrderEntries: [string, number][] |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | interface PreloadMetadataRequest { | |
| 56 | type: 'preload-metadata' | |
| 57 | id: number | |
| 58 | pageOrderEntries: [string, number][] | |
| 59 | } | |
| 60 | ||
| 55 | 61 | type SearchWorkerRequest = |
| 56 | 62 | | LoadIndexRequest |
| 57 | 63 | | TextSearchRequest |
| 58 | 64 | | UrlSearchRequest |
| 65 | | PreloadMetadataRequest | |
| 59 | 66 | |
| 60 | 67 | type WorkerSearchResult = SearchResult & Result |
| 61 | 68 | |
| @@ -391,6 +398,8 @@ self.onmessage = async (event: MessageEvent<SearchWorkerRequest>) => { | ||
| 391 | 398 | ) |
| 392 | 399 | : message.type === 'text-search' |
| 393 | 400 | ? await textSearch(message) |
| 401 | : message.type === 'preload-metadata' | |
| 402 | ? (setPageOrder(message.pageOrderEntries), await ensureSearchMetadata()) | |
| 394 | 403 | : await urlSearch(message) |
| 395 | 404 | self.postMessage({ |
| 396 | 405 | type: `${message.type}:success`, |