(mutationList: MutationRecord[])
| 2 | // TODO-APP: Remove this logic when Float has GC built-in in development. |
| 3 | if (process.env.NODE_ENV !== 'production') { |
| 4 | const callback = (mutationList: MutationRecord[]) => { |
| 5 | for (const mutation of mutationList) { |
| 6 | if (mutation.type === 'childList') { |
| 7 | for (const node of mutation.addedNodes) { |
| 8 | if ( |
| 9 | 'tagName' in node && |
| 10 | (node as HTMLLinkElement).tagName === 'LINK' |
| 11 | ) { |
| 12 | const link = node as HTMLLinkElement |
| 13 | if (link.dataset.precedence?.startsWith('next')) { |
| 14 | const href = link.getAttribute('href') |
| 15 | if (href) { |
| 16 | const [resource, version] = href.split('?v=', 2) |
| 17 | if (version) { |
| 18 | const currentOrigin = window.location.origin |
| 19 | const allLinks = [ |
| 20 | ...document.querySelectorAll( |
| 21 | 'link[href^="' + resource + '"]' |
| 22 | ), |
| 23 | // It's possible that the resource is a full URL or only pathname, |
| 24 | // so we need to remove the alternative href as well. |
| 25 | ...document.querySelectorAll( |
| 26 | 'link[href^="' + |
| 27 | (resource.startsWith(currentOrigin) |
| 28 | ? resource.slice(currentOrigin.length) |
| 29 | : currentOrigin + resource) + |
| 30 | '"]' |
| 31 | ), |
| 32 | ] as HTMLLinkElement[] |
| 33 | |
| 34 | for (const otherLink of allLinks) { |
| 35 | if (otherLink.dataset.precedence?.startsWith('next')) { |
| 36 | const otherHref = otherLink.getAttribute('href') |
| 37 | if (otherHref) { |
| 38 | const [, otherVersion] = otherHref.split('?v=', 2) |
| 39 | if (!otherVersion || +otherVersion < +version) { |
| 40 | // Delay the removal of the stylesheet to avoid FOUC |
| 41 | // caused by `@font-face` rules, as they seem to be |
| 42 | // a couple of ticks delayed between the old and new |
| 43 | // styles being swapped even if the font is cached. |
| 44 | setTimeout(() => { |
| 45 | otherLink.remove() |
| 46 | }, 5) |
| 47 | const preloadLink = document.querySelector( |
| 48 | `link[rel="preload"][as="style"][href="${otherHref}"]` |
| 49 | ) |
| 50 | if (preloadLink) { |
| 51 | preloadLink.remove() |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
no test coverage detected