MCPcopy
hub / github.com/vercel/next.js / updateElements

Function updateElements

packages/next/src/client/head-manager.ts:53–100  ·  view source on GitHub ↗
(type: string, components: JSX.Element[])

Source from the content-addressed store, hash-verified

51}
52
53function 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
102export default function initHeadManager(): {
103 mountedInstances: Set<unknown>

Callers 1

initHeadManagerFunction · 0.85

Calls 10

reactElementToDOMFunction · 0.85
isEqualNodeFunction · 0.85
toLowerCaseMethod · 0.80
getAttributeMethod · 0.80
pushMethod · 0.65
addMethod · 0.45
setAttributeMethod · 0.45
deleteMethod · 0.45
removeChildMethod · 0.45
prependMethod · 0.45

Tested by

no test coverage detected