Skip to content

Commit 970fe23

Browse files
authored
fix(useScriptTag): unload should remove script tag (#1114)
1 parent cb1654b commit 970fe23

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

packages/core/useScriptTag/index.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ describe('useScriptTag', () => {
2727
expect(scriptTagElement()).toBeInstanceOf(HTMLScriptElement)
2828
})
2929

30+
it('should re-use the same src for multiple loads', async() => {
31+
const addChildListener = vitest.spyOn(document.head, 'appendChild')
32+
33+
expect(addChildListener).not.toBeCalled()
34+
35+
expect(scriptTagElement()).toBeNull()
36+
37+
const vm = useSetup(() => {
38+
const script1 = useScriptTag(src, () => {}, { immediate: false, manual: true })
39+
const script2 = useScriptTag(src, () => {}, { immediate: false, manual: true })
40+
41+
return {
42+
script1,
43+
script2,
44+
}
45+
})
46+
47+
await vm.script1.load(false)
48+
await vm.script2.load(false)
49+
50+
expect(vm.script1.scriptTag.value).not.toBeNull()
51+
expect(vm.script2.scriptTag.value).not.toBeNull()
52+
53+
expect(addChildListener).toBeCalledTimes(1)
54+
expect(scriptTagElement()).toBeInstanceOf(HTMLScriptElement)
55+
})
56+
3057
it('should remove script tag on unmount', async() => {
3158
const removeChildListener = vitest.spyOn(document.head, 'removeChild')
3259

@@ -90,4 +117,36 @@ describe('useScriptTag', () => {
90117

91118
expect(vm.scriptTag).toBeNull()
92119
})
120+
121+
it('should remove script tag on unload call after multiple loads', async() => {
122+
const removeChildListener = vitest.spyOn(document.head, 'removeChild')
123+
124+
expect(removeChildListener).not.toBeCalled()
125+
126+
expect(scriptTagElement()).toBeNull()
127+
128+
const vm = useSetup(() => {
129+
const script1 = useScriptTag(src, () => {}, { immediate: false, manual: true })
130+
const script2 = useScriptTag(src, () => {}, { immediate: false, manual: true })
131+
132+
return {
133+
script1,
134+
script2,
135+
}
136+
})
137+
138+
// Multiple Loads
139+
await vm.script1.load(false)
140+
await vm.script2.load(false)
141+
142+
expect(scriptTagElement()).toBeInstanceOf(HTMLScriptElement)
143+
144+
vm.script1.unload()
145+
vm.script2.unload()
146+
147+
expect(vm.script1.scriptTag.value).toBeNull()
148+
expect(vm.script2.scriptTag.value).toBeNull()
149+
expect(removeChildListener).toBeCalledTimes(1)
150+
expect(scriptTagElement()).toBeNull()
151+
})
93152
})

packages/core/useScriptTag/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,12 @@ export function useScriptTag(
156156

157157
_promise = null
158158

159-
if (scriptTag.value) {
160-
document.head.removeChild(scriptTag.value)
159+
if (scriptTag.value)
161160
scriptTag.value = null
162-
}
161+
162+
const el = document.querySelector(`script[src="${src}"]`) as HTMLScriptElement
163+
if (el)
164+
document.head.removeChild(el)
163165
}
164166

165167
if (immediate && !manual)

0 commit comments

Comments
 (0)