Skip to content

Commit

Permalink
Simplify service worker and don't show analytics setting/notification…
Browse files Browse the repository at this point in the history
… in development
  • Loading branch information
jmerle committed Aug 17, 2019
1 parent 5e23f64 commit fef9589
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 38 deletions.
2 changes: 2 additions & 0 deletions assets/javascripts/lib/page.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ page.track = (fn) ->
return

track = ->
return unless app.config.env == 'production'

consentGiven = Cookies.get('analyticsConsent')
consentAsked = Cookies.get('analyticsConsentAsked')

Expand Down
2 changes: 1 addition & 1 deletion assets/javascripts/templates/pages/settings_tmpl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
3 changes: 2 additions & 1 deletion assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
'components/path',
'components/notice',
'components/prism',
'components/mobile';
'components/mobile',
'components/environment';

@import 'pages/simple',
'pages/angular',
Expand Down
3 changes: 3 additions & 0 deletions assets/stylesheets/components/_environment.scss.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
._hide-in-development {
<%= App.environment != :production ? 'display: none;' : '' %>
}
37 changes: 1 addition & 36 deletions views/service-worker.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit fef9589

Please sign in to comment.