From 1b1532e97f9c7d5c8d1dd0abc25da829bc13bf59 Mon Sep 17 00:00:00 2001 From: harlan Date: Sat, 23 Mar 2024 14:44:28 +1100 Subject: [PATCH] refactor(script)!: remove `script:transform` hook --- packages/schema/src/hooks.ts | 1 - packages/schema/src/script.ts | 5 --- packages/unhead/src/composables/useScript.ts | 41 ++------------------ 3 files changed, 3 insertions(+), 44 deletions(-) diff --git a/packages/schema/src/hooks.ts b/packages/schema/src/hooks.ts index d436acc1..76e0bc69 100644 --- a/packages/schema/src/hooks.ts +++ b/packages/schema/src/hooks.ts @@ -54,7 +54,6 @@ export interface HeadHooks { 'ssr:render': (ctx: { tags: HeadTag[] }) => HookResult 'ssr:rendered': (ctx: SSRRenderContext) => HookResult - 'script:transform': (ctx: { script: UseScriptResolvedInput }) => HookResult 'script:updated': (ctx: { script: ScriptInstance }) => HookResult 'script:instance-fn': (ctx: { script: ScriptInstance, fn: string | symbol, args: any }) => HookResult } diff --git a/packages/schema/src/script.ts b/packages/schema/src/script.ts index 8c3066aa..f0b876fa 100644 --- a/packages/schema/src/script.ts +++ b/packages/schema/src/script.ts @@ -34,13 +34,8 @@ export interface UseScriptOptions extends Omit * Stub the script instance. Useful for SSR or testing. */ stub?: ((ctx: { script: ScriptInstance, fn: string | symbol }) => any) - /** - * Transform the script instance before it's resolved. - */ - transform?: (script: UseScriptResolvedInput) => Promise | UseScriptResolvedInput /** * The trigger to load the script: - * - `idle` - Load the script when the browser is idle. * - `manual` - Load the script manually by calling `$script.load()` or `$script.waitForLoad()`. * - `Promise` - Load the script when the promise resolves. */ diff --git a/packages/unhead/src/composables/useScript.ts b/packages/unhead/src/composables/useScript.ts index dce16b4d..502f87f9 100644 --- a/packages/unhead/src/composables/useScript.ts +++ b/packages/unhead/src/composables/useScript.ts @@ -45,30 +45,7 @@ export function useScript(_input: UseScriptInput, _options?: UseScriptOptions if (head._scripts?.[id]) return head._scripts[id] - async function transform(entry: Head): Promise { - const script = await (options.transform || (input => input))(entry.script![0] as UseScriptResolvedInput) - const ctx = { script } - await head!.hooks.callHook('script:transform', ctx) - return { script: [ctx.script] } - } - - function maybeHintEarlyConnection(rel: 'preconnect' | 'dns-prefetch') { - if ( - // opt-out - options.skipEarlyConnections - // must be a valid absolute url - || !input.src.includes('//') - // must be server-side - || !head!.ssr - ) - return - const key = `use-script.${id}.early-connection` - head!.push({ - link: [{ key, rel, href: new URL(input.src).origin }], - }, { mode: 'server' }) - } - - const script: ScriptInstance = { + const script = { id, status: 'awaitingLoad', loaded: false, @@ -128,20 +105,8 @@ export function useScript(_input: UseScriptInput, _options?: UseScriptOptions } }) - let trigger = options.trigger - if (trigger) { - const isIdle = trigger === 'idle' - 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 + const trigger = options.trigger + if (options.trigger) trigger instanceof Promise && trigger.then(script.load) // if we're lazy it's likely it will load within the first 10 seconds, otherwise we just prefetch the DNS for a quicker load maybeHintEarlyConnection(isIdle ? 'preconnect' : 'dns-prefetch')