Skip to content

Commit

Permalink
Use module preload when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinBrett committed Dec 6, 2024
1 parent ba09ed0 commit e0a7939
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,22 @@ const getPreloadedLinks = (): HTMLLinkElement[] => [
...document.querySelectorAll<HTMLLinkElement>("link[rel=preload]"),
];

let HAS_MODULE_PRELOAD_SUPPORT = false;

const supportsModulePreload = (): boolean => {
if (HAS_MODULE_PRELOAD_SUPPORT) return true;

try {
HAS_MODULE_PRELOAD_SUPPORT = Boolean(
document.createElement("link").relList?.supports?.("modulepreload")
);
} catch {
// Ignore failure to check for modulepreload support
}

return HAS_MODULE_PRELOAD_SUPPORT;
};

let HAS_WEBP_SUPPORT = false;

export const supportsWebp = (): boolean => {
Expand Down Expand Up @@ -958,6 +974,11 @@ export const preloadLibs = (libs: string[] = []): void => {
case ".html":
link.rel = "prerender";
break;
case ".js":
if (supportsModulePreload()) {
link.rel = "modulepreload";
}
break;
case ".json":
case ".wasm":
link.as = "fetch";
Expand Down

0 comments on commit e0a7939

Please sign in to comment.