Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the loading of next-boot.js and some 3rd-party plugins #990

Merged
merged 13 commits into from
Jul 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,8 @@ growingio_analytics: # <project_id>

# Google Analytics
google_analytics:
#tracking_id:
#localhost_ignored: true
tracking_id: # <app_id>
localhost_ignored: true

# CNZZ count
cnzz_siteid:
Expand Down
22 changes: 4 additions & 18 deletions layout/_layout.swig
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@
</div>
{% endif %}

{% if theme.add_this_id %}
<div>
{% include '_partials/share/add-this.swig' %}
</div>
{% endif %}
{% include '_partials/share/needsharebutton.swig' %}
{% include '_partials/share/add-this.swig' %}
</div>

{% include '_scripts/vendors.swig' %}
Expand All @@ -103,19 +100,8 @@
{% include '_scripts/scroll-cookie.swig' %}
{% include '_scripts/exturl.swig' %}
{% include '_scripts/three.swig' %}
{% include '_third-party/math/index.swig' %}
{% include '_third-party/pangu.swig' %}
{% include '_third-party/quicklink.swig' %}
{% include '_third-party/bookmark.swig' %}
{% include '_third-party/schedule.swig' %}
{% include '_third-party/comments/index.swig' %}
{% include '_third-party/analytics/analytics-with-jquery.swig' %}
{% include '_third-party/search/index.swig' %}
{% include '_third-party/chat/index.swig' %}
{% include '_third-party/tags/index.swig' %}
{% include '_third-party/baidu-push.swig' %}
{% include '_third-party/needsharebutton.swig' %}
{% include '_third-party/rating.swig' %}

{% include '_third-party/index.swig' %}

{% for inject_item in theme.injects.bodyEnd %}
{{ partial(inject_item.layout, inject_item.locals, inject_item.options) }}
Expand Down
4 changes: 1 addition & 3 deletions layout/_macro/post.swig
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,7 @@
{% endif %}
<div class="social_share">
{% if theme.likely.enable %}
<div>
{% include '../_partials/share/likely.swig' %}
</div>
{% include '../_partials/share/likely.swig' %}
{% endif %}
{% if theme.needmoreshare2.enable and theme.needmoreshare2.postbottom.enable %}
<div id="needsharebutton-postbottom">
Expand Down
7 changes: 4 additions & 3 deletions layout/_partials/head/head.swig
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@
mediumzoom: {{ theme.mediumzoom }},
fastclick: {{ theme.fastclick }},
lazyload: {{ theme.lazyload }},
pangu: {{ theme.pangu }},
tabs: {{ theme.tabs.enable }},
motion: {{ theme.motion | json_encode }},
algolia: {
applicationID: '{{ theme.algolia.applicationID }}',
apiKey: '{{ theme.algolia.apiKey }}',
indexName: '{{ theme.algolia.indexName }}',
applicationID: '{{ config.algolia.applicationID }}',
apiKey: '{{ config.algolia.apiKey }}',
indexName: '{{ config.algolia.indexName }}',
hits: {{ theme.algolia_search.hits | json_encode }},
labels: {{ theme.algolia_search.labels | json_encode }}
},
Expand Down
8 changes: 5 additions & 3 deletions layout/_partials/share/add-this.swig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div class="addthis_inline_share_toolbox">
<script src="//s7.addthis.com/js/300/addthis_widget.js#pubid={{ theme.add_this_id }}" async="async"></script>
</div>
{% if theme.add_this_id %}
<div class="addthis_inline_share_toolbox">
<script src="//s7.addthis.com/js/300/addthis_widget.js#pubid={{ theme.add_this_id }}" async="async"></script>
</div>
{% endif %}
26 changes: 15 additions & 11 deletions layout/_scripts/vendors.swig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
{% set js_defer_vendors = {} %}
{% set js_vendors.jquery = 'jquery/index.js?v=3.4.1' %}

{% if theme.fancybox %}
{% set js_vendors.fancybox = 'fancybox/source/jquery.fancybox.pack.js' %}
{% endif %}

{% if theme.mediumzoom %}
{% set js_vendors.mediumzoom = 'mediumzoom/medium-zoom.min.js' %}
{% endif %}

{% if theme.fastclick %}
{% set js_vendors.fastclick = 'fastclick/lib/fastclick.min.js?v=1.0.6' %}
{% endif %}
Expand All @@ -18,17 +26,17 @@
{% set js_vendors.lazyload = 'lazyload/lozad.min.js?v=1.10.0' %}
{% endif %}

{% if theme.motion.enable %}
{% set js_vendors.velocity = 'velocity/velocity.min.js?v=1.2.1' %}
{% set js_vendors.velocity_ui = 'velocity/velocity.ui.min.js?v=1.2.1' %}
{% if theme.pangu %}
{% set js_vendors.pangu = 'pangu/dist/pangu.min.js?v=4.0.7' %}
{% endif %}

{% if theme.fancybox %}
{% set js_vendors.fancybox = 'fancybox/source/jquery.fancybox.pack.js' %}
{% if theme.reading_progress.enable %}
{% set js_vendors.reading_progress = 'reading_progress/reading_progress.js' %}
{% endif %}

{% if theme.mediumzoom %}
{% set js_vendors.mediumzoom = 'mediumzoom/medium-zoom.min.js' %}
{% if theme.motion.enable %}
{% set js_vendors.velocity = 'velocity/velocity.min.js?v=1.2.1' %}
{% set js_vendors.velocity_ui = 'velocity/velocity.ui.min.js?v=1.2.1' %}
{% endif %}

{% if theme.canvas_nest.enable %}
Expand Down Expand Up @@ -69,10 +77,6 @@
<script size="{{ theme.canvas_ribbon.size }}" alpha="{{ theme.canvas_ribbon.alpha }}" zIndex="{{theme.canvas_ribbon.zIndex }}" src="{{ canvas_ribbon_uri }}"></script>
{% endif %}

{% if theme.reading_progress.enable %}
{% set js_vendors.reading_progress = 'reading_progress/reading_progress.js' %}
{% endif %}

{% for name, internal in js_vendors %}
{% set internal_script = url_for(theme.vendors._internal + '/' + internal) %}
<script src="{{ theme.vendors[name] | default(internal_script) }}"></script>
Expand Down
11 changes: 11 additions & 0 deletions layout/_third-party/index.swig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% include 'math/index.swig' %}
{% include 'quicklink.swig' %}
{% include 'bookmark.swig' %}
{% include 'schedule.swig' %}
{% include 'comments/index.swig' %}
{% include 'analytics/analytics-with-jquery.swig' %}
{% include 'search/index.swig' %}
{% include 'chat/index.swig' %}
{% include 'tags/index.swig' %}
{% include 'baidu-push.swig' %}
{% include 'rating.swig' %}
8 changes: 0 additions & 8 deletions layout/_third-party/pangu.swig

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#needsharebutton-float {
position: fixed;
bottom: 38px;
bottom: 88px;
left: -8px;
z-index: 9999;
cursor: pointer;
Expand Down
2 changes: 1 addition & 1 deletion source/css/_common/components/third-party/third-party.styl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import "gitalk" if hexo-config('gitalk.enable');
@import "localsearch";
@import "localsearch" if hexo-config('local_search.enable');
@import "algolia-search" if hexo-config('algolia_search.enable');
@import "needsharebutton" if hexo-config('needmoreshare2.enable');
@import "related-posts" if hexo-config('related_posts.enable');
Expand Down
30 changes: 14 additions & 16 deletions source/js/next-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ $(document).ready(function() {
* Register JS handlers by condition option.
* Need to add config option in Front-End at 'layout/_partials/head.swig' file.
*/
CONFIG.fancybox && NexT.utils.wrapImageWithFancyBox();
CONFIG.mediumzoom && window.mediumZoom('.post-body img');
CONFIG.fastclick && NexT.utils.isMobile() && window.FastClick.attach(document.body);
CONFIG.lazyload && window.lozad('.post-body img').observe();
CONFIG.pangu && window.pangu.spacingPage();

CONFIG.copycode.enable && NexT.utils.registerCopyCode();
CONFIG.tabs && NexT.utils.registerTabsTag();
NexT.utils.registerESCKeyEvent();

CONFIG.back2top.enable && NexT.utils.registerBackToTop();
NexT.utils.embeddedVideoTransformer();

// Mobile top menu bar.
$('.site-nav-toggle button').on('click', function() {
Expand All @@ -26,21 +31,6 @@ $(document).ready(function() {
});
});

CONFIG.fancybox && NexT.utils.wrapImageWithFancyBox();
CONFIG.mediumzoom && window.mediumZoom('.post-body img');
CONFIG.copycode.enable && NexT.utils.registerCopyCode();
CONFIG.tabs && NexT.utils.registerTabsTag();

NexT.utils.embeddedVideoTransformer();

// Define Motion Sequence & Bootstrap Motion.
CONFIG.motion.enable && NexT.motion.integrator
.add(NexT.motion.middleWares.logo)
.add(NexT.motion.middleWares.menu)
.add(NexT.motion.middleWares.postList)
.add(NexT.motion.middleWares.sidebar)
.bootstrap();

/**
* Init Sidebar & TOC inner dimensions on all pages and for all schemes.
* Need for Sidebar/TOC inner scrolling if content taller then viewport.
Expand Down Expand Up @@ -81,4 +71,12 @@ $(document).ready(function() {
$('table').not('.gist table').wrap('<div class="table-container"></div>');
}
wrapTable();

// Define Motion Sequence & Bootstrap Motion.
CONFIG.motion.enable && NexT.motion.integrator
.add(NexT.motion.middleWares.logo)
.add(NexT.motion.middleWares.menu)
.add(NexT.motion.middleWares.postList)
.add(NexT.motion.middleWares.sidebar)
.bootstrap();
});
7 changes: 3 additions & 4 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ NexT.utils = {
* Wrap images with fancybox.
*/
wrapImageWithFancyBox: function() {
$('.content img')
.not('#qr img')
$('.post-body img')
.each(function() {
var $image = $(this);
var imageTitle = $image.attr('title') || $image.attr('alt');
Expand Down Expand Up @@ -156,8 +155,8 @@ NexT.utils = {
var contentVisibilityHeight = NexT.utils.getContentVisibilityHeight();
var scrollPercent = scrollTop / contentVisibilityHeight;
var scrollPercentRounded = Math.round(scrollPercent * 100);
var scrollPercentMaxed = scrollPercentRounded > 100 ? 100 : scrollPercentRounded;
$('#scrollpercent>span').html(scrollPercentMaxed);
var scrollPercentMaxed = Math.min(scrollPercentRounded, 100);
$('#scrollpercent > span').html(scrollPercentMaxed);
}

// For init back to top in sidebar if page was scrolled after page refresh.
Expand Down