From 525c8a7cf5f84aca3eb1120d984731d32b7236d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=A1=E6=A2=A6=E6=98=9F=E5=B0=98?= Date: Wed, 26 Oct 2022 22:37:03 +0800 Subject: [PATCH] :zap: Upgrade third party js logic improve load speed. --- assets/js/config.js | 36 +++++++++++++++++--- assets/js/next-boot.js | 2 +- assets/js/third-party/comments/artalk.js | 3 +- assets/js/third-party/comments/giscus.js | 3 +- assets/js/third-party/comments/livere.js | 3 +- assets/js/third-party/comments/utterances.js | 3 +- assets/js/third-party/comments/waline.js | 3 +- assets/js/third-party/search/algolia.js | 16 ++++----- assets/js/third-party/search/local.js | 6 ++-- assets/js/third-party/share/addthis.js | 10 +++--- 10 files changed, 58 insertions(+), 27 deletions(-) diff --git a/assets/js/config.js b/assets/js/config.js index 0a23d35..00da52e 100644 --- a/assets/js/config.js +++ b/assets/js/config.js @@ -1,14 +1,42 @@ /* global NexT, boot, CONFIG */ window.NexT = {}; NexT.boot = {}; -NexT.comments = {}; +NexT.plugins = {}; -NexT.comments.register = function() { +// Defined comment component & add register event +NexT.plugins.comments = {}; +NexT.plugins.comments.register = function() { if (!NexT.CONFIG.page.comments) return; - for(var c in NexT.comments) { + for(var c in NexT.plugins.comments) { if (c === 'register') continue; - eval('NexT.comments.'+c)(); + eval('NexT.plugins.comments.'+c)(); }; +} + +// Defined search engine & add register event +NexT.plugins.search = {} +NexT.plugins.search.register = function() { + for(var s in NexT.plugins.search) { + if (s === 'register') continue; + eval('NexT.plugins.search.'+s)(); + }; +} + +// Defined share plugin & add register event +NexT.plugins.share = {} +NexT.plugins.share.register = function() { + for(var s in NexT.plugins.share) { + if (s === 'register') continue; + eval('NexT.plugins.share.'+s)(); + }; +} + +// Add event to register all third party plugins +NexT.plugins.register = function() { + for(var p in NexT.plugins) { + if (p === 'register') continue; + eval('NexT.plugins.'+p+'.register')(); + } }; (function() { diff --git a/assets/js/next-boot.js b/assets/js/next-boot.js index a7046ac..5ccb9cb 100644 --- a/assets/js/next-boot.js +++ b/assets/js/next-boot.js @@ -21,7 +21,7 @@ NexT.boot.registerEvents = function() { // NexT.utils.registerCanIUseTag(); NexT.utils.registerToolButtons(); // Register comment's components - NexT.comments.register(); + NexT.plugins.register(); // Mobile top menu bar. document.querySelector('.site-nav-toggle .toggle').addEventListener('click', event => { diff --git a/assets/js/third-party/comments/artalk.js b/assets/js/third-party/comments/artalk.js index 8444a22..827f2dd 100644 --- a/assets/js/third-party/comments/artalk.js +++ b/assets/js/third-party/comments/artalk.js @@ -1,4 +1,5 @@ -NexT.comments.giscus = function() { +/* Artalk comment plugin */ +NexT.plugins.comments.artalk = function() { const element = '.artalk-container'; if (!NexT.CONFIG.artalk || !NexT.utils.checkDOMExist(element)) return; diff --git a/assets/js/third-party/comments/giscus.js b/assets/js/third-party/comments/giscus.js index 860a60c..2147daa 100644 --- a/assets/js/third-party/comments/giscus.js +++ b/assets/js/third-party/comments/giscus.js @@ -1,4 +1,5 @@ -NexT.comments.giscus = function() { +/* Giscus comment plugin */ +NexT.plugins.comments.giscus = function() { const element = '.giscus-container'; if (!NexT.CONFIG.page.comments || !NexT.CONFIG.giscus diff --git a/assets/js/third-party/comments/livere.js b/assets/js/third-party/comments/livere.js index d18edc8..2d1820c 100644 --- a/assets/js/third-party/comments/livere.js +++ b/assets/js/third-party/comments/livere.js @@ -1,4 +1,5 @@ -NexT.comments.giscus = function() { +/* LiveRe comment plugin */ +NexT.plugins.comments.livere = function() { const element = '#lv-container'; if (!NexT.CONFIG.livere || !NexT.utils.checkDOMExist(element)) return; diff --git a/assets/js/third-party/comments/utterances.js b/assets/js/third-party/comments/utterances.js index 43f382b..0e32c64 100644 --- a/assets/js/third-party/comments/utterances.js +++ b/assets/js/third-party/comments/utterances.js @@ -1,4 +1,5 @@ -NexT.comments.giscus = function() { +/* Utterances comment plugin */ +NexT.plugins.comments.utterances = function() { const element = '.utterances-container'; if (!NexT.CONFIG.utterances || !NexT.utils.checkDOMExist(element)) return; diff --git a/assets/js/third-party/comments/waline.js b/assets/js/third-party/comments/waline.js index 1d17691..2766f5e 100644 --- a/assets/js/third-party/comments/waline.js +++ b/assets/js/third-party/comments/waline.js @@ -1,4 +1,5 @@ -NexT.comments.waline = function() { +/* Waline comment plugin */ +NexT.plugins.comments.waline = function() { const element = '.waline-container'; if (!NexT.CONFIG.waline || !NexT.utils.checkDOMExist(element)) return; diff --git a/assets/js/third-party/search/algolia.js b/assets/js/third-party/search/algolia.js index d293fcd..5af3492 100644 --- a/assets/js/third-party/search/algolia.js +++ b/assets/js/third-party/search/algolia.js @@ -1,12 +1,11 @@ -/* global instantsearch, algoliasearch, CONFIG, pjax */ +/* Algolia search engine */ +NexT.plugins.search.algolia = function() { -document.addEventListener('DOMContentLoaded', () => { - - const algoiajs = NexT.utils.getCDNResource(NexT.CONFIG.algolia.js); + const algoliajs = NexT.utils.getCDNResource(NexT.CONFIG.algolia.js); const instantschjs = NexT.utils.getCDNResource(NexT.CONFIG.algolia.instantjs); - NexT.utils.getScript(algoiajs, {}); - NexT.utils.getScript(instantschjs, {}).then(() => { + NexT.utils.getScript(algoliajs); + NexT.utils.getScript(instantschjs, function() { const { indexname, appid, apikey, hits } = NexT.CONFIG.algolia.cfg; const indexName = indexname; @@ -138,7 +137,6 @@ document.addEventListener('DOMContentLoaded', () => { onPopupClose(); } }); - });; + }); - -}); \ No newline at end of file +} \ No newline at end of file diff --git a/assets/js/third-party/search/local.js b/assets/js/third-party/search/local.js index c2c238f..5c30d54 100644 --- a/assets/js/third-party/search/local.js +++ b/assets/js/third-party/search/local.js @@ -1,4 +1,4 @@ -/* global CONFIG, pjax, LocalSearch */ +/* LocalSearch engine */ class LocalSearch { constructor({ path = '', @@ -227,7 +227,7 @@ class LocalSearch { } } -document.addEventListener('DOMContentLoaded', () => { +NexT.plugins.search.localsearch = function() { if (! NexT.CONFIG.localSearch.path) { // Search DB path console.warn('`search indexes file` is not configurate!'); @@ -323,4 +323,4 @@ document.addEventListener('DOMContentLoaded', () => { onPopupClose(); } }); -}); +} diff --git a/assets/js/third-party/share/addthis.js b/assets/js/third-party/share/addthis.js index 17371a3..1bc88e3 100644 --- a/assets/js/third-party/share/addthis.js +++ b/assets/js/third-party/share/addthis.js @@ -1,16 +1,16 @@ -document.addEventListener('DOMContentLoaded', () => { - +/* AddThis share plugin */ +NexT.plugins.share.addthis = function() { const element = '.addthis_inline_share_toolbox'; if (!NexT.CONFIG.addthis || !NexT.utils.checkDOMExist(element)) return; const addthis_js = NexT.CONFIG.addthis.js + '?pubid=' + NexT.CONFIG.addthis.cfg.pubid; - NexT.utils.loadComments(element).then(() => { + NexT.utils.lazyLoadComponent(element, function() { NexT.utils.getScript(addthis_js, { attributes: { - async: true + async: false }, parentNode: document.querySelector(element) }); }); -}); \ No newline at end of file +} \ No newline at end of file