Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"]);

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <head>.
test("reactive Meta content updates in place across many toggles (#76)", () => {
let div = document.createElement("div");
const [themeColor, setThemeColor] = createSignal("#fff");
const dispose = render(
() => (
<MetaProvider>
<Meta name="theme-color" content={themeColor()} />
</MetaProvider>
),
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");
Expand Down