diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-06 17:13:10 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-06 17:13:10 -0700 |
commit | 9ffb1a062f6bfa4c3253eb40c89ba54e6e429fe4 (patch) | |
tree | 125ce2fb57852b3a5fe90a53c5d2197f4bfe8170 /theme/assets/app.js | |
parent | 536c48096938ef141fdf4351739f8362e80641da (diff) | |
download | gitbook-9ffb1a062f6bfa4c3253eb40c89ba54e6e429fe4.zip gitbook-9ffb1a062f6bfa4c3253eb40c89ba54e6e429fe4.tar.gz gitbook-9ffb1a062f6bfa4c3253eb40c89ba54e6e429fe4.tar.bz2 |
Use cache in localstorage to store search index
Diffstat (limited to 'theme/assets/app.js')
-rw-r--r-- | theme/assets/app.js | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/theme/assets/app.js b/theme/assets/app.js index ec6285f..f2956de 100644 --- a/theme/assets/app.js +++ b/theme/assets/app.js @@ -18089,7 +18089,8 @@ define('core/state',[ 'githubId': $book.data("github"), 'level': $book.data("level"), - 'basePath': $book.data("basepath") + 'basePath': $book.data("basepath"), + 'revision': $book.data("revision") }; }); /*global define:false */ @@ -21009,19 +21010,32 @@ define('core/search',[ "jQuery", "lodash", "lunr", + "utils/storage", "core/state", "core/sidebar" -], function($, _, lunr, state, sidebar) { +], function($, _, lunr, storage, state, sidebar) { var index = null; var $searchBar = state.$book.find(".book-search"); var $searchInput = $searchBar.find("input"); + // Use a specific idnex + var useIndex = function(data) { + index = lunr.Index.load(data); + }; + // Load complete index var loadIndex = function() { - return $.getJSON(state.basePath+"/search_index.json") - .then(function(data) { - index = lunr.Index.load(data); - }); + var cacheKey = state.revision+":"+"searchIndex"; + var cache = storage.get(cacheKey); + + if (cache) return useIndex(cache); + + $.getJSON(state.basePath+"/search_index.json") + .then(function(index) { + storage.set(cacheKey, index); + return index; + }) + .then(useIndex); }; // Search for a term |