Code-server: service-worker script wont load from non-root path

Created on 10 May 2019  路  11Comments  路  Source: cdr/code-server

  • code-server version: 1.939-vsc1.33.1
  • OS Version: irrelevant

Description

We are running code-server behind a reverse proxy that uses paths instead of distinct domains. e.g. domain.com/ide/123456. The service-worker script introduced in #154 will try to load the script from domain.com/service-worker.js instead of domain.com/ide/123456/service-worker.js.

~It should be sufficient~ (it's not) to remove the leading slash in navigator.serviceWorker.register("/service-worker.js");. So either navigator.serviceWorker.register("./service-worker.js"); or navigator.serviceWorker.register("service-worker.js");. But I haven't tested all scenarios, yet.

bug

All 11 comments

I was messing with this. The problem is going to be the scope also. I think navigator.serviceWorker.register('service-worker.js', {scope: './'}); will operate correctly. This way the service worker doesn't try to apply to other paths. Most of the time when I do a PWA with a service worker though I use an absolute path. I guess the other approach would be to use the window.location to build something correctly.

Ah, there are some consequences here with the stuff generated in the manifests, etc. I am not sure if they are using workbox, but that can take care of some madness, but I think it introduces other madness =)

Just want to chime in that I'm hitting this as well.

I'm trying to run coder on Kubernetes behind a reverse proxy. It would be great to get this fixed.

cc @code-asher

@lucacasonato as well

I would add a --root flag and then make that the scope and the root path for all assets including the service worker.

The problem is you might not know what the mount point is when the server is started. I was trying to play games with location but then I got bogged down in CSP hell. That being said, maybe --root would work if there was a way to have the index.html as a template and its expanded from cli values.

A "--root" flag would work in our (Kubeflow)'s use case. We know the root path on which we will be serving it.

/cc @aronchick

@geiseri what issues did you encountered while working on this? The simple navigator.serviceWorker.register('service-worker.js', {scope: './'}) solution seems to work fine for me.

Paths from the manifest and links to assets inside the index.html shouldnt be a problem:

  • Relative paths inside the index.html are loaded relative to the document (you only have to ensure your URL ends in a slash which should be the case when using a reverse-proxy anyway)
  • Relative paths inside the manifest are loaded relative to the manifest-URL

I would love to see a solution without another CLI-option. The IDE shouldn't care about where it is mounted.

Actually, the register worked, it was the resulting CSP issues that popped up later. I got around some with making workbox local vs hosted on the CDN. I will PR that part while I work out a better solution on the CSP.

I opene #768 since I couldn't find this. Is it possible to add more tags here like reverse-proxy, ingress, kubernetes?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Arsaev picture Arsaev  路  3Comments

tecosaur picture tecosaur  路  3Comments

nol166 picture nol166  路  3Comments

avelino picture avelino  路  3Comments

lshamis picture lshamis  路  3Comments