From 9ffb1a062f6bfa4c3253eb40c89ba54e6e429fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Sun, 6 Apr 2014 17:13:10 -0700 Subject: [PATCH] Use cache in localstorage to store search index --- lib/generate/site/index.js | 3 +++ theme/assets/app.js | 26 ++++++++++++++++++++------ theme/javascript/core/search.js | 23 ++++++++++++++++++----- theme/javascript/core/state.js | 3 ++- theme/templates/site.html | 2 +- 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/lib/generate/site/index.js b/lib/generate/site/index.js index 474de2aab..8b080f243 100644 --- a/lib/generate/site/index.js +++ b/lib/generate/site/index.js @@ -26,6 +26,7 @@ var Generator = function() { // Attach methods to instance _.bindAll(this); + this.revision = Date.now(); this.indexer = indexer(); // Load base template @@ -41,6 +42,8 @@ Generator.prototype._writeTemplate = function(tpl, options, output) { return Q() .then(function(sections) { return tpl(_.extend({ + revision: that.revision, + title: that.options.title, description: that.options.description, diff --git a/theme/assets/app.js b/theme/assets/app.js index ec6285ff5..f2956de43 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 diff --git a/theme/javascript/core/search.js b/theme/javascript/core/search.js index a5399d996..bc1a31b3b 100644 --- a/theme/javascript/core/search.js +++ b/theme/javascript/core/search.js @@ -2,19 +2,32 @@ define([ "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 diff --git a/theme/javascript/core/state.js b/theme/javascript/core/state.js index 5ed11e8a9..79876b95c 100644 --- a/theme/javascript/core/state.js +++ b/theme/javascript/core/state.js @@ -8,6 +8,7 @@ define([ 'githubId': $book.data("github"), 'level': $book.data("level"), - 'basePath': $book.data("basepath") + 'basePath': $book.data("basepath"), + 'revision': $book.data("revision") }; }); \ No newline at end of file diff --git a/theme/templates/site.html b/theme/templates/site.html index 5bb3c8b67..5771c729f 100644 --- a/theme/templates/site.html +++ b/theme/templates/site.html @@ -2,7 +2,7 @@ {% block title %}{{ progress.current.title }}{% parent %}{% endblock %} {% block content %} -
+
{% include "includes/book/header.html" %} {% include "includes/book/summary.html" %}