Skip to content

Commit

Permalink
Add option to make storage persistent to Offline page.
Browse files Browse the repository at this point in the history
Fixes #703
  • Loading branch information
pwnall committed Sep 14, 2020
1 parent 4eb4bfe commit d616cf9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
8 changes: 8 additions & 0 deletions assets/javascripts/templates/error_tmpl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ app.templates.offlineError = (reason, exception) ->

error 'Offline mode is unavailable.', reason

app.templates.persistenceError = (exception) ->
reason = if exception
"""<code class="_label">#{exception.name}: #{exception.message}</code>"""
else
"""Bookmark this site and try again."""

error 'Persistence request denied by browser.', reason

app.templates.unsupportedBrowser = """
<div class="_fail">
<h1 class="_fail-title">Your browser is unsupported, sorry.</h1>
Expand Down
23 changes: 21 additions & 2 deletions assets/javascripts/templates/pages/offline_tmpl.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
app.templates.offlinePage = (docs) -> """
app.templates.offlinePage = (docs, hasPersistence, isPersistent) -> """
<h1 class="_lined-heading">Offline Documentation</h1>
<div class="_docs-tools">
Expand All @@ -21,7 +21,10 @@ app.templates.offlinePage = (docs) -> """
#{docs}
</table>
</div>
<p class="_note"><strong>Note:</strong> 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.
</div>
<div id="_offline-persistence-note">
#{offlinePersistenceNote(hasPersistence, isPersistent)}
</div>
<h2 class="_block-heading">Questions & Answers</h2>
<dl>
<dt>How does this work?
Expand Down Expand Up @@ -78,3 +81,19 @@ app.templates.offlineDoc = (doc, status) ->
"""

html + '</tr>'

offlinePersistenceNote = (hasPersistence, isPersistent) ->
if isPersistent
return ''

html = """
<p class="_note">
<strong>Note:</strong> 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 += ' <button type="button" class ="_btn-link _bold" data-enable-persistence>Enable persistent storage</button>.'
else
html += ' Load this page before going offline to make sure the data is still there.'

return html
32 changes: 28 additions & 4 deletions assets/javascripts/views/content/offline_page.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -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)

0 comments on commit d616cf9

Please sign in to comment.