(type: string, components: JSX.Element[])
| 51 | } |
| 52 | |
| 53 | function updateElements(type: string, components: JSX.Element[]) { |
| 54 | const headEl = document.querySelector('head') |
| 55 | if (!headEl) return |
| 56 | |
| 57 | const oldTags = new Set(headEl.querySelectorAll(`${type}[data-next-head]`)) |
| 58 | |
| 59 | if (type === 'meta') { |
| 60 | const metaCharset = headEl.querySelector('meta[charset]') |
| 61 | if (metaCharset !== null) { |
| 62 | oldTags.add(metaCharset) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const newTags: Element[] = [] |
| 67 | for (let i = 0; i < components.length; i++) { |
| 68 | const component = components[i] |
| 69 | const newTag = reactElementToDOM(component) |
| 70 | newTag.setAttribute('data-next-head', '') |
| 71 | |
| 72 | let isNew = true |
| 73 | for (const oldTag of oldTags) { |
| 74 | if (isEqualNode(oldTag, newTag)) { |
| 75 | oldTags.delete(oldTag) |
| 76 | isNew = false |
| 77 | break |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (isNew) { |
| 82 | newTags.push(newTag) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | for (const oldTag of oldTags) { |
| 87 | oldTag.parentNode?.removeChild(oldTag) |
| 88 | } |
| 89 | |
| 90 | for (const newTag of newTags) { |
| 91 | // meta[charset] must be first element so special case |
| 92 | if ( |
| 93 | newTag.tagName.toLowerCase() === 'meta' && |
| 94 | newTag.getAttribute('charset') !== null |
| 95 | ) { |
| 96 | headEl.prepend(newTag) |
| 97 | } |
| 98 | headEl.appendChild(newTag) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | export default function initHeadManager(): { |
| 103 | mountedInstances: Set<unknown> |
no test coverage detected