-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from kunai-consulting/module-preload
module-preload
- Loading branch information
Showing
2 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { component$, sync$, useOnWindow } from '@builder.io/qwik'; | ||
|
||
export const ModulePreload = component$(() => { | ||
useOnWindow( | ||
'load', | ||
sync$(async () => { | ||
// for safari support | ||
if (!window.requestIdleCallback) { | ||
window.requestIdleCallback = function ( | ||
callback: IdleRequestCallback, | ||
options?: IdleRequestOptions, | ||
): number { | ||
const opts = options || {}; | ||
const relaxation = 1; | ||
const timeout = opts.timeout || relaxation; | ||
const start = performance.now(); | ||
return setTimeout(function () { | ||
callback({ | ||
get didTimeout() { | ||
return opts.timeout | ||
? false | ||
: performance.now() - start - relaxation > timeout; | ||
}, | ||
timeRemaining: function () { | ||
return Math.max(0, relaxation + (performance.now() - start)); | ||
}, | ||
}); | ||
}, relaxation) as unknown as number; | ||
}; | ||
} | ||
|
||
const startPreloading = async () => { | ||
const qChunks = new Set<string>(); | ||
|
||
// Check prefetch bundles | ||
const prefetchScript = document.querySelector('script[q\\:type="prefetch-bundles"]'); | ||
if (prefetchScript?.textContent) { | ||
const content = prefetchScript.textContent; | ||
const match = content.match(/\["prefetch","\/build\/","(.*?)"\]/); | ||
if (match && match[1]) { | ||
match[1].split('","').forEach((chunk) => { | ||
if (chunk.startsWith('q-')) { | ||
qChunks.add(chunk); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
// Check qwik/json script | ||
const qwikJson = document.querySelector('script[type="qwik/json"]'); | ||
if (qwikJson?.textContent) { | ||
const matches = qwikJson.textContent.match(/q-[A-Za-z0-9_-]+\.js/g); | ||
if (matches) { | ||
matches.forEach(chunk => qChunks.add(chunk)); | ||
} | ||
} | ||
|
||
qChunks.forEach((chunk) => { | ||
const link = document.createElement('link'); | ||
link.rel = 'modulepreload'; | ||
link.href = `/build/${chunk}`; | ||
link.fetchPriority = 'low'; | ||
document.head.appendChild(link); | ||
}); | ||
}; | ||
|
||
await requestIdleCallback(await startPreloading); | ||
}), | ||
); | ||
|
||
return <></>; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters