MCPcopy Create free account
hub / github.com/ThatGuySam/doesitarm / loadPagefindBrowserModule

Function loadPagefindBrowserModule

helpers/pagefind/browser.js:107–141  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

105}
106
107async 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
143export class PagefindClient {
144 constructor ( options = {} ) {

Callers 1

loadPagefindScriptMethod · 0.85

Calls 2

getPagefindModuleSourceFunction · 0.85
waitForPagefindGlobalFunction · 0.85

Tested by

no test coverage detected