Skip to content

[✨] add serviceWorker$ #53

@serbyxp

Description

@serbyxp

Is your feature request related to a problem?

No

Describe the solution you'd like

A service-worker hook. For example serviceWorker$(…)

A hook that can be used anywhere in the project similar to worker$ or server$, but for the current service-worker ( qwikcity). The hook / wrapper function would act as a means for the optimizer to bundle the functions in it, to be ran on the service worker.

export const mySWFunc = serviceWorker$(()=> /* some function/s that will be bundled with the service worker. And executed on the service-worker when called, could be chunked and have their own QRL to be fetched later on when they are needed (same as normal $ but execute on the service-worker)
*/)

Describe alternatives you've considered

Currently adding functions to the service-worker.ts file.

Additional context

Currently there are no hooks to the optimizer / bundler. Adding a listener directly onto service-worker.ts does not take into account the current (built into setupServiceWorker() ) listener which could cause conflicts in the order of execution.

The service-worker does all the fetching for the chunks, acting as a proxy, if something is already cached in the service-worker , there is no entry point or “interception” point to modify the request between the main thread and the service-worker. Nor is there a mechanism to bundle code that should be executed on the service-worker… for example say a normal QRL $
The service-worker has a chunk for that, fetches it as needed, let’s say you want to manipulate it first on the service-worker, wrap it first…

  const domFunc = serviceWorker$(()=> {
  /* this runs on service-worker scope / thread,
     can do some “work” on the service-worker 
     before returning its QRL 
  */
    return $(()=> {
      /* this would run on the main thread
         Doesn’t have to be $ can just be an
         object with data or anything that
         can be serialized…
      */
    });
  });

  onClick$={domFunc}

There is currently an event$() in qwik that is similar to the above that is a QRL that returns a QRL with an implicit first arg which could be a similar way to set the serviceWorker$ … the key difference would be the “scope” of en vocation

Since it’s a QRL it’s not needed right of way…

First func$:
runs on service worker if it returns a QRL… it will need to be fetched before the main thread QRL is needed, so it can do its work before the main thread QRL is fetched and stay in an “awaiting” state to finish and returns the second (main thread) QRL

Second func$:
runs in main thread

I think this is a sort of callback but the callback is present on the service-worker so the second func$ can do some main thread work and return to the service worker

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions