Is there any way to disable service worker initializer ?
Because my web served at example.com with it’s own registered service worker, I embed discourse forum which under the same domain example.com/forum for SSO.
Discourse forum would unregister all my parent page service worker.
AI-generated summary
Disable service worker initializer
Jacky_Cheng asked if it’s possible to disable the service worker initializer due to a conflict with a registered service worker on a parent page (example.com) and a Discourse forum under the same domain (example.com/forum). sam suggested that a theme component or a whitelist of service worker JS files could be used to resolve the issue.
Jacky_Cheng proposed registering and unregistering service workers under the base URL scope instead of the whole domain. sam explained that Discourse only deregisters its own service workers and provided code references.
Jacky_Cheng pointed out an incorrect if condition that deregisters service workers and suggested a correction. sam agreed that the current implementation is aggressive and asked for tgxworld’s thoughts on the matter.
tgxworld fixed the issue by considering subfolder support and Jacky_Cheng confirmed that the fix works. Additionally, Jacky_Cheng suggested removing the Mac condition for Safari support, but tgxworld explained that Discourse is not yet ready to support service workers on Safari due to previous issues.
robbiet480 submitted a PR to re-enable service workers on Apple platforms for push notifications.
I don’t think we have a “disable service worker” switch, you may be able to swing something in a theme component, it makes sense to have a switch or at least a white list of service worker js files that we never de-register for the sub folder case.
Thanks for your reply.
In my opinion, it would be better to register and unregister service worker under the base-url scope, not the whole domain.
Correction, we only de-register stuff that we know is ours:
One spinner is that we de-register apple cause it is buggy at the moment:
As to using scope here, I think it makes sense to make the change to Discourse provided scope is well supported.
if (
registration.active &&
!registration.active.scriptURL.includes(
Discourse.ServiceWorkerURL
)
) {
registration.unregister();
}
shouldn’t this if condition to be this one to only unregister Discourse ServiceWorker ?
if (
registration.active &&
registration.active.scriptURL.includes(
Discourse.ServiceWorkerURL
)
) {
registration.unregister();
}
Yeah reading through this we are being mega aggressive with de-registrations due to historic moves of our url.
@tgxworld any thoughts here? should we be more accurate here.
Could you provide some workaround now ?
I saw some tutorials about plugins?
Can I write a plugin to overwrite the initialize/js file ?
We need to fix the file so we only de-register our stuff, maybe look for a specific string.
@tgxworld can you add this to your list, should be an easy change.
Looks like registration didn’t consider subfolder support as well. Fixed in
https://github.com/discourse/discourse/commit/abbc639e0d362ae90b52c8b5c6922d9e610777f8
@Jacky_Cheng Let me know if this fixes it for you ![]()
Thanks for your reply.
I forgot to bring up that Safari now can support service-worker.
So
const isApple = !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i);
should be remove Mac condition in my opinion.
Also in discourse/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 at abbc639e0d362ae90b52c8b5c6922d9e610777f8 · discourse/discourse · GitHub the else condition would also unregister service worker, it should add the url condition.
Yup we know but Discourse is not ready to support Service workers on Safari yet because it has been problematic for us so far.
It does consider the URL condition because I extracted both of the unregister function calls into a common function.
Oh I see.
I think this would fix my problem.
Thanks a lot.
Just a FYI that I’ve submitted a PR to once again enable the service worker on Apple platforms so that push notifications start working as per my findings here.