Skip to content

Commit

Permalink
refactor(script)!: remove script:transform hook
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Mar 23, 2024
1 parent e8ca72c commit 1b1532e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 44 deletions.
1 change: 0 additions & 1 deletion packages/schema/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> }) => HookResult
'script:instance-fn': (ctx: { script: ScriptInstance<any>, fn: string | symbol, args: any }) => HookResult
}
5 changes: 0 additions & 5 deletions packages/schema/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ export interface UseScriptOptions<T> extends Omit<HeadEntryOptions, 'transform'>
* Stub the script instance. Useful for SSR or testing.
*/
stub?: ((ctx: { script: ScriptInstance<T>, fn: string | symbol }) => any)
/**
* Transform the script instance before it's resolved.
*/
transform?: (script: UseScriptResolvedInput) => Promise<UseScriptResolvedInput> | 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.
*/
Expand Down
41 changes: 3 additions & 38 deletions packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,7 @@ export function useScript<T>(_input: UseScriptInput, _options?: UseScriptOptions
if (head._scripts?.[id])
return head._scripts[id]

async function transform(entry: Head): Promise<Head> {
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<T> = {
const script = {
id,
status: 'awaitingLoad',
loaded: false,
Expand Down Expand Up @@ -128,20 +105,8 @@ export function useScript<T>(_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<void>(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')
Expand Down

0 comments on commit 1b1532e

Please sign in to comment.