diff --git a/src/index.tsx b/src/index.tsx index a10da4e..ea0ccae 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -20,6 +20,8 @@ interface TagDescription { id: string; name?: string; ref?: Element; + /** Cascade key captured at add time so remove still finds the entry after reactive props change (#76). */ + key?: string; } export interface MetaContextType { @@ -34,7 +36,9 @@ const titleTagProperties: string[] = []; const metaTagProperties: string[] = // https://html.spec.whatwg.org/multipage/semantics.html#the-meta-element - ["name", "http-equiv", "content", "charset", "media"] + // `content` is the payload, not identity: including it made reactive content + // updates look like a different tag, so removeTag missed the cascade entry (#76). + ["name", "http-equiv", "charset", "media"] // additional properties .concat(["property"]); @@ -95,6 +99,7 @@ function initClientProvider() { if (cascadingTags.indexOf(tag.tag) !== -1) { const properties = tag.tag === "title" ? titleTagProperties : metaTagProperties; const tagKey = getTagKey(tag, properties); + tag.key = tagKey; // only cascading tags need to be kept as singletons if (!cascadedTagInstances.has(tagKey)) { @@ -145,7 +150,9 @@ function initClientProvider() { }, removeTag(tag: TagDescription, index: number) { const properties = tag.tag === "title" ? titleTagProperties : metaTagProperties; - const tagKey = getTagKey(tag, properties); + // Prefer the key captured at add time: reactive props may already have + // changed by the time createRenderEffect's cleanup runs (#76). + const tagKey = tag.key ?? getTagKey(tag, properties); if (tag.ref) { const t = cascadedTagInstances.get(tagKey); diff --git a/test/index.spec.tsx b/test/index.spec.tsx index 6487447..179d0fd 100644 --- a/test/index.spec.tsx +++ b/test/index.spec.tsx @@ -318,6 +318,37 @@ test("renders both meta with the same name/property but different other attribut dispose(); }); +// #76: reactive content must not change the cascade key, or removeTag misses +// the entry and repeated toggles eventually drop the tag from . +test("reactive Meta content updates in place across many toggles (#76)", () => { + let div = document.createElement("div"); + const [themeColor, setThemeColor] = createSignal("#fff"); + const dispose = render( + () => ( + + + + ), + div + ); + + expect(document.head.querySelectorAll('meta[name="theme-color"]').length).toBe(1); + expect(document.head.querySelector('meta[name="theme-color"]')?.getAttribute("content")).toBe( + "#fff" + ); + + for (let i = 0; i < 6; i++) { + setThemeColor(i % 2 === 0 ? "#000" : "#fff"); + expect(document.head.querySelectorAll('meta[name="theme-color"]').length).toBe(1); + expect(document.head.querySelector('meta[name="theme-color"]')?.getAttribute("content")).toBe( + i % 2 === 0 ? "#000" : "#fff" + ); + } + + dispose(); + expect(document.head.querySelectorAll('meta[name="theme-color"]').length).toBe(0); +}); + test("throws error if head tag is rendered without MetaProvider", () => { expect(() => { let div = document.createElement("div");