@@ -6,6 +6,10 @@ import {
66 pagefindScriptURL
77} from '~/helpers/pagefind/config.js'
88
9+ const pagefindGlobalKey = '__doesItArmPagefind'
10+ const pagefindLoaderPromiseKey = '__doesItArmPagefindLoaderPromise'
11+ const pagefindLoaderTimeoutMs = 10 * 1000
12+
913function escapeHtml ( text = '' ) {
1014 return text
1115 . replaceAll ( '&' , '&' )
@@ -70,6 +74,72 @@ export function mapPagefindDataToListing ( resultData, {
7074 }
7175}
7276
77+ function getPagefindModuleSource ( ) {
78+ return [
79+ `import * as pagefindModule from ${ JSON . stringify ( pagefindScriptURL ) } ` ,
80+ `globalThis.${ pagefindGlobalKey } = pagefindModule.default || pagefindModule`
81+ ] . join ( '\n' )
82+ }
83+
84+ function waitForPagefindGlobal ( ) {
85+ return new Promise ( ( resolve , reject ) => {
86+ if ( globalThis [ pagefindGlobalKey ] ) {
87+ resolve ( globalThis [ pagefindGlobalKey ] )
88+ return
89+ }
90+
91+ const startedAt = Date . now ( )
92+ const timer = setInterval ( ( ) => {
93+ if ( globalThis [ pagefindGlobalKey ] ) {
94+ clearInterval ( timer )
95+ resolve ( globalThis [ pagefindGlobalKey ] )
96+ return
97+ }
98+
99+ if ( Date . now ( ) - startedAt >= pagefindLoaderTimeoutMs ) {
100+ clearInterval ( timer )
101+ reject ( new Error ( `Timed out waiting for Pagefind browser module at ${ pagefindScriptURL } ` ) )
102+ }
103+ } , 20 )
104+ } )
105+ }
106+
107+ async function loadPagefindBrowserModule ( ) {
108+ if ( typeof document === 'undefined' ) {
109+ throw new Error ( 'PagefindClient can only load in a browser document' )
110+ }
111+
112+ if ( globalThis [ pagefindGlobalKey ] ) {
113+ return globalThis [ pagefindGlobalKey ]
114+ }
115+
116+ if ( ! globalThis [ pagefindLoaderPromiseKey ] ) {
117+ globalThis [ pagefindLoaderPromiseKey ] = new Promise ( async ( resolve , reject ) => {
118+ const script = document . createElement ( 'script' )
119+
120+ script . async = true
121+ script . type = 'module'
122+ script . textContent = getPagefindModuleSource ( )
123+
124+ script . onerror = ( ) => {
125+ delete globalThis [ pagefindLoaderPromiseKey ]
126+ reject ( new Error ( `Failed to load Pagefind browser module from ${ pagefindScriptURL } ` ) )
127+ }
128+
129+ document . head . append ( script )
130+
131+ try {
132+ resolve ( await waitForPagefindGlobal ( ) )
133+ } catch ( err ) {
134+ delete globalThis [ pagefindLoaderPromiseKey ]
135+ reject ( err )
136+ }
137+ } )
138+ }
139+
140+ return await globalThis [ pagefindLoaderPromiseKey ]
141+ }
142+
73143export class PagefindClient {
74144 constructor ( options = { } ) {
75145 this . bundlePath = options . bundlePath || pagefindBundleRelativeURL
@@ -100,7 +170,7 @@ export class PagefindClient {
100170 async loadPagefindScript ( ) {
101171 if ( this . pagefind ) return
102172
103- const pagefindModule = await import ( /* @vite -ignore */ pagefindScriptURL )
173+ const pagefindModule = await loadPagefindBrowserModule ( )
104174 this . pagefind = pagefindModule . default || pagefindModule
105175
106176 this . pagefind . options ( {
0 commit comments