@@ -18,7 +18,7 @@ import { OpenInMyLangQuickJump } from "./LanguageRecommendation";
18
18
export const SiteNav = ( props : Props ) => {
19
19
const i = createInternational < typeof navCopy > ( useIntl ( ) )
20
20
const IntlLink = createIntlLink ( props . lang )
21
- const loadDocSearch = ( ) => {
21
+ const loadDocSearch = ( docsearch ) => {
22
22
const fixURL = ( url : string ) => {
23
23
const u = new URL ( url ) ;
24
24
if ( u . host === document . location . host ) return url ;
@@ -47,26 +47,35 @@ export const SiteNav = (props: Props) => {
47
47
useEffect ( ( ) => {
48
48
setupStickyNavigation ( )
49
49
50
- // @ts -ignore - this comes from the script above
51
- if ( window . docsearch ) {
52
- loadDocSearch ( ) ;
53
- }
54
- if ( document . getElementById ( "algolia-search" ) ) return
55
-
56
50
const searchScript = document . createElement ( 'script' ) ;
57
51
searchScript . id = "algolia-search"
58
- const searchCSS = document . createElement ( 'link' ) ;
59
52
60
53
searchScript . src = withPrefix ( "/js/docsearch.js" ) ;
61
54
searchScript . async = true ;
62
- searchScript . onload = ( ) => {
63
- // @ts -ignore - this comes from the script above
64
- if ( window . docsearch ) {
65
- loadDocSearch ( ) ;
55
+ searchScript . onload = async ( ) => {
56
+ // @ts -ignore this comes from the script above
57
+ let universalDocSearch = window . docsearch ;
58
+
59
+ if ( global . require ) {
60
+ universalDocSearch = await new Promise ( resolve => {
61
+ const re : any = global . require ;
62
+ re ( [ '/js/docsearch.js' ] , ( docsearch ) => {
63
+ resolve ( docsearch ) ;
64
+ } ) ;
65
+ } ) ;
66
+ }
67
+
68
+ if ( universalDocSearch ) {
69
+ loadDocSearch ( universalDocSearch ) ;
70
+ }
71
+
72
+ if ( ! document . querySelector ( "#docsearch-css" ) ) {
73
+ const searchCSS = document . createElement ( 'link' ) ;
66
74
67
75
searchCSS . rel = 'stylesheet' ;
68
76
searchCSS . href = withPrefix ( '/css/docsearch.css' ) ;
69
77
searchCSS . type = 'text/css' ;
78
+ searchCSS . id = 'docsearch-css' ;
70
79
document . body . appendChild ( searchCSS ) ;
71
80
72
81
document . getElementById ( "search-form" ) ?. classList . add ( "search-enabled" )
0 commit comments