From 09da8053ba94409ecd5dc313bb7dfcb9e8b9ad81 Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Fri, 27 Oct 2023 13:40:28 +0900 Subject: [PATCH] fix(useScript): support SSR idle trigger --- packages/unhead/src/composables/useScript.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/unhead/src/composables/useScript.ts b/packages/unhead/src/composables/useScript.ts index fcbd337f..db86fa64 100644 --- a/packages/unhead/src/composables/useScript.ts +++ b/packages/unhead/src/composables/useScript.ts @@ -107,7 +107,14 @@ export function useScript(input: UseScriptInput, _options?: UseScriptOptions< let trigger = options.trigger if (trigger) { const isIdle = trigger === 'idle' - isIdle && (trigger = new Promise(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(resolve => requestIdleCallback(() => resolve())) + } // never resolves trigger === 'manual' && (trigger = new Promise(() => {})) // check trigger is a promise