Преглед на файлове

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

Jasper van Merle преди 6 години
родител
ревизия
fef9589fae

+ 2 - 0
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')
 

+ 1 - 1
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>

+ 2 - 1
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',

+ 3 - 0
assets/stylesheets/components/_environment.scss.erb

@@ -0,0 +1,3 @@
+._hide-in-development {
+  <%= App.environment != :production ? 'display: none;' : '' %>
+}

+ 1 - 36
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);