From 8c5d31498ba297be71816e8ffbb24cde6e993f37 Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Wed, 15 May 2024 12:34:14 +0000 Subject: [PATCH 1/5] Enable source map for development --- blog.config.js | 2 +- next.config.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/blog.config.js b/blog.config.js index 7c5faf6c..3a30ddea 100644 --- a/blog.config.js +++ b/blog.config.js @@ -540,7 +540,7 @@ const BLOG = { process.env.ENABLE_CACHE || process.env.npm_lifecycle_event === 'build' || process.env.npm_lifecycle_event === 'export', // 在打包过程中默认开启缓存,开发或运行时开启此功能意义不大。 - isProd: process.env.VERCEL_ENV === 'production', // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) isProd: process.env.VERCEL_ENV === 'production' // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) + isProd: process.env.VERCEL_ENV === 'production', // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables) BUNDLE_ANALYZER: process.env.ANALYZE === 'true' || false, // 是否展示编译依赖内容与大小 VERSION: process.env.NEXT_PUBLIC_VERSION // 版本号 } diff --git a/next.config.js b/next.config.js index d8a6c2ba..20a1c311 100644 --- a/next.config.js +++ b/next.config.js @@ -165,6 +165,10 @@ const nextConfig = { 'themes', THEME ) + // Enable source maps in development mode + if (process.env.NODE_ENV_API === 'development') { + config.devtool = 'source-map' + } return config }, experimental: { @@ -181,7 +185,6 @@ const nextConfig = { }, publicRuntimeConfig: { // 这里的配置既可以服务端获取到,也可以在浏览器端获取到 - NODE_ENV_API: process.env.NODE_ENV_API || 'prod', THEMES: themes } } From a66c416fa4f3041ae81e8d7363c3ca38d4936a0a Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Wed, 15 May 2024 12:46:17 +0000 Subject: [PATCH 2/5] Add parent checking before inserting or removing the child --- components/CustomContextMenu.js | 4 +++- components/DifyChatbot.js | 4 +++- components/FacebookMessenger.js | 6 +++++- components/PWA.js | 2 +- components/PrismMac.js | 2 +- lib/plugins/busuanzi.js | 5 ++++- public/js/flutteringRibbon.js | 2 +- public/js/nest.js | 2 +- public/js/ribbon.js | 2 +- public/js/sakura.js | 10 ++++++---- themes/movie/index.js | 6 +++++- themes/theme.js | 4 +++- 12 files changed, 34 insertions(+), 15 deletions(-) diff --git a/components/CustomContextMenu.js b/components/CustomContextMenu.js index 46ffb2a0..282c9244 100644 --- a/components/CustomContextMenu.js +++ b/components/CustomContextMenu.js @@ -111,7 +111,9 @@ export default function CustomContextMenu(props) { document.body.appendChild(tempInput); tempInput.select(); document.execCommand('copy'); - document.body.removeChild(tempInput); + if (tempInput && tempInput.parentNode && tempInput.parentNode.contains(tempInput)) { + tempInput.parentNode.removeChild(tempInput); + } // alert("Text copied: " + selectedText); } else { // alert("Please select some text first."); diff --git a/components/DifyChatbot.js b/components/DifyChatbot.js index c954a872..6190c58b 100644 --- a/components/DifyChatbot.js +++ b/components/DifyChatbot.js @@ -24,7 +24,9 @@ export default function DifyChatbot() { return () => { // 在组件卸载时清理 script 标签 const existingScript = document.getElementById(siteConfig('DIFY_CHATBOT_TOKEN')); // 注意调用 siteConfig() - if (existingScript) document.body.removeChild(existingScript); + if (existingScript && existingScript.parentNode && existingScript.parentNode.contains(existingScript)) { + existingScript.parentNode.removeChild(existingScript); + } }; }, []); // 注意依赖数组为空,意味着脚本将仅在加载页面时执行一次 diff --git a/components/FacebookMessenger.js b/components/FacebookMessenger.js index ef8da0cb..9531b322 100644 --- a/components/FacebookMessenger.js +++ b/components/FacebookMessenger.js @@ -116,7 +116,11 @@ class MessengerCustomerChat extends Component { js = d.createElement(s); js.id = id; js.src = `https://connect.facebook.net/${language}/sdk/xfbml.customerchat.js`; - fjs.parentNode.insertBefore(js, fjs); + if (fjs && fjs.parentNode && fjs.parentNode.contains(fjs)) { + fjs.parentNode.insertBefore(js, fjs); + } else { + document.body.appendChild(js); + } })(document, 'script', 'facebook-jssdk'); /* eslint-enable */ } diff --git a/components/PWA.js b/components/PWA.js index 118a7c18..2a292100 100644 --- a/components/PWA.js +++ b/components/PWA.js @@ -34,7 +34,7 @@ export function PWA(post, siteInfo) { // 删除已有的 manifest link 元素(如果存在) const existingManifest = document.querySelector('link[rel="manifest"]') - if (existingManifest) { + if (existingManifest && existingManifest.parentNode && existingManifest.parentNode.contains(existingManifest)) { existingManifest.parentNode.removeChild(existingManifest) } diff --git a/components/PrismMac.js b/components/PrismMac.js index c92d594d..5d04fb22 100644 --- a/components/PrismMac.js +++ b/components/PrismMac.js @@ -74,7 +74,7 @@ const loadPrismThemeCSS = (isDarkMode, prismThemeSwitch, prismThemeDarkPath, pri PRISM_PREVIOUS = prismThemeDarkPath } const previousTheme = document.querySelector(`link[href="${PRISM_PREVIOUS}"]`) - if (previousTheme) { + if (previousTheme && previousTheme.parentNode && previousTheme.parentNode.contains(previousTheme)) { previousTheme.parentNode.removeChild(previousTheme) } loadExternalResource(PRISM_THEME, 'css') diff --git a/lib/plugins/busuanzi.js b/lib/plugins/busuanzi.js index 439aa4b7..9186078a 100644 --- a/lib/plugins/busuanzi.js +++ b/lib/plugins/busuanzi.js @@ -43,7 +43,10 @@ bszCaller = { return function (t) { ready(function () { try { - e(t), scriptTag && scriptTag.parentElement && scriptTag.parentElement.removeChild && scriptTag.parentElement.removeChild(scriptTag) + e(t) + if (scriptTag && scriptTag.parentElement && scriptTag.parentElement.contains(scriptTag)) { + scriptTag.parentElement.removeChild(scriptTag) + } } catch (t) { // console.log(t), bszTag.hides() } diff --git a/public/js/flutteringRibbon.js b/public/js/flutteringRibbon.js index d698a117..e701ebd2 100644 --- a/public/js/flutteringRibbon.js +++ b/public/js/flutteringRibbon.js @@ -3,7 +3,7 @@ const idFlutteringRibbon = 'canvasFlutteringRibbon' function destroyFlutteringRibbon() { const ribbon = document.getElementById(idFlutteringRibbon) - if (ribbon && ribbon.parentNode) { + if (ribbon && ribbon.parentNode && ribbon.parentNode.contains(ribbon)) { ribbon.parentNode.removeChild(ribbon) } } diff --git a/public/js/nest.js b/public/js/nest.js index 2b151d00..2dc5cf25 100644 --- a/public/js/nest.js +++ b/public/js/nest.js @@ -109,7 +109,7 @@ function createNest() { function destroyNest() { const nest = document.getElementById(idNest) - if(nest && nest.parentNode){ + if (nest && nest.parentNode && nest.parentNode.contains(nest)) { nest.parentNode.removeChild(nest) } } diff --git a/public/js/ribbon.js b/public/js/ribbon.js index 928ccd11..55c6731c 100644 --- a/public/js/ribbon.js +++ b/public/js/ribbon.js @@ -80,7 +80,7 @@ function createRibbon() { function destroyRibbon() { const ribbon = document.getElementById(idRibbon) - if (ribbon && ribbon.parentNode) { + if (ribbon && ribbon.parentNode && ribbon.parentNode.contains(ribbon)) { ribbon.parentNode.removeChild(ribbon) } } diff --git a/public/js/sakura.js b/public/js/sakura.js index 4d246a35..fcdf0739 100644 --- a/public/js/sakura.js +++ b/public/js/sakura.js @@ -165,9 +165,11 @@ function createSakura() { function stopp() { if (staticx) { var child = document.getElementById(id) - child.parentNode.removeChild(child) - window.cancelAnimationFrame(stop) - staticx = false + if (child && child.parentNode && child.parentNode.contains(child)) { + child.parentNode.removeChild(child) + window.cancelAnimationFrame(stop) + staticx = false + } } else { startSakura() } @@ -177,7 +179,7 @@ function createSakura() { // 销毁樱花雨 function destroySakura() { const sakura = document.getElementById(idSakura) - if (sakura && sakura.parentNode) { + if (sakura && sakura.parentNode && sakura.parentNode.contains(sakura)) { sakura.parentNode.removeChild(sakura) } } diff --git a/themes/movie/index.js b/themes/movie/index.js index 96195bf2..b62b543f 100644 --- a/themes/movie/index.js +++ b/themes/movie/index.js @@ -258,7 +258,11 @@ const LayoutSlug = props => { videoWrapper.appendChild(figCaptionWrapper) } // 放入页面 - notionArticle.insertBefore(videoWrapper, notionArticle.firstChild) + if (notionArticle.firstChild && notionArticle.contains(notionArticle.firstChild)) { + notionArticle.insertBefore(videoWrapper, notionArticle.firstChild) + } else { + notionArticle.appendChild(videoWrapper) + } } } diff --git a/themes/theme.js b/themes/theme.js index fb4f94f0..8192c96f 100644 --- a/themes/theme.js +++ b/themes/theme.js @@ -82,7 +82,9 @@ const checkThemeDOM = () => { elements[elements.length - 1].scrollIntoView() // 删除前面的元素,只保留最后一个元素 for (let i = 0; i < elements.length - 1; i++) { - elements[i].parentNode.removeChild(elements[i]) + if (elements[i] && elements[i].parentNode && elements[i].parentNode.contains(elements[i])) { + elements[i].parentNode.removeChild(elements[i]) + } } } } From f730c4150d7f0664d0bfd5683fef78d2950721da Mon Sep 17 00:00:00 2001 From: Phillweston <2436559745@qq.com> Date: Wed, 15 May 2024 13:44:27 +0000 Subject: [PATCH 3/5] Update script format for external plugins --- components/ExternalPlugins.js | 37 +++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/components/ExternalPlugins.js b/components/ExternalPlugins.js index 89c18688..bf1c9e8b 100644 --- a/components/ExternalPlugins.js +++ b/components/ExternalPlugins.js @@ -187,10 +187,19 @@ const ExternalPlugin = props => { async dangerouslySetInnerHTML={{ __html: ` - (function(c,l,a,r,i,t,y){ - c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; - t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; - y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); + (function(c, l, a, r, i, t, y) { + c[a] = c[a] || function() { + (c[a].q = c[a].q || []).push(arguments); + }; + t = l.createElement(r); + t.async = 1; + t.src = "https://www.clarity.ms/tag/" + i; + y = l.getElementsByTagName(r)[0]; + if (y && y.parentNode) { + y.parentNode.insertBefore(t, y); + } else { + l.head.appendChild(t); + } })(window, document, "clarity", "script", "${CLARITY_ID}"); ` }} @@ -205,8 +214,24 @@ const ExternalPlugin = props => { async dangerouslySetInnerHTML={{ __html: ` - (function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/daf1a94b.js","daovoice") - ` + (function(i, s, o, g, r, a, m) { + i["DaoVoiceObject"] = r; + i[r] = i[r] || function() { + (i[r].q = i[r].q || []).push(arguments); + }; + i[r].l = 1 * new Date(); + a = s.createElement(o); + m = s.getElementsByTagName(o)[0]; + a.async = 1; + a.src = g; + a.charset = "utf-8"; + if (m && m.parentNode) { + m.parentNode.insertBefore(a, m); + } else { + s.head.appendChild(a); + } + })(window, document, "script", ('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/daf1a94b.js", "daovoice") + ` }} />