[devdocsgjs/main: 840/1867] Simplify service worker and don't show analytics setting/notification in development




commit fef9589fae4a8490ec9687d2e788a060292914c1
Author: Jasper van Merle <jaspervmerle gmail com>
Date:   Sat Aug 17 22:55:40 2019 +0200

    Simplify service worker and don't show analytics setting/notification in development

 assets/javascripts/lib/page.coffee                 |  2 ++
 .../templates/pages/settings_tmpl.coffee           |  2 +-
 assets/stylesheets/application.css.scss            |  3 +-
 .../stylesheets/components/_environment.scss.erb   |  3 ++
 views/service-worker.js.erb                        | 37 +---------------------
 5 files changed, 9 insertions(+), 38 deletions(-)
---
diff --git a/assets/javascripts/lib/page.coffee b/assets/javascripts/lib/page.coffee
index 91d73432..7a5329f6 100644
--- a/assets/javascripts/lib/page.coffee
+++ b/assets/javascripts/lib/page.coffee
@@ -199,6 +199,8 @@ page.track = (fn) ->
   return
 
 track = ->
+  return unless app.config.env == 'production'
+
   consentGiven = Cookies.get('analyticsConsent')
   consentAsked = Cookies.get('analyticsConsentAsked')
 
diff --git a/assets/javascripts/templates/pages/settings_tmpl.coffee 
b/assets/javascripts/templates/pages/settings_tmpl.coffee
index 94afe3df..d5cb0985 100644
--- a/assets/javascripts/templates/pages/settings_tmpl.coffee
+++ b/assets/javascripts/templates/pages/settings_tmpl.coffee
@@ -19,7 +19,7 @@ app.templates.settingsPage = (settings) -> """
         <input type="checkbox" form="settings" name="autoInstall" value="_auto-install"#{if 
settings.autoInstall then ' checked' else ''}>Automatically download documentation for offline use
         <small>Only enable this when bandwidth isn't a concern to you.</small>
       </label>
-      <label class="_settings-label">
+      <label class="_settings-label _hide-in-development">
         <input type="checkbox" form="settings" name="analyticsConsent"#{if settings.analyticsConsent then ' 
checked' else ''}>Enable tracking cookies
         <small>With this checked, we enable Google Analytics and Gauges to collect anonymous traffic 
information.</small>
       </label>
diff --git a/assets/stylesheets/application.css.scss b/assets/stylesheets/application.css.scss
index 05f8bb2f..1d058ebe 100644
--- a/assets/stylesheets/application.css.scss
+++ b/assets/stylesheets/application.css.scss
@@ -29,7 +29,8 @@
         'components/path',
         'components/notice',
         'components/prism',
-        'components/mobile';
+        'components/mobile',
+        'components/environment';
 
 @import 'pages/simple',
         'pages/angular',
diff --git a/assets/stylesheets/components/_environment.scss.erb 
b/assets/stylesheets/components/_environment.scss.erb
new file mode 100644
index 00000000..5e2201c3
--- /dev/null
+++ b/assets/stylesheets/components/_environment.scss.erb
@@ -0,0 +1,3 @@
+._hide-in-development {
+  <%= App.environment != :production ? 'display: none;' : '' %>
+}
diff --git a/views/service-worker.js.erb b/views/service-worker.js.erb
index 4a0bd9d0..81689a2e 100644
--- a/views/service-worker.js.erb
+++ b/views/service-worker.js.erb
@@ -11,34 +11,6 @@ const urlsToCache = [
   '<%= doc_index_urls.join "',\n  '" %>',
 ];
 
-<%# Clone a request with the mode set to 'cors' %>
-function corsify(request) {
-  const options = {
-    mode: 'cors',
-  };
-
-  const keys = [
-    'body',
-    'cache',
-    'credentials',
-    'headers',
-    'integrity',
-    'keepalive',
-    'method',
-    'redirect',
-    'referrer',
-    'referrerPolicy',
-    'signal',
-    'window',
-  ];
-
-  for (const key of keys) {
-    options[key] = request[key];
-  }
-
-  return new Request(request.url, options);
-}
-
 <%# Set-up the cache %>
 self.addEventListener('install', event => {
   self.skipWaiting();
@@ -64,14 +36,7 @@ self.addEventListener('fetch', event => {
     if (cachedResponse) return cachedResponse;
 
     try {
-      const response = await fetch(corsify(event.request));
-
-      <%# If the status code is 0 it means the response is opaque %>
-      <%# If the response is opaque it's not possible to read whether it is successful or not, so we assume 
it is %>
-      if (!response.ok && response.status !== 0) {
-        throw new Error(`The HTTP request failed with status code ${response.status}`);
-      }
-
+      const response = await fetch(event.request);
       return response;
     } catch (err) {
       const url = new URL(event.request.url);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]