| | import { setAttributesFromProps } from './set-attributes-from-props' |
| |
|
| | import type { JSX } from 'react' |
| |
|
| | function reactElementToDOM({ type, props }: JSX.Element): HTMLElement { |
| | const el: HTMLElement = document.createElement(type) |
| | setAttributesFromProps(el, props) |
| |
|
| | const { children, dangerouslySetInnerHTML } = props |
| | if (dangerouslySetInnerHTML) { |
| | el.innerHTML = dangerouslySetInnerHTML.__html || '' |
| | } else if (children) { |
| | el.textContent = |
| | typeof children === 'string' |
| | ? children |
| | : Array.isArray(children) |
| | ? children.join('') |
| | : '' |
| | } |
| | return el |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export function isEqualNode(oldTag: Element, newTag: Element) { |
| | if (oldTag instanceof HTMLElement && newTag instanceof HTMLElement) { |
| | const nonce = newTag.getAttribute('nonce') |
| | |
| | |
| | if (nonce && !oldTag.getAttribute('nonce')) { |
| | const cloneTag = newTag.cloneNode(true) as typeof newTag |
| | cloneTag.setAttribute('nonce', '') |
| | cloneTag.nonce = nonce |
| | return nonce === oldTag.nonce && oldTag.isEqualNode(cloneTag) |
| | } |
| | } |
| |
|
| | return oldTag.isEqualNode(newTag) |
| | } |
| |
|
| | function updateElements(type: string, components: JSX.Element[]) { |
| | const headEl = document.querySelector('head') |
| | if (!headEl) return |
| |
|
| | const oldTags = new Set(headEl.querySelectorAll(`${type}[data-next-head]`)) |
| |
|
| | if (type === 'meta') { |
| | const metaCharset = headEl.querySelector('meta[charset]') |
| | if (metaCharset !== null) { |
| | oldTags.add(metaCharset) |
| | } |
| | } |
| |
|
| | const newTags: Element[] = [] |
| | for (let i = 0; i < components.length; i++) { |
| | const component = components[i] |
| | const newTag = reactElementToDOM(component) |
| | newTag.setAttribute('data-next-head', '') |
| |
|
| | let isNew = true |
| | for (const oldTag of oldTags) { |
| | if (isEqualNode(oldTag, newTag)) { |
| | oldTags.delete(oldTag) |
| | isNew = false |
| | break |
| | } |
| | } |
| |
|
| | if (isNew) { |
| | newTags.push(newTag) |
| | } |
| | } |
| |
|
| | for (const oldTag of oldTags) { |
| | oldTag.parentNode?.removeChild(oldTag) |
| | } |
| |
|
| | for (const newTag of newTags) { |
| | |
| | if ( |
| | newTag.tagName.toLowerCase() === 'meta' && |
| | newTag.getAttribute('charset') !== null |
| | ) { |
| | headEl.prepend(newTag) |
| | } |
| | headEl.appendChild(newTag) |
| | } |
| | } |
| |
|
| | export default function initHeadManager(): { |
| | mountedInstances: Set<unknown> |
| | updateHead: (head: JSX.Element[]) => void |
| | } { |
| | return { |
| | mountedInstances: new Set(), |
| | updateHead: (head: JSX.Element[]) => { |
| | const tags: Record<string, JSX.Element[]> = {} |
| |
|
| | head.forEach((h) => { |
| | if ( |
| | |
| | |
| | h.type === 'link' && |
| | h.props['data-optimized-fonts'] |
| | ) { |
| | if ( |
| | document.querySelector(`style[data-href="${h.props['data-href']}"]`) |
| | ) { |
| | return |
| | } else { |
| | h.props.href = h.props['data-href'] |
| | h.props['data-href'] = undefined |
| | } |
| | } |
| |
|
| | const components = tags[h.type] || [] |
| | components.push(h) |
| | tags[h.type] = components |
| | }) |
| |
|
| | const titleComponent = tags.title ? tags.title[0] : null |
| | let title = '' |
| | if (titleComponent) { |
| | const { children } = titleComponent.props |
| | title = |
| | typeof children === 'string' |
| | ? children |
| | : Array.isArray(children) |
| | ? children.join('') |
| | : '' |
| | } |
| | if (title !== document.title) document.title = title |
| | ;['meta', 'base', 'link', 'style', 'script'].forEach((type) => { |
| | updateElements(type, tags[type] || []) |
| | }) |
| | }, |
| | } |
| | } |
| |
|