|
| 1 | +/** |
| 2 | + * Helper to update only the necessary tags in the head. |
| 3 | + * |
| 4 | + * @async |
| 5 | + * @param {Array} newHead The head elements of the new page. |
| 6 | + * |
| 7 | + */ |
| 8 | +export const updateHead = async ( newHead ) => { |
| 9 | + // Helper to get the tag id store in the cache. |
| 10 | + const getTagId = ( tag ) => tag.id || tag.outerHTML; |
| 11 | + |
| 12 | + // Map incoming head tags by their content. |
| 13 | + const newHeadMap = new Map(); |
| 14 | + for ( const child of newHead ) { |
| 15 | + newHeadMap.set( getTagId( child ), child ); |
| 16 | + } |
| 17 | + |
| 18 | + const toRemove = []; |
| 19 | + |
| 20 | + // Detect nodes that should be added or removed. |
| 21 | + for ( const child of document.head.children ) { |
| 22 | + const id = getTagId( child ); |
| 23 | + // Always remove styles and links as they might change. |
| 24 | + if ( child.nodeName === 'LINK' || child.nodeName === 'STYLE' ) |
| 25 | + toRemove.push( child ); |
| 26 | + else if ( newHeadMap.has( id ) ) newHeadMap.delete( id ); |
| 27 | + else if ( child.nodeName !== 'SCRIPT' && child.nodeName !== 'META' ) |
| 28 | + toRemove.push( child ); |
| 29 | + } |
| 30 | + |
| 31 | + // Prepare new assets. |
| 32 | + const toAppend = [ ...newHeadMap.values() ]; |
| 33 | + |
| 34 | + // Apply the changes. |
| 35 | + toRemove.forEach( ( n ) => n.remove() ); |
| 36 | + document.head.append( ...toAppend ); |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Fetches and processes head assets (stylesheets and scripts) from a specified document. |
| 41 | + * |
| 42 | + * @async |
| 43 | + * @param {Document} doc The document from which to fetch head assets. It should support standard DOM querying methods. |
| 44 | + * @param {Map} headElements A map of head elements to modify tracking the URLs of already processed assets to avoid duplicates. |
| 45 | + * |
| 46 | + * @return {Promise<HTMLElement[]>} Returns an array of HTML elements representing the head assets. |
| 47 | + */ |
| 48 | +export const fetchHeadAssets = async ( doc, headElements ) => { |
| 49 | + const headTags = []; |
| 50 | + const assets = [ |
| 51 | + { |
| 52 | + tagName: 'style', |
| 53 | + selector: 'link[rel=stylesheet]', |
| 54 | + attribute: 'href', |
| 55 | + }, |
| 56 | + { tagName: 'script', selector: 'script[src]', attribute: 'src' }, |
| 57 | + ]; |
| 58 | + for ( const asset of assets ) { |
| 59 | + const { tagName, selector, attribute } = asset; |
| 60 | + const tags = doc.querySelectorAll( selector ); |
| 61 | + |
| 62 | + // Use Promise.all to wait for fetch to complete |
| 63 | + await Promise.all( |
| 64 | + Array.from( tags ).map( async ( tag ) => { |
| 65 | + const attributeValue = tag.getAttribute( attribute ); |
| 66 | + if ( ! headElements.has( attributeValue ) ) { |
| 67 | + try { |
| 68 | + const response = await fetch( attributeValue ); |
| 69 | + const text = await response.text(); |
| 70 | + headElements.set( attributeValue, { |
| 71 | + tag, |
| 72 | + text, |
| 73 | + } ); |
| 74 | + } catch ( e ) { |
| 75 | + // eslint-disable-next-line no-console |
| 76 | + console.error( e ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const headElement = headElements.get( attributeValue ); |
| 81 | + const element = doc.createElement( tagName ); |
| 82 | + element.innerText = headElement.text; |
| 83 | + for ( const attr of headElement.tag.attributes ) { |
| 84 | + element.setAttribute( attr.name, attr.value ); |
| 85 | + } |
| 86 | + headTags.push( element ); |
| 87 | + } ) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return [ |
| 92 | + doc.querySelector( 'title' ), |
| 93 | + ...doc.querySelectorAll( 'style' ), |
| 94 | + ...headTags, |
| 95 | + ]; |
| 96 | +}; |
0 commit comments