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

feat(cache): localstorage cache js and css files #299

Merged
merged 5 commits into from
Apr 19, 2017
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
2 changes: 1 addition & 1 deletion _config.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,4 @@ busuanzi:
enable: false
all_site_uv: false
post_pv: false
busuanzi_pure_mini_js: "https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"
busuanzi_pure_mini_js: "https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"
2 changes: 1 addition & 1 deletion layout/_partial/Paradox-post-thumbnail.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script type="text/ls-javascript" id="post-thumbnail-script">
var randomNum = Math.floor(Math.random() * <%= theme.thumbnail.random_amount %> + 1);

$('.post_thumbnail-random').attr('data-original', '<%= theme.img.random_thumbnail %>' + randomNum + '.png');
Expand Down
2 changes: 1 addition & 1 deletion layout/_partial/Paradox-post_entry-thumbnail.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script type="text/ls-javascript" id="thumbnail-script">
var randomNum;

var locatePost = $('.locate-thumbnail-symbol').next();
Expand Down
16 changes: 16 additions & 0 deletions layout/_partial/config_css.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
font-family: <%- theme.fonts %>;
}

@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(<%- config.root %>fonts/MaterialIcons-Regular.eot);
src: url(<%- config.root %>fonts/MaterialIcons-Regular.woff2) format('woff2'), url(<%- config.root %>fonts/MaterialIcons-Regular.woff) format('woff'), url(<%- config.root %>fonts/MaterialIcons-Regular.ttf) format('truetype')
}

@font-face {
font-family: 'FontAwesome';
src: url(<%- config.root %>fonts/fontawesome-webfont.eot?v=4.5.0);
src: url(<%- config.root %>fonts/fontawesome-webfont.eot?#iefix&v=4.5.0) format('embedded-opentype'), url(<%- config.root %>fonts/fontawesome-webfont.woff2?v=4.5.0) format('woff2'), url(<%- config.root %>fonts/fontawesome-webfont.woff?v=4.5.0) format('woff'), url(<%- config.root %>fonts/fontawesome-webfont.ttf?v=4.5.0) format('truetype'), url(<%- config.root %>fonts/fontawesome-webfont.svg?v=4.5.0#fontawesomeregular) format('svg');
font-weight: 400;
font-style: normal
}

a {
color: <%= theme.uiux.hyperlink_color %>;
}
Expand Down
18 changes: 10 additions & 8 deletions layout/_partial/head.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,21 @@
<% } %>
<![endif]-->

<%- jsHex('js/lsloader.min') %>

<!-- Import CSS & jQuery -->
<% if(theme.materialcdn.use === true) { %>
<link rel="stylesheet" href="<%= theme.materialcdn.base %>/css/material.min.css">
<link rel="stylesheet" href="<%= theme.materialcdn.base %>/css/style.min.css">
<%- cssLsload(theme.materialcdn.base + '/css/material.min.css') %>
<%- cssLsload(theme.materialcdn.base + '/css/style.min.css') %>
<%- partial('_partial/config_css') %>
<script src="<%= theme.materialcdn.base %>/js/jquery.min.js"></script>
<script src="<%= theme.materialcdn.base %>/js/queue.js"></script>
<%- jsLsload(theme.materialcdn.base + '/js/jquery.min.js') %>
<%- jsLsload(theme.materialcdn.base + '/js/queue.js') %>
<%}else{%>
<%- css('css/material.min') %>
<%- css('css/style.min') %>
<%- cssLsload('css/material.min') %>
<%- cssLsload('css/style.min') %>
<%- partial('_partial/config_css') %>
<%- js('js/jquery.min') %>
<%- js('js/queue') %>
<%- jsLsload('js/jquery.min') %>
<%- jsLsload('js/queue') %>
<%}%>

<% if(theme.analytics.baidu_id) { %>
Expand Down
27 changes: 18 additions & 9 deletions layout/_partial/import_js.ejs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<!-- Import File -->

<% if(theme.materialcdn.use === true) { %>
<script src="<%= theme.materialcdn.base %>/js/lazyload.min.js"></script>
<script src="<%= theme.materialcdn.base %>/js/js.min.js"></script>
<%- jsLsload(theme.materialcdn.base + "/js/lazyload.min.js") %>
<%- jsLsload(theme.materialcdn.base + "/js/js.min.js") %>
<%}else{%>
<%- js('js/lazyload.min') %>
<%- js('js/js.min') %>
<%- jsLsload('js/lazyload.min.js') %>
<%- jsLsload('js/js.min') %>
<%}%>

<%- partial('_widget/nprogress') %>

<% if( theme.js_effect.smoothscroll === true ) { %>
<% if(theme.materialcdn.use === true) { %>
<script src="<%= theme.materialcdn.base %>/js/smoothscroll.js"></script>
<%- jsLsload(theme.materialcdn.base + "/js/smoothscroll.js") %>
<%}else{%>
<%- js('js/smoothscroll') %>
<%- jsLsload('js/smoothscroll') %>
<%}%>
<% } %>

Expand All @@ -29,7 +30,7 @@
<% if( theme.search.use === 'local' ) { %>
<%- partial('_widget/search-local-js') %>

<script>
<script type="text/ls-javascript" id="search-input-script">
var inputArea = document.querySelector('#search');
var getSearchFile = function() {
var path = '<%= config.search.path %>';
Expand All @@ -55,7 +56,7 @@
</script>

<!-- Window Load-->
<script>
<script type="text/ls-javascript" id="window-load">
$(window).load(function() {
// Post_Toc parent position fixed
$('.post-toc-wrap').parent('.mdl-menu__container').css('position', 'fixed');
Expand All @@ -66,7 +67,7 @@
<% if (page.mathjax) { %>
<%- partial('_widget/mathjax') %>
<% } %>
<script>
<script type="text/ls-javascript" id="lazy-load">
<!-- Offer LazyLoad -->
queue.offer(function(){
$('.lazy').lazyload({
Expand All @@ -83,5 +84,13 @@
</script>

<script>
(function(){
var scriptList = document.querySelectorAll('script[type="text/ls-javascript"]')

for (var i = 0; i < scriptList.length; ++i) {
var item = scriptList[i];
lsloader.runInlineScript(item.id,item.id);
}
})()
console.log('\n %c © Material Theme | Version: 1.3.3 | https://github.com/viosey/hexo-theme-material %c \n', 'color:#455a64;background:#e0e0e0;padding:5px 0;border-top-left-radius:5px;border-bottom-left-radius:5px;', 'color:#455a64;background:#e0e0e0;padding:5px 0;border-top-right-radius:5px;border-bottom-right-radius:5px;');
</script>
2 changes: 1 addition & 1 deletion layout/_widget/disqus.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="disqus_thread"></div>
<script>
<script type="text/ls-javascript" id="disqus-thread-script">
var disqus_config = function () {
this.page.url = '<%= page.permalink %>'; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = '<%= page.permalink %>'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
Expand Down
4 changes: 2 additions & 2 deletions layout/_widget/disqus_click.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@
<button class="disqus_click_btn"><%= __('post.comments_load_button') %></button>
</div>

<script>
<script type="text/ls-javascript" id="create-ripple-script">
// add effect to elements
Array.prototype.forEach.call(document.querySelectorAll('[data-ripple]'), function(element){
// find all elements and attach effect
new RippleEffect(element); // element is instance of javascript element node
});
</script>

<script>
<script type="text/ls-javascript" id="disqus-lazy-load-script">
var disqus_config = function () {
this.page.url = '<%= page.permalink %>'; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = '<%= page.permalink %>'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/duoshuo.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%- css('css/duoshuo.min') %>
<%- cssLsload('css/duoshuo.min') %>
<style>
#ds-thread #ds-reset .ds-post-button{
background-color: <%= theme.uiux.theme_color %> !important;
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/leancloud-like.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script type="text/ls-javascript" id="leancloud-like-script">
function showTime(Like) {
var query = new AV.Query(Like);
$('.leancloud-like_num').each(function() {
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/leancloud-views.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script type="text/ls-javascript" id="leancloud-views-script">
function showTime(Counter) {
var query = new AV.Query(Counter);
$('.leancloud-views_num').each(function() {
Expand Down
6 changes: 3 additions & 3 deletions layout/_widget/nprogress.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% if(theme.materialcdn.use === true) { %>
<script src="<%= theme.materialcdn.base %>/js/nprogress.js"></script>
<%- jsLsload(theme.materialcdn.base + "/js/nprogress.js") %>
<%}else{%>
<%- js('js/nprogress') %>
<%- jsLsload('js/nprogress') %>
<%}%>

<script type="text/javascript">
<script type="text/ls-javascript" id="NProgress-script">
NProgress.configure({
showSpinner: true
});
Expand Down
14 changes: 7 additions & 7 deletions layout/_widget/page-gallery.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@

<!-- Scripts -->
<% if(theme.materialcdn.use === true) { %>
<script src="<%= theme.materialcdn.base %>/js/jquery.min.js"></script>
<script src="<%= theme.materialcdn.base %>/js/gallery/gallery.js"></script>
<script src="<%= theme.materialcdn.base %>/js/lazyload.min.js"></script>
<%- jsLsload(theme.materialcdn.base + "/js/jquery.min.js") %>
<%- jsLsload(theme.materialcdn.base + "/js/gallery/gallery.js") %>
<%- jsLsload(theme.materialcdn.base + "/js/lazyload.min.js") %>
<%}else{%>
<%- js('js/jquery.min') %>
<%- js('js/gallery/gallery') %>
<%- js('js/lazyload.min') %>
<%- jsLsload('js/jquery.min') %>
<%- jsLsload('js/gallery/gallery') %>
<%- jsLsload('js/lazyload.min') %>
<%}%>

<script type="text/javascript">
<script type="text/ls-javascript" id="page-gallery-script">
queue.offer(function(){
$('.lazy').lazyload({
effect: 'fadeIn',
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/page-links.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<% } %>
</ul>

<script>
<script type="text/ls-javascript" id="page-links-script">
var adjustFooter = function() {
if( ($('#bottom').offset().top + $('#bottom').outerHeight() )<$(window).height() ) {
var footerBottom = $(window).height() - $('#bottom').outerHeight() - $('#bottom').offset().top;
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/page-tagcloud.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</div>


<script>
<script type="text/ls-javascript" id="page-tagcloud-script">
var adjustFooter = function() {
if( ($('#bottom').offset().top + $('#bottom').outerHeight() ) < $(window).height() ) {
var footerBottom = $(window).height() - $('#bottom').outerHeight() - $('#bottom').offset().top;
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/page-timeline.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
<% } -%>
</section>

<script>
<script type="text/ls-javascript" id="page-timeline-script">
var adjustFooter = function() {
if( ($('#bottom').offset().top + $('#bottom').outerHeight() )<$(window).height() ) {
var footerBottom = $(window).height() - $('#bottom').outerHeight() - $('#bottom').offset().top;
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/search-local-js.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script>
<script type="text/ls-javascript" id="search-local-js-script">
var searchFunc = function(path, search_id, content_id) {
'use strict';
$.ajax({
Expand Down
2 changes: 1 addition & 1 deletion layout/_widget/search-swiftype-js.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script type="text/javascript">
<script type="text/ls-javascript" id="search-swiftype-js-script">
(function(w,d,t,u,n,s,e) {w['SwiftypeObject']=n;w[n]=w[n]||function() {
(w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t);
e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e);
Expand Down
2 changes: 1 addition & 1 deletion layout/layout.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html <% if(config.language !== null) { %>lang="<%- config.language.substring(0,2) %>"<% } %>>
<html style="display: none;" <% if(config.language !== null) { %>lang="<%- config.language.substring(0,2) %>"<% } %>>
<%- partial('_partial/head') %>

<% if(page.layout === 'gallery') { %>
Expand Down
11 changes: 11 additions & 0 deletions scripts/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
hexo.extend.helper.register('jsHex', function(){
return require("./lib/js_hex.js").call(hexo,...arguments);
});

hexo.extend.helper.register('jsLsload', function(){
return require("./lib/js_lsload.js").call(hexo,...arguments);
});

hexo.extend.helper.register('cssLsload', function(){
return require("./lib/css_lsload.js").call(hexo,...arguments);
});
35 changes: 35 additions & 0 deletions scripts/lib/css_lsload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var path_for = require("./path_for");
var get_file_hex = require("./get_file_hex");
var fs = require('fs');

function cssHelper() {
var result = '';
var path = '';

for (var i = 0, len = arguments.length; i < len; i++) {
path = arguments[i];

if (i) result += '\n';

var localpath = path_for.call(this,path);

if (Array.isArray(path)) {
result += jsHelper.apply(this, path);
} else {
if (path.indexOf('?') < 0 && path.substring(path.length - 4, path.length) !== '.css') path += '.css';
result += '<style id="' + path + '"></style><script>if(typeof window.lsLoadCSSMaxNums === "undefined")window.lsLoadCSSMaxNums = 0;' +
'window.lsLoadCSSMaxNums++;' +
'lsloader.load("' + path + '","' +
require("../../../../node_modules/hexo/lib/plugins/helper/url_for").call(this,path) +
(fs.existsSync(localpath)?'?' + get_file_hex(localpath):'') + '",function(){if(typeof window.lsLoadCSSNums === "undefined")window.lsLoadCSSNums = 0;' +
'window.lsLoadCSSNums++;' +
'if(window.lsLoadCSSNums == window.lsLoadCSSMaxNums)document.documentElement.style.display="";' +
'})</script>'
}
}
return result;
}

module.exports = cssHelper;
Empty file added scripts/lib/font_lsload.js
Empty file.
14 changes: 14 additions & 0 deletions scripts/lib/get_file_hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var fs = require('fs');
var crypto = require('crypto');


function getFileHexSync(filepath) {
var buffer = fs.readFileSync(filepath);
var fsHash = crypto.createHash('md5');

fsHash.update(buffer);
var sha384 = fsHash.digest('base64');
return sha384;
}

module.exports = getFileHexSync
26 changes: 26 additions & 0 deletions scripts/lib/js_hex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

var path_for = require("./path_for");
var get_file_hex = require("./get_file_hex");

function jsHelper() {
var result = '';
var path = '';

for (var i = 0, len = arguments.length; i < len; i++) {
path = arguments[i];

if (i) result += '\n';

if (Array.isArray(path)) {
result += jsHelper.apply(this, path);
} else {
if (path.indexOf('?') < 0 && path.substring(path.length - 3, path.length) !== '.js') path += '.js';
result += '<script src="' + require("../../../../node_modules/hexo/lib/plugins/helper/url_for").call(this,path) + '?' + get_file_hex(path_for.call(this,path)) + '"></script>';
}
}

return result;
}

module.exports = jsHelper;
30 changes: 30 additions & 0 deletions scripts/lib/js_lsload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var path_for = require("./path_for");
var get_file_hex = require("./get_file_hex");
var fs = require('fs');

function jsHelper() {
var result = '';
var path = '';

for (var i = 0, len = arguments.length; i < len; i++) {
path = arguments[i];

if (i) result += '\n';

var localpath = path_for.call(this,path);

if (Array.isArray(path)) {
result += jsHelper.apply(this, path);
} else {
if (path.indexOf('?') < 0 && path.substring(path.length - 3, path.length) !== '.js') path += '.js';
result += '<script>lsloader.load("' + path + '","' +
require("../../../../node_modules/hexo/lib/plugins/helper/url_for").call(this,path) +
(fs.existsSync(localpath)?'?' + get_file_hex(localpath):'') + '")</script>'
}
}
return result;
}

module.exports = jsHelper;
Loading