From d616cf9ef8915ab83542ace9bf9f270d81e2dbc8 Mon Sep 17 00:00:00 2001 From: Victor Costan Date: Sun, 13 Sep 2020 21:26:19 -0700 Subject: [PATCH] Add option to make storage persistent to Offline page. Fixes #703 --- .../javascripts/templates/error_tmpl.coffee | 8 +++++ .../templates/pages/offline_tmpl.coffee | 23 +++++++++++-- .../views/content/offline_page.coffee | 32 ++++++++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/assets/javascripts/templates/error_tmpl.coffee b/assets/javascripts/templates/error_tmpl.coffee index 9cca1f9d32..5f2bd4035e 100644 --- a/assets/javascripts/templates/error_tmpl.coffee +++ b/assets/javascripts/templates/error_tmpl.coffee @@ -51,6 +51,14 @@ app.templates.offlineError = (reason, exception) -> error 'Offline mode is unavailable.', reason +app.templates.persistenceError = (exception) -> + reason = if exception + """#{exception.name}: #{exception.message}""" + else + """Bookmark this site and try again.""" + + error 'Persistence request denied by browser.', reason + app.templates.unsupportedBrowser = """

Your browser is unsupported, sorry.

diff --git a/assets/javascripts/templates/pages/offline_tmpl.coffee b/assets/javascripts/templates/pages/offline_tmpl.coffee index bb9e06e803..9894adeb70 100644 --- a/assets/javascripts/templates/pages/offline_tmpl.coffee +++ b/assets/javascripts/templates/pages/offline_tmpl.coffee @@ -1,4 +1,4 @@ -app.templates.offlinePage = (docs) -> """ +app.templates.offlinePage = (docs, hasPersistence, isPersistent) -> """

Offline Documentation

@@ -21,7 +21,10 @@ app.templates.offlinePage = (docs) -> """ #{docs}
-

Note: your browser may delete DevDocs's offline data if your computer is running low on disk space and you haven't used the app in a while. Load this page before going offline to make sure the data is still there. +

+
+ #{offlinePersistenceNote(hasPersistence, isPersistent)} +

Questions & Answers

How does this work? @@ -78,3 +81,19 @@ app.templates.offlineDoc = (doc, status) -> """ html + '' + +offlinePersistenceNote = (hasPersistence, isPersistent) -> + if isPersistent + return '' + + html = """ +

+ Note: your browser may delete DevDocs's offline data if your computer is running low on disk space and you haven't used the app in a while. + """ + + if hasPersistence + html += ' .' + else + html += ' Load this page before going offline to make sure the data is still there.' + + return html diff --git a/assets/javascripts/views/content/offline_page.coffee b/assets/javascripts/views/content/offline_page.coffee index 2f0d615f47..d7d5e65190 100644 --- a/assets/javascripts/views/content/offline_page.coffee +++ b/assets/javascripts/views/content/offline_page.coffee @@ -20,10 +20,11 @@ class app.views.OfflinePage extends app.View if statuses is false @html @tmpl('offlineError', app.db.reason, app.db.error) else - html = '' - html += @renderDoc(doc, statuses[doc.slug]) for doc in app.docs.all() - @html @tmpl('offlinePage', html) - @refreshLinks() + @checkPersistence (hasPersistence, isPersisted) => + html = '' + html += @renderDoc(doc, statuses[doc.slug]) for doc in app.docs.all() + @html @tmpl('offlinePage', html, hasPersistence, isPersisted) + @refreshLinks() return return @@ -60,6 +61,8 @@ class app.views.OfflinePage extends app.View return unless action isnt 'uninstall' or window.confirm('Uninstall all docs?') app.db.migrate() $.click(el) for el in @findAll("[data-action='#{action}']") + else if el.hasAttribute('data-enable-persistence') + @requestPersistence() return onInstallSuccess: (doc) -> @@ -90,3 +93,24 @@ class app.views.OfflinePage extends app.View if event.target.name is 'autoUpdate' app.settings.set 'manualUpdate', !event.target.checked return + + checkPersistence: (callback) -> + if navigator.storage and navigator.storage.persisted + navigator.storage.persisted().then((persisted) -> + callback true, persisted + ).catch -> + callback false, persisted + else + callback false, false + + requestPersistence: -> + navigator.storage.persist().then((success) => + @onPersistenceRequestCompleted success + ).catch (exception) => + @onPersistenceRequestCompleted false, exception + + onPersistenceRequestCompleted: (success, exception) -> + if success + @render() + else + @html @tmpl('persistenceError', exception)