From db2e64eb56d724c620207e9e0b100aa1ee45b39f Mon Sep 17 00:00:00 2001 From: Jude Agboola Date: Mon, 8 Aug 2022 13:59:08 +0100 Subject: [PATCH 1/4] remove useless new array creation --- .../cache-dir/head/head-export-handler-for-browser.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js b/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js index 7f1866fbd5c40..f5efc17fbcf34 100644 --- a/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js +++ b/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js @@ -15,7 +15,7 @@ import { const hiddenRoot = document.createElement(`div`) const removePrevHeadElements = () => { - const prevHeadNodes = [...document.querySelectorAll(`[data-gatsby-head]`)] + const prevHeadNodes = document.querySelectorAll(`[data-gatsby-head]`) prevHeadNodes.forEach(e => e.remove()) } @@ -58,9 +58,7 @@ const onHeadRendered = () => { } } - const existingHeadElements = [ - ...document.querySelectorAll(`[data-gatsby-head]`), - ] + const existingHeadElements = document.querySelectorAll(`[data-gatsby-head]`) if (existingHeadElements.length === 0) { document.head.append(...validHeadNodes) From 76b2302e50eb68908ef0c2c11d7797109d031e10 Mon Sep 17 00:00:00 2001 From: Jude Agboola Date: Mon, 8 Aug 2022 15:57:39 +0100 Subject: [PATCH 2/4] use for..of over forEach for browser compatibility --- .../gatsby/cache-dir/head/head-export-handler-for-browser.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js b/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js index f5efc17fbcf34..f7f5234f1fe2f 100644 --- a/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js +++ b/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js @@ -16,7 +16,10 @@ const hiddenRoot = document.createElement(`div`) const removePrevHeadElements = () => { const prevHeadNodes = document.querySelectorAll(`[data-gatsby-head]`) - prevHeadNodes.forEach(e => e.remove()) + + for (const node of prevHeadNodes) { + node.remove() + } } const onHeadRendered = () => { From c97168619a47a56b547a58c19854957f80217fc7 Mon Sep 17 00:00:00 2001 From: Jude Agboola Date: Tue, 9 Aug 2022 11:10:31 +0100 Subject: [PATCH 3/4] use parentNode.removeChild() for compatibility with IE --- .../cache-dir/head/head-export-handler-for-browser.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js b/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js index f7f5234f1fe2f..101afbcb9f885 100644 --- a/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js +++ b/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js @@ -18,7 +18,7 @@ const removePrevHeadElements = () => { const prevHeadNodes = document.querySelectorAll(`[data-gatsby-head]`) for (const node of prevHeadNodes) { - node.remove() + node.parentNode.removeChild(node) } } @@ -52,7 +52,9 @@ const onHeadRendered = () => { seenIds.set(id, validHeadNodes.length - 1) } else { const indexOfPreviouslyInsertedNode = seenIds.get(id) - validHeadNodes[indexOfPreviouslyInsertedNode].remove() + validHeadNodes[indexOfPreviouslyInsertedNode].parentNode.removeChild( + validHeadNodes[indexOfPreviouslyInsertedNode] + ) validHeadNodes[indexOfPreviouslyInsertedNode] = clonedNode } } else { @@ -72,7 +74,7 @@ const onHeadRendered = () => { diffNodes({ oldNodes: existingHeadElements, newNodes: validHeadNodes, - onStale: node => node.remove(), + onStale: node => node.parentNode.removeChild(node), onNew: node => newHeadNodes.push(node), }) From 71ac8edb9ec6ecf585d7db1d3822bff35ecedd5c Mon Sep 17 00:00:00 2001 From: Jude Agboola Date: Wed, 10 Aug 2022 10:48:25 +0100 Subject: [PATCH 4/4] fix error when parentNode is null --- .../gatsby/cache-dir/head/head-export-handler-for-browser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js b/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js index 101afbcb9f885..f13bb7bfc093a 100644 --- a/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js +++ b/packages/gatsby/cache-dir/head/head-export-handler-for-browser.js @@ -52,7 +52,7 @@ const onHeadRendered = () => { seenIds.set(id, validHeadNodes.length - 1) } else { const indexOfPreviouslyInsertedNode = seenIds.get(id) - validHeadNodes[indexOfPreviouslyInsertedNode].parentNode.removeChild( + validHeadNodes[indexOfPreviouslyInsertedNode].parentNode?.removeChild( validHeadNodes[indexOfPreviouslyInsertedNode] ) validHeadNodes[indexOfPreviouslyInsertedNode] = clonedNode