Skip to content

Commit ff72edc

Browse files
committed
fix duplicate head tag issue sveltejs#6463
1 parent ec3dbc9 commit ff72edc

File tree

2 files changed

+20
-1
lines changed
  • src
    • compiler/compile/render_dom/wrappers
    • runtime/internal

2 files changed

+20
-1
lines changed

src/compiler/compile/render_dom/wrappers/Head.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class HeadWrapper extends Wrapper {
3636
let nodes;
3737
if (this.renderer.options.hydratable && this.fragment.nodes.length) {
3838
nodes = block.get_unique_name('head_nodes');
39-
block.chunks.claim.push(b`const ${nodes} = @query_selector_all('[data-svelte="${this.node.id}"]', @_document.head);`);
39+
block.chunks.claim.push(b`const ${nodes} = @head_selector('${this.node.id}', @_document.head);`);
4040
}
4141

4242
this.fragment.render(block, x`@_document.head` as unknown as Identifier, nodes);

src/runtime/internal/dom.ts

+19
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,25 @@ export function query_selector_all(selector: string, parent: HTMLElement = docum
630630
return Array.from(parent.querySelectorAll(selector)) as ChildNodeArray;
631631
}
632632

633+
export function head_selector(nodeId: string, head: HTMLElement) {
634+
const result = [];
635+
let started = 0;
636+
for (const node of head.childNodes) {
637+
if (node.nodeType === 8 /* comment node */ && node.textContent.trim() === 'HTML_TAG_END') {
638+
started -= 1;
639+
result.push(node);
640+
} else if (node.nodeType === 8 /* comment node */ && node.textContent.trim() === 'HTML_TAG_START') {
641+
started += 1;
642+
result.push(node);
643+
} else if (started > 0) {
644+
result.push(node);
645+
} else if (node.nodeType === 1 /* element node */ && (<Element>node).getAttribute('data-svelte') === nodeId) {
646+
result.push(node);
647+
}
648+
}
649+
return result;
650+
}
651+
633652
export class HtmlTag {
634653
// parent for creating node
635654
e: HTMLElement;

0 commit comments

Comments
 (0)