Skip to content

Commit

Permalink
fix(useScript): support SSR idle trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 27, 2023
1 parent 8868fe0 commit 09da805
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ export function useScript<T>(input: UseScriptInput, _options?: UseScriptOptions<
let trigger = options.trigger
if (trigger) {
const isIdle = trigger === 'idle'
isIdle && (trigger = new Promise<void>(resolve => requestIdleCallback(() => resolve())))
if (isIdle) {
// we don't need idle trigger for server
if (head.ssr) {
trigger = 'manual'
} else
// won't work in a SSR environment
trigger = new Promise<void>(resolve => requestIdleCallback(() => resolve()))
}
// never resolves
trigger === 'manual' && (trigger = new Promise(() => {}))
// check trigger is a promise
Expand Down

0 comments on commit 09da805

Please sign in to comment.