()
| 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 | |
| 143 | export class PagefindClient { |
| 144 | constructor ( options = {} ) { |
no test coverage detected