-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverse engineer next/script with lazyOnLoad strategy
- Loading branch information
1 parent
e4da93e
commit 3aceb74
Showing
4 changed files
with
102 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,57 @@ | ||
/** Reversed engineerd from next/script | ||
* https://github.com/vercel/next.js/blob/9f1d5d7fca55f909a3c1b60b1996e6c2124702a5/packages/next/client/script.tsx#L31 | ||
*/ | ||
|
||
import { ScriptHTMLAttributes, useEffect } from "react"; | ||
import { requestIdleCallback } from "./request-idle-callback"; | ||
|
||
const loadScript = (props: ScriptHTMLAttributes<HTMLScriptElement>): void => { | ||
const { dangerouslySetInnerHTML } = props; | ||
|
||
const el = document.createElement("script"); | ||
|
||
if (dangerouslySetInnerHTML) { | ||
el.innerHTML = dangerouslySetInnerHTML.__html || ""; | ||
} | ||
|
||
document.body.appendChild(el); | ||
}; | ||
|
||
type RequestIdleCallbackHandle = any; | ||
type RequestIdleCallbackOptions = { | ||
timeout: number; | ||
}; | ||
type RequestIdleCallbackDeadline = { | ||
readonly didTimeout: boolean; | ||
timeRemaining: () => number; | ||
}; | ||
|
||
declare global { | ||
interface Window { | ||
requestIdleCallback: ( | ||
callback: (deadline: RequestIdleCallbackDeadline) => void, | ||
opts?: RequestIdleCallbackOptions | ||
) => RequestIdleCallbackHandle; | ||
cancelIdleCallback: (handle: RequestIdleCallbackHandle) => void; | ||
} | ||
} | ||
|
||
function loadLazyScript(props: ScriptHTMLAttributes<HTMLScriptElement>) { | ||
if (document.readyState === "complete") { | ||
requestIdleCallback(() => loadScript(props)); | ||
} else { | ||
window.addEventListener("load", () => { | ||
requestIdleCallback(() => loadScript(props)); | ||
}); | ||
} | ||
} | ||
|
||
export function Script( | ||
props: ScriptHTMLAttributes<HTMLScriptElement> | ||
): JSX.Element | null { | ||
useEffect(() => { | ||
loadLazyScript(props); | ||
}, [props]); | ||
|
||
return null; | ||
} |
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,42 @@ | ||
/** From https://github.com/vercel/next.js/blob/0af3b526408bae26d6b3f8cab75c4229998bf7cb/packages/next/client/request-idle-callback.ts */ | ||
|
||
type RequestIdleCallbackHandle = any; | ||
type RequestIdleCallbackOptions = { | ||
timeout: number; | ||
}; | ||
type RequestIdleCallbackDeadline = { | ||
readonly didTimeout: boolean; | ||
timeRemaining: () => number; | ||
}; | ||
|
||
declare global { | ||
interface Window { | ||
requestIdleCallback: ( | ||
callback: (deadline: RequestIdleCallbackDeadline) => void, | ||
opts?: RequestIdleCallbackOptions | ||
) => RequestIdleCallbackHandle; | ||
cancelIdleCallback: (id: RequestIdleCallbackHandle) => void; | ||
} | ||
} | ||
|
||
export const requestIdleCallback = | ||
(typeof self !== "undefined" && self.requestIdleCallback) || | ||
function ( | ||
cb: (deadline: RequestIdleCallbackDeadline) => void | ||
): NodeJS.Timeout { | ||
let start = Date.now(); | ||
return setTimeout(function () { | ||
cb({ | ||
didTimeout: false, | ||
timeRemaining: function () { | ||
return Math.max(0, 50 - (Date.now() - start)); | ||
}, | ||
}); | ||
}, 1); | ||
}; | ||
|
||
export const cancelIdleCallback = | ||
(typeof self !== "undefined" && self.cancelIdleCallback) || | ||
function (id: RequestIdleCallbackHandle) { | ||
return clearTimeout(id); | ||
}; |
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
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