Merge pull request #5749 from featheredtoast/service-worker-registration-fixes

Fix - service worker registrations
This commit is contained in:
Jeff Wong 2018-04-10 16:37:06 -07:00 committed by GitHub
commit 4c6d2ed59a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 19 deletions

View File

@ -2,27 +2,25 @@ export default {
name: 'register-service-worker', name: 'register-service-worker',
initialize() { initialize() {
window.addEventListener('load', () => { const isSecured = (document.location.protocol === 'https:') ||
const isSecured = (document.location.protocol === 'https:') || (location.hostname === "localhost");
(location.hostname === "localhost");
const isSupported= isSecured && ('serviceWorker' in navigator); const isSupported= isSecured && ('serviceWorker' in navigator);
if (isSupported) { if (isSupported) {
if (Discourse.ServiceWorkerURL) { if (Discourse.ServiceWorkerURL) {
navigator.serviceWorker navigator.serviceWorker
.register(`${Discourse.BaseUri}/${Discourse.ServiceWorkerURL}`) .register(`${Discourse.BaseUri}/${Discourse.ServiceWorkerURL}`)
.catch(error => { .catch(error => {
Ember.Logger.info(`Failed to register Service Worker: ${error}`); Ember.Logger.info(`Failed to register Service Worker: ${error}`);
});
} else {
navigator.serviceWorker.getRegistrations().then(registrations => {
for(let registration of registrations) {
registration.unregister();
};
}); });
} } else {
navigator.serviceWorker.getRegistrations().then(registrations => {
for(let registration of registrations) {
registration.unregister();
};
});
} }
}); }
} }
}; };

View File

@ -45,7 +45,7 @@
Discourse.ThemeSettings = ps.get('themeSettings'); Discourse.ThemeSettings = ps.get('themeSettings');
Discourse.LetterAvatarVersion = '<%= LetterAvatar.version %>'; Discourse.LetterAvatarVersion = '<%= LetterAvatar.version %>';
Discourse.MarkdownItURL = '<%= asset_url('markdown-it-bundle.js') %>'; Discourse.MarkdownItURL = '<%= asset_url('markdown-it-bundle.js') %>';
Discourse.ServiceWorkerURL = '<%= Rails.application.assets_manifest.assets['service-worker.js'] %>' Discourse.ServiceWorkerURL = Discourse.Environment != "development" ? '<%= Rails.application.assets_manifest.assets['service-worker.js'] %>' : 'service-worker.js';
I18n.defaultLocale = '<%= SiteSetting.default_locale %>'; I18n.defaultLocale = '<%= SiteSetting.default_locale %>';
Discourse.start(); Discourse.start();
Discourse.set('assetVersion','<%= Discourse.assets_digest %>'); Discourse.set('assetVersion','<%= Discourse.assets_digest %>');

View File

@ -734,6 +734,8 @@ Discourse::Application.routes.draw do
# logs. # logs.
get "/service-worker.js" => redirect(relative_url_root + service_worker_asset), format: :js get "/service-worker.js" => redirect(relative_url_root + service_worker_asset), format: :js
get service_worker_asset => "static#service_worker_asset", format: :js get service_worker_asset => "static#service_worker_asset", format: :js
elsif Rails.env.development?
get "/service-worker.js" => "static#service_worker_asset", format: :js
end end
get "cdn_asset/:site/*path" => "static#cdn_asset", format: false get "cdn_asset/:site/*path" => "static#cdn_asset", format: false