Skip to content

Commit

Permalink
fix duplicate head tag issue sveltejs#6463
Browse files Browse the repository at this point in the history
  • Loading branch information
hbirler committed Jul 31, 2021
1 parent ec3dbc9 commit ff72edc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/compile/render_dom/wrappers/Head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class HeadWrapper extends Wrapper {
let nodes;
if (this.renderer.options.hydratable && this.fragment.nodes.length) {
nodes = block.get_unique_name('head_nodes');
block.chunks.claim.push(b`const ${nodes} = @query_selector_all('[data-svelte="${this.node.id}"]', @_document.head);`);
block.chunks.claim.push(b`const ${nodes} = @head_selector('${this.node.id}', @_document.head);`);
}

this.fragment.render(block, x`@_document.head` as unknown as Identifier, nodes);
Expand Down
19 changes: 19 additions & 0 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,25 @@ export function query_selector_all(selector: string, parent: HTMLElement = docum
return Array.from(parent.querySelectorAll(selector)) as ChildNodeArray;
}

export function head_selector(nodeId: string, head: HTMLElement) {
const result = [];
let started = 0;
for (const node of head.childNodes) {
if (node.nodeType === 8 /* comment node */ && node.textContent.trim() === 'HTML_TAG_END') {
started -= 1;
result.push(node);
} else if (node.nodeType === 8 /* comment node */ && node.textContent.trim() === 'HTML_TAG_START') {
started += 1;
result.push(node);
} else if (started > 0) {
result.push(node);
} else if (node.nodeType === 1 /* element node */ && (<Element>node).getAttribute('data-svelte') === nodeId) {
result.push(node);
}
}
return result;
}

export class HtmlTag {
// parent for creating node
e: HTMLElement;
Expand Down

0 comments on commit ff72edc

Please sign in to comment.