Skip to content

Commit 071a61c

Browse files
committed
fix: avoid algoliasearch async import build errors
1 parent 7ac1ae9 commit 071a61c

File tree

3 files changed

+77
-87
lines changed

3 files changed

+77
-87
lines changed

docs/.vitepress/theme/components/AlgoliaSearchBox.vue

Lines changed: 72 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { AlgoliaSearchOptions } from 'algoliasearch'
3-
// import docsearch from '@docsearch/js'
4-
// import '@docsearch/css'
3+
import docsearch from '@docsearch/js'
4+
import '@docsearch/css'
55
import { useRoute, useRouter } from 'vitepress'
66
import { getCurrentInstance, onMounted, PropType, watch } from 'vue'
77
@@ -53,89 +53,79 @@ export default {
5353
}
5454
5555
function initialize(userOptions: any) {
56-
Promise.all([
57-
import('@docsearch/js'),
58-
import('@docsearch/css'),
59-
// import(/* webpackChunkName: "docsearch" */ '@docsearch/js'),
60-
// Promise.resolve(docsearch),
61-
// import(/* webpackChunkName: "docsearch" */ '@docsearch/css'),
62-
]).then(([docsearch]) => {
63-
docsearch = docsearch.default
64-
65-
docsearch(
66-
Object.assign({}, userOptions, {
67-
container: '#docsearch',
68-
// #697 Make DocSearch work well in i18n mode.
69-
searchParameters: Object.assign(
70-
{},
71-
// lang && {
72-
// facetFilters: [`lang:${lang}`].concat(
73-
// userOptions.facetFilters || []
74-
// )
75-
// },
76-
userOptions.searchParameters
77-
),
78-
navigator: {
79-
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
80-
const { pathname: hitPathname } = new URL(
81-
window.location.origin + suggestionUrl
82-
)
83-
84-
// Vue Router doesn't handle same-page navigation so we use
85-
// the native browser location API for anchor navigation.
86-
if (route.path === hitPathname) {
87-
window.location.assign(window.location.origin + suggestionUrl)
88-
} else {
89-
router.go(suggestionUrl)
90-
}
91-
},
56+
docsearch(
57+
Object.assign({}, userOptions, {
58+
container: '#docsearch',
59+
// #697 Make DocSearch work well in i18n mode.
60+
searchParameters: Object.assign(
61+
{},
62+
// lang && {
63+
// facetFilters: [`lang:${lang}`].concat(
64+
// userOptions.facetFilters || []
65+
// )
66+
// },
67+
userOptions.searchParameters
68+
),
69+
navigator: {
70+
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
71+
const { pathname: hitPathname } = new URL(
72+
window.location.origin + suggestionUrl
73+
)
74+
75+
// Vue Router doesn't handle same-page navigation so we use
76+
// the native browser location API for anchor navigation.
77+
if (route.path === hitPathname) {
78+
window.location.assign(window.location.origin + suggestionUrl)
79+
} else {
80+
router.go(suggestionUrl)
81+
}
9282
},
93-
transformItems: items => {
94-
return items.map(item => {
95-
return Object.assign({}, item, {
96-
url: getRelativePath(item.url),
97-
})
83+
},
84+
transformItems: items => {
85+
return items.map(item => {
86+
return Object.assign({}, item, {
87+
url: getRelativePath(item.url),
9888
})
99-
},
100-
hitComponent: ({ hit, children }) => {
101-
const relativeHit = hit.url.startsWith('http')
102-
? getRelativePath(hit.url as string)
103-
: hit.url
104-
105-
return {
106-
type: 'a',
107-
ref: undefined,
108-
constructor: undefined,
109-
key: undefined,
110-
props: {
111-
href: hit.url,
112-
onClick: (event: MouseEvent) => {
113-
if (isSpecialClick(event)) {
114-
return
115-
}
116-
117-
// We rely on the native link scrolling when user is
118-
// already on the right anchor because Vue Router doesn't
119-
// support duplicated history entries.
120-
if (route.path === relativeHit) {
121-
return
122-
}
123-
124-
// If the hits goes to another page, we prevent the native link behavior
125-
// to leverage the Vue Router loading feature.
126-
if (route.path !== relativeHit) {
127-
event.preventDefault()
128-
}
129-
130-
router.go(relativeHit)
131-
},
132-
children,
89+
})
90+
},
91+
hitComponent: ({ hit, children }) => {
92+
const relativeHit = hit.url.startsWith('http')
93+
? getRelativePath(hit.url as string)
94+
: hit.url
95+
96+
return {
97+
type: 'a',
98+
ref: undefined,
99+
constructor: undefined,
100+
key: undefined,
101+
props: {
102+
href: hit.url,
103+
onClick: (event: MouseEvent) => {
104+
if (isSpecialClick(event)) {
105+
return
106+
}
107+
108+
// We rely on the native link scrolling when user is
109+
// already on the right anchor because Vue Router doesn't
110+
// support duplicated history entries.
111+
if (route.path === relativeHit) {
112+
return
113+
}
114+
115+
// If the hits goes to another page, we prevent the native link behavior
116+
// to leverage the Vue Router loading feature.
117+
if (route.path !== relativeHit) {
118+
event.preventDefault()
119+
}
120+
121+
router.go(relativeHit)
133122
},
134-
}
135-
},
136-
})
137-
)
138-
})
123+
children,
124+
},
125+
}
126+
},
127+
})
128+
)
139129
}
140130
141131
onMounted(() => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"ts-jest": "^26.4.0",
122122
"typescript": "^4.1.2",
123123
"typescript-eslint-language-service": "^4.1.2",
124-
"vitepress": "^0.11.0",
124+
"vitepress": "^0.11.3",
125125
"vue": "^3.0.0"
126126
},
127127
"engines": {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9849,10 +9849,10 @@ vite@^2.0.0-beta.21:
98499849
optionalDependencies:
98509850
fsevents "~2.1.2"
98519851

9852-
vitepress@^0.11.0:
9853-
version "0.11.1"
9854-
resolved "https://registry.yarnpkg.com/vitepress/-/vitepress-0.11.1.tgz#1eee2dac8d3aad8d488558ddbc2367e33b2a2a3f"
9855-
integrity sha512-ThjTFNQjlVcVz0UNv1zV6fs1cYs8h/xNWmzrbF08gqKtE6vTSRVEb9SVlIXq1LVt13/0+t5XyddEURi5wo1Big==
9852+
vitepress@^0.11.3:
9853+
version "0.11.3"
9854+
resolved "https://registry.yarnpkg.com/vitepress/-/vitepress-0.11.3.tgz#bcc1b06792a3786175f72a1735ea4dc724d14c85"
9855+
integrity sha512-9WaZzlJgmOYXlMoTGaROP1npCp1A6V6BYB2nbYucsID0aTrm79p18Tdg2OEjG8X3iAOgN2PX5c1IXmrUneycqA==
98569856
dependencies:
98579857
"@docsearch/css" "^1.0.0-alpha.28"
98589858
"@docsearch/js" "^1.0.0-alpha.28"

0 commit comments

Comments
 (0)