History

Add preference to bypass service worker cache

Jasper van Merle committed 7 years ago

Commit 0e9aa881 parent ec8616e6 files · +28 −1Browse files

Unified · Split

6 files
  1. assets/javascripts/app/settings.coffee +6 −0
  2. assets/javascripts/templates/pages/settings_tmpl.coffee +4 −0
  3. assets/javascripts/views/content/settings_page.coffee +7 −0
  4. docs/adding-docs.md +2 −0
  5. lib/app.rb +4 −0
  6. views/service-worker.js.erb +5 −1
assets/javascripts/app/settings.coffee+6−0
@@ -5,6 +5,7 @@ class app.Settings · 4 unmodified lines
55 'manualUpdate'
66 'fastScroll'
77 'arrowScroll'
8+ 'bypassCache'
89 'docs'
910 'dark'
1011 'layout'
@@ -91,6 +92,11 @@ class app.Settings · 80 unmodified lines
9192 @set 'size', value
9293 return
9394
95+ setBypassCache: (value) ->
96+ @set 'bypassCache', value
97+ app.serviceWorker?.updateInBackground()
98+ return
99+
94100 dump: ->
95101 @store.dump()
96102
assets/javascripts/templates/pages/settings_tmpl.coffee+4−0
@@ -15,6 +15,10 @@ app.templates.settingsPage = (settings) -> """ · 14 unmodified lines
1515 <input type="checkbox" form="settings" name="layout" value="_sidebar-hidden"#{if settings['_sidebar-hidden'] then ' checked' else ''}>Automatically hide and show the sidebar
1616 <small>Tip: drag the edge of the sidebar to resize it.</small>
1717 </label>
18+ <label class="_settings-label">
19+ <input type="checkbox" form="settings" name="bypassCache" value="1"#{if settings.bypassCache then ' checked' else ''}>Bypass Service Worker cache
20+ <small>When this is checked, the Service Worker will always fetch the latest version of requested files. Useful when making changes to the DevDocs source code.</small>
21+ </label>
1822 </div>
1923 </div>
2024
assets/javascripts/views/content/settings_page.coffee+7−0
@@ -14,6 +14,7 @@ class app.views.SettingsPage extends app.View · 13 unmodified lines
1414 settings.dark = app.settings.get('dark')
1515 settings.smoothScroll = !app.settings.get('fastScroll')
1616 settings.arrowScroll = app.settings.get('arrowScroll')
17+ settings.bypassCache = app.settings.get('bypassCache')
1718 settings[layout] = app.settings.hasLayout(layout) for layout in app.settings.LAYOUTS
1819 settings
1920
@@ -32,6 +33,10 @@ class app.views.SettingsPage extends app.View · 12 unmodified lines
3233 app.settings.set('fastScroll', !enable)
3334 return
3435
36+ toggleBypassCache: (enable) ->
37+ app.settings.setBypassCache(!!enable)
38+ return
39+
3540 toggle: (name, enable) ->
3641 app.settings.set(name, enable)
3742 return
@@ -75,6 +80,8 @@ class app.views.SettingsPage extends app.View · 37 unmodified lines
7580 @toggleSmoothScroll input.checked
7681 when 'import'
7782 @import input.files[0], input
83+ when 'bypassCache'
84+ @toggleBypassCache input.checked
7885 else
7986 @toggle input.name, input.checked
8087 return
docs/adding-docs.md+2−0
@@ -2,6 +2,8 @@ Adding a documentation may look like a daunting task but once you get the hang o · 1 unmodified line
22
33 **Note:** please read the [contributing guidelines](../.github/CONTRIBUTING.md) before submitting a new documentation.
44
5+**Note:** when editing any of the files in the `assets` directory or the `public` directory, you'll have to bypass the service worker cache. To do this, go to the Preferences page on your local instance of DevDocs, check "Bypass Service Worker cache" and refresh the page.
6+
57 1. Create a subclass of `Docs::UrlScraper` or `Docs::FileScraper` in the `lib/docs/scrapers/` directory. Its name should be the [PascalCase](http://api.rubyonrails.org/classes/String.html#method-i-camelize) equivalent of the filename (e.g. `my_doc` → `MyDoc`)
68 2. Add the appropriate class attributes and filter options (see the [Scraper Reference](./scraper-reference.md) page).
79 3. Check that the scraper is listed in `thor docs:list`.
lib/app.rb+4−0
@@ -220,6 +220,10 @@ class App < Sinatra::Application · 219 unmodified lines
220220 app_theme == 'dark'
221221 end
222222
223+ def bypass_cache?
224+ !memoized_cookies['bypassCache'].nil?
225+ end
226+
223227 def redirect_via_js(path)
224228 response.set_cookie :initial_path, value: path, expires: Time.now + 15, path: '/'
225229 redirect '/', 302
views/service-worker.js.erb+5−1
@@ -31,13 +31,16 @@ self.addEventListener('activate', event => { · 30 unmodified lines
3131 event.waitUntil((async () => {
3232 const keys = await caches.keys();
3333 const jobs = keys.map(key => key !== cacheName ? caches.delete(key) : Promise.resolve());
34− return await Promise.all(jobs);
34+ return Promise.all(jobs);
3535 })());
3636 });
3737
3838 <%# Handle HTTP requests %>
3939 self.addEventListener('fetch', event => {
4040 event.respondWith((async () => {
41+ <% if bypass_cache? %>
42+ return fetch(event.request);
43+ <% else %>
4144 const cachedResponse = await caches.match(event.request);
4245 if (cachedResponse) return cachedResponse;
4346
@@ -56,5 +59,6 @@ self.addEventListener('fetch', event => { · 12 unmodified lines
5659
5760 throw err;
5861 }
62+ <% end %>
5963 })());
6064 });