From 6a9b4e188f2abe1ea19e002347b4f4899aad07ec Mon Sep 17 00:00:00 2001 From: harlan Date: Sun, 8 Dec 2024 13:26:17 +1100 Subject: [PATCH] fix(useScript): allow `load()` to be called again once `remove()` is called --- packages/unhead/src/composables/useScript.ts | 1 + test/unhead/dom/useScript.test.ts | 28 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/packages/unhead/src/composables/useScript.ts b/packages/unhead/src/composables/useScript.ts index 38846342..2ab51ec8 100644 --- a/packages/unhead/src/composables/useScript.ts +++ b/packages/unhead/src/composables/useScript.ts @@ -109,6 +109,7 @@ export function useScript = Record { expect(await instance.proxy.test('hello-world')).toEqual('hello-world') }) + it('remove & re-add', async () => { + useDOMHead() + + const instance = useScript<{ test: (foo: string) => void }>({ + src: 'https://cdn.example.com/script.js', + }) + + let dom = await useDelayedSerializedDom() + expect(dom.split('\n').filter(l => l.trim().startsWith('", + ] + `) + instance.remove() + // wait + await new Promise((r) => setTimeout(r, 100)) + dom = await useDelayedSerializedDom() + expect(dom.split('\n').filter(l => l.trim().startsWith(' setTimeout(r, 100)) + dom = await useDelayedSerializedDom() + expect(dom.split('\n').filter(l => l.trim().startsWith('", + ] + `) + }) })