Page MenuHomePhabricator

[keyserver] Serve web worker files
ClosedPublic

Authored by michal on Feb 13 2023, 7:08 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Mar 31, 7:49 AM
Unknown Object (File)
Sun, Mar 31, 7:49 AM
Unknown Object (File)
Sun, Mar 31, 7:49 AM
Unknown Object (File)
Sun, Mar 31, 7:49 AM
Unknown Object (File)
Sun, Mar 31, 7:49 AM
Unknown Object (File)
Sun, Mar 31, 7:49 AM
Unknown Object (File)
Sun, Mar 31, 7:41 AM
Unknown Object (File)
Fri, Mar 29, 2:53 AM
Subscribers

Details

Summary

Part of ENG-2628

We need to serve web/service worker files from the keyserver because during registration we need to pass a url where it's available. So the web app would call e.g. serviceWorkers.register('http://web.comm.app/workers/notif') to register the notification service worker. Service workers work only in a specific "scope" which depends on the url, but we want the notif service worker to apply everywhere and the simplest way is to just set the Service-Worker-Allowed header.

Depends on D6715

Test Plan

Test that the file is correctly served in dev and prod modes. Test with later diffs if the service worker is correctly registered

Diff Detail

Repository
rCOMM Comm
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

Did you test this in dev and prod modes? Is there a way to server a hot-reloading version of this bundle? Does the bundle really contain CommonJS (hence the .cjs extension)?

keyserver/src/responders/webworker-responders.js
6 ↗(On Diff #22432)

I think in all files for responders we have functions definition and then at the bottom of the file export { instead of inline.

Also, maybe the return type will be better for clarity.

10 ↗(On Diff #22432)

Shouldn't we return a different status code when the worker param is not defined at all? (400 or 422 comes to mind but not sure)

15 ↗(On Diff #22432)

Probably I'll be better to leave the default scope. If I understand correctly notifs feature will not have to intercept calls to the network, so there is no need for broad scope - that being said we can have only one SW per scope and if we will want to introduce SW in the future which will intercept network calls we will need to move this one (which seems more complicated than just changing scopes).

Looks like the default scope, without the header or specifying sth like `'Service-Worker-Allowed': '/push-notifs', and then registering SW in the same scope on the client will be sufficient.

  • Changed formatting, remove Service-Worker-Allowed header (it's actually not needed if we don't specify a custom scope during registration)

@ashoat:

  • Tested on prod mode and amended the test plan
  • Hot-reloading was already discussed in D6715
  • From what I understand the default target for webpack is CommonJS so .cjs should be correct.
keyserver/src/responders/webworker-responders.js
10 ↗(On Diff #22432)

In express route parameters can't be null or undefined. So currently it will just redirect to our '*' route returns the web app. We could probably handle it with additional routes for 'worker' and 'worker/' but I'm not sure if that's worth it.

ashoat requested changes to this revision.Feb 15 2023, 7:42 AM
  • From what I understand the default target for webpack is CommonJS so .cjs should be correct.

I'm a bit confused here. commonjs2 isn't an option for target, but it is an option for libraryTarget and library.type. But those only appear to be relevant if the output is a library, and I don't think our output is.

It makes sense that it's only relevant if the output is a library... CommonJS tells us how module 1 will include (require / import) module 2, but in this case we don't have a module loading it.

Can you please actually open the file that you've suffixed with .cjs, check the contents, and see if there is anything "CommonJS" about it?

This revision now requires changes to proceed.Feb 15 2023, 7:42 AM

I'm a bit confused here. commonjs2 isn't an option for target [...]

Sorry, I shouldn't have used the word "target", I meant this option: output.module. In the generated file I can see some webpack boilerplate that defines properties on the exports object so the file should be cjs.

Sorry, I shouldn't have used the word "target", I meant this option: output.module.

Are you sure? The docs say:

output.module is an experimental feature and can only be enabled by setting experiments.outputModule to true.

We don't seem to enable that in D6715.

In the generated file I can see some webpack boilerplate that defines properties on the exports object so the file should be cjs.

This is good to hear, and a more compelling argument. But that might just indicate that the modules *within* the bundle are being bundled together via CommonJS. Is there a global variable named exports that is set?

My expectation would be that this is not being compiled as a "module" at all, eg. nobody is going to import this bundle... the bundle is just going to be run directly by the browser. It would be surprising to me if the browser was looking at exports.

If that's correct, and there is no global exports set, then the suffix should probably be .js and not .cjs.

There's no global exports set and "the bundle is just going to be run directly by the browser" is correct, so I renamed the file to .js.
Additionaly I'm bringing back the Service-Worker-Allowed header because (now that I understand it better) this service worker should have all tabs in scope and needs to be able to claim them all (for example to focus on them after clicking the notification). It's not like it handles only some part of the website.

Please rebase on master, re-run yarn cleaninstall, update this diff, and confirm all CI gates are passing before landing!

for example to focus on them after clicking the notification

If possible, I think it would be best if only the tab that is being brought to focus navigates to the notification. The rest of them should stay where they are

This revision is now accepted and ready to land.Feb 20 2023, 2:22 PM

Rebase

If possible, I think it would be best if only the tab that is being brought to focus navigates to the notification. The rest of them should stay where they are

Yeah, that's how it's going to work