PERF: Have nginx cache and serve the service worker file.
This commit is contained in:
parent
d8b4627fc8
commit
28365f8ae5
|
@ -2,27 +2,27 @@ export default {
|
||||||
name: 'register-service-worker',
|
name: 'register-service-worker',
|
||||||
|
|
||||||
initialize() {
|
initialize() {
|
||||||
// only allow service worker on android for now
|
window.addEventListener('load', () => {
|
||||||
if (!/(android)/i.test(navigator.userAgent)) {
|
const isSecured = (document.location.protocol === 'https:') ||
|
||||||
|
|
||||||
// remove old service worker
|
|
||||||
if ('serviceWorker' in navigator && navigator.serviceWorker.getRegistrations) {
|
|
||||||
navigator.serviceWorker.getRegistrations().then((registrations) => {
|
|
||||||
for(let registration of registrations) {
|
|
||||||
registration.unregister();
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
const isSecure = (document.location.protocol === 'https:') ||
|
|
||||||
(location.hostname === "localhost");
|
(location.hostname === "localhost");
|
||||||
|
|
||||||
|
const isSupported= isSecured && ('serviceWorker' in navigator);
|
||||||
|
|
||||||
if (isSecure && ('serviceWorker' in navigator)) {
|
if (isSupported) {
|
||||||
navigator.serviceWorker.register(`${Discourse.BaseUri}/service-worker.js`);
|
if (Discourse.ServiceWorkerURL) {
|
||||||
|
navigator.serviceWorker
|
||||||
|
.register(`${Discourse.BaseUri}/${Discourse.ServiceWorkerURL}`)
|
||||||
|
.catch(error => {
|
||||||
|
Ember.Logger.info(`Failed to register Service Worker: ${error}`);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
navigator.serviceWorker.getRegistrations().then(registrations => {
|
||||||
|
for(let registration of registrations) {
|
||||||
|
registration.unregister();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -148,9 +148,9 @@ class StaticController < ApplicationController
|
||||||
def service_worker_asset
|
def service_worker_asset
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.js do
|
format.js do
|
||||||
|
# https://github.com/w3c/ServiceWorker/blob/master/explainer.md#updating-a-service-worker
|
||||||
# we take 1 hour to give a new service worker to all users
|
# Maximum cache that the service worker will respect is 24 hours.
|
||||||
immutable_for 1.hour
|
immutable_for 24.hours
|
||||||
|
|
||||||
render(
|
render(
|
||||||
plain: Rails.application.assets_manifest.find_sources('service-worker.js').first,
|
plain: Rails.application.assets_manifest.find_sources('service-worker.js').first,
|
||||||
|
|
|
@ -44,6 +44,9 @@
|
||||||
Discourse.SiteSettings = ps.get('siteSettings');
|
Discourse.SiteSettings = ps.get('siteSettings');
|
||||||
Discourse.LetterAvatarVersion = '<%= LetterAvatar.version %>';
|
Discourse.LetterAvatarVersion = '<%= LetterAvatar.version %>';
|
||||||
Discourse.MarkdownItURL = '<%= asset_url('markdown-it-bundle.js') %>';
|
Discourse.MarkdownItURL = '<%= asset_url('markdown-it-bundle.js') %>';
|
||||||
|
<%- if DiscoursePluginRegistry.service_workers.present? %>
|
||||||
|
Discourse.ServiceWorkerURL = '<%= Rails.application.assets_manifest.assets['service-worker.js'] %>'
|
||||||
|
<%- end %>
|
||||||
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 %>');
|
||||||
|
|
|
@ -188,7 +188,7 @@ server {
|
||||||
# This big block is needed so we can selectively enable
|
# This big block is needed so we can selectively enable
|
||||||
# acceleration for backups and avatars
|
# acceleration for backups and avatars
|
||||||
# see note about repetition above
|
# see note about repetition above
|
||||||
location ~ ^/(letter_avatar/|user_avatar|highlight-js|stylesheets|favicon/proxied) {
|
location ~ ^/(letter_avatar/|user_avatar|highlight-js|stylesheets|favicon/proxied|service-worker-.*.js) {
|
||||||
proxy_set_header Host $http_host;
|
proxy_set_header Host $http_host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
|
|
@ -696,7 +696,9 @@ Discourse::Application.routes.draw do
|
||||||
post "draft" => "draft#update"
|
post "draft" => "draft#update"
|
||||||
delete "draft" => "draft#destroy"
|
delete "draft" => "draft#destroy"
|
||||||
|
|
||||||
get "service-worker" => "static#service_worker_asset", format: :js
|
if service_worker_asset = Rails.application.assets_manifest.assets['service-worker.js']
|
||||||
|
get service_worker_asset => "static#service_worker_asset", format: :js
|
||||||
|
end
|
||||||
|
|
||||||
get "cdn_asset/:site/*path" => "static#cdn_asset", format: false
|
get "cdn_asset/:site/*path" => "static#cdn_asset", format: false
|
||||||
get "brotli_asset/*path" => "static#brotli_asset", format: false
|
get "brotli_asset/*path" => "static#brotli_asset", format: false
|
||||||
|
|
Loading…
Reference in New Issue