Skip to content

Commit 2f652ff

Browse files
committed
fix(scripts): trigger onload event for inlineHTML
1 parent 87b8075 commit 2f652ff

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

packages/unhead/src/composables/useScript.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NetworkEvents, hashCode } from '@unhead/shared'
2-
import type { Head, HeadEntryOptions, Script, ScriptInstance, UseScriptInput, UseScriptOptions } from '@unhead/schema'
2+
import type { DomRenderTagContext, Head, HeadEntryOptions, Script, ScriptInstance, UseScriptInput, UseScriptOptions } from '@unhead/schema'
33
import { getActiveHead } from './useActiveHead'
44

55
export function useScript<T>(input: UseScriptInput, _options?: UseScriptOptions<T>): T & { $script: ScriptInstance<T> } {
@@ -83,6 +83,17 @@ export function useScript<T>(input: UseScriptInput, _options?: UseScriptOptions<
8383
}
8484
})
8585

86+
function resolveInnerHtmlLoad(ctx: DomRenderTagContext) {
87+
if (ctx.tag.key === key) {
88+
// trigger load event
89+
script.status = 'loaded'
90+
head!.hooks.callHook('script:loaded', hookCtx)
91+
typeof input.onload === 'function' && input.onload(new Event('load'))
92+
head!.hooks.removeHook('dom:renderTag', resolveInnerHtmlLoad)
93+
}
94+
}
95+
input.innerHTML && head.hooks.hook('dom:renderTag', resolveInnerHtmlLoad)
96+
8697
// 3. Proxy the script API
8798
const instance = new Proxy({}, {
8899
get(_, fn) {

0 commit comments

Comments
 (0)