whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ru...@apache.org
Subject [whimsy] branch master updated: cache pages to be used by service workers
Date Mon, 25 Jul 2016 19:12:34 GMT
This is an automated email from the ASF dual-hosted git repository.

rubys pushed a commit to branch master
in repository https://git-dual.apache.org/repos/asf/whimsy.git

The following commit(s) were added to refs/heads/master by this push:
       new  796a4a6   cache pages to be used by service workers
796a4a6 is described below

commit 796a4a6b2dd74f69a3052f9f3b7f82d9eba4ba8b
Author: Sam Ruby <rubys@intertwingly.net>
AuthorDate: Mon Jul 25 15:12:07 2016 -0400

    cache pages to be used by service workers
---
 www/board/agenda/routes.rb                    | 10 ++++-
 www/board/agenda/views/app.js.rb              |  1 +
 www/board/agenda/views/bootstrap.html.rb      | 21 +++++++++
 www/board/agenda/views/models/events.js.rb    |  4 ++
 www/board/agenda/views/models/pagecache.js.rb | 64 +++++++++++++++++++++++++++
 www/board/agenda/views/pages/cache.js.rb      |  6 ++-
 6 files changed, 104 insertions(+), 2 deletions(-)

diff --git a/www/board/agenda/routes.rb b/www/board/agenda/routes.rb
index a62db84..15f1fa8 100755
--- a/www/board/agenda/routes.rb
+++ b/www/board/agenda/routes.rb
@@ -114,7 +114,15 @@ get %r{/(\d\d\d\d-\d\d-\d\d)/(.*)} do |date, path|
 
   @cssmtime = File.mtime('public/stylesheets/app.css').to_i
 
-  _html :'main'
+  if path == 'bootstrap.html'
+    @page[:path] = ''
+    @page[:parsed] = [@page[:parsed].first]
+    @page[:digest] = nil
+    @page[:etag] = nil
+    _html :bootstrap
+  else
+    _html :main
+  end
 end
 
 # append slash to agenda page if not present
diff --git a/www/board/agenda/views/app.js.rb b/www/board/agenda/views/app.js.rb
index 448002a..9242ef3 100644
--- a/www/board/agenda/views/app.js.rb
+++ b/www/board/agenda/views/app.js.rb
@@ -54,6 +54,7 @@ require_relative 'elements/info'
 
 # Model
 require_relative 'models/events'
+require_relative 'models/pagecache'
 require_relative 'models/agenda'
 require_relative 'models/minutes'
 require_relative 'models/chat'
diff --git a/www/board/agenda/views/bootstrap.html.rb b/www/board/agenda/views/bootstrap.html.rb
new file mode 100644
index 0000000..3ebe956
--- /dev/null
+++ b/www/board/agenda/views/bootstrap.html.rb
@@ -0,0 +1,21 @@
+#
+# Content-less stub HTML which will fetch and display agenda
+#
+_html do
+  _base href: @base
+  _title 'ASF Board Agenda'
+  _link rel: 'stylesheet', href: "../stylesheets/app.css?#{@cssmtime}"
+  _meta name: 'viewport', content: 'width=device-width, initial-scale=1.0'
+
+  _div.main! do
+    _header
+    _footer
+  end
+
+  _script src: '../app.js', lang: 'text/javascript'
+  _script %{
+    React.render(React.createElement(Main, 
+      #{JSON.generate(server: @server, page: @page)}),
+      document.getElementById("main"))
+  }
+end
diff --git a/www/board/agenda/views/models/events.js.rb b/www/board/agenda/views/models/events.js.rb
index 351b8ae..8d20572 100644
--- a/www/board/agenda/views/models/events.js.rb
+++ b/www/board/agenda/views/models/events.js.rb
@@ -40,6 +40,10 @@ class Events
   end
 
   def self.monitor()
+    window.addEventListener :load do |event|
+      PageCache.preload()
+    end
+
     @@prefix = JSONStorage.prefix
 
     # pick something unique to identify this tab/window
diff --git a/www/board/agenda/views/models/pagecache.js.rb b/www/board/agenda/views/models/pagecache.js.rb
new file mode 100644
index 0000000..a41779b
--- /dev/null
+++ b/www/board/agenda/views/models/pagecache.js.rb
@@ -0,0 +1,64 @@
+#
+# A cache of agenda related pages, useful for:
+#
+#  1) quick loading of possibly stale data, which will be updated with
+#     current information as it becomes available.
+#
+#  2) offline access to the agenda tool
+#
+
+class PageCache
+
+  # is page cache available?
+  def self.enabled
+    unless location.protocol == 'https:' or location.hostname == 'localhost'
+      return false
+    end
+
+    return defined? ServiceWorker
+  end
+
+  # fetch and cache page and referenced scripts and stylesheets
+  def self.preload()
+    return unless PageCache.enabled?
+
+    base = document.getElementsByTagName('base')[0].href
+    fetch_options = {credentials: 'include'}
+
+    fetch('bootstrap.html', fetch_options).then do |response|
+      response.clone().text().then do |text|
+        urls = []
+
+        # search body text for scripts
+        script = Regexp.new(/<script.*?>/, 'g')
+        matches = text.match(script)
+        matches.each do |match|
+          src = match.match(/src="(.*?)"/)
+          urls << URL.new(src[1], base) if src
+        end
+
+        # search body text for links to stylesheets
+        links = Regexp.new(/<link.*?>/, 'g')
+        matches = text.match(links)
+        matches.each do |match|
+          href = match.match(/href="(.*?)"/)
+          urls << URL.new(href[1], base) if href
+        end
+
+        # add bootstrap.html and each URL to the cache.
+        caches.open('board/agenda').then do |cache|
+          agenda = Agenda.file[/\d+_\d+_\d+/].gsub('_', '-')
+          cache.put("#{agenda}.html", response)
+
+          urls.each do |url|
+            fetch(url.pathname, fetch_options).then do |response|
+              basename = url.pathname.split('/').pop().split('?')[0]
+              cache.put(basename, response)
+            end
+          end
+        end
+      end
+    end
+  end
+
+end
diff --git a/www/board/agenda/views/pages/cache.js.rb b/www/board/agenda/views/pages/cache.js.rb
index 3747877..4ddf95e 100644
--- a/www/board/agenda/views/pages/cache.js.rb
+++ b/www/board/agenda/views/pages/cache.js.rb
@@ -68,7 +68,11 @@ class CacheStatus < React
   def componentWillReceiveProps()
     if defined? caches
       caches.open('board/agenda').then do |cache|
-        @cache = cache.keys()
+        cache.matchAll().then do |responses|
+          cache = responses.map {|response| response.url}
+          cache.sort()
+          @cache = cache
+        end
       end
 
       navigator.serviceWorker.getRegistrations().then do |registrations|

-- 
To stop receiving notification emails like this one, please contact
['"commits@whimsical.apache.org" <commits@whimsical.apache.org>'].

Mime
View raw message