fresh icon indicating copy to clipboard operation
fresh copied to clipboard

feat(server): workers in islands

Open deer opened this issue 1 year ago • 2 comments

closes https://github.com/denoland/fresh/issues/1398

Or, at least it attempts to. Not entirely sure what the desired functionality is here. I'd appreciate guidance on some sensible tests and clarification around how we intend this to be used. I improved my test case from the comment I posted last night on the issue. You can see it here. I support two forms of worker import rewriting right now.

  1. do it manually, e.g. new URL("../workers/myFirstWorker.ts", import.meta.url),
  2. use the new hook, e.g. useWorker("../../workers/doStuff.ts");

This is still a bit hacky, since I had to declare a global state in order to share the current request url with the esbuild plugin. You can see that in bundleAssetRoute where I do this:

sharedState = {
  reqUrl: req.url,
};

I then use it later like:

const url = sharedState.reqUrl;

Otherwise I don't know how to rewrite the new URL(.....) and useWorker(....) calls to point to the correct value in the server, since we append that randomNonce to our URLs. Maybe I'm misunderstanding something.

As always looking into a new section of code was very illuminating, although I think I need a bit of help here in order to get this over the line.

deer avatar Jul 04 '23 08:07 deer

I need to use web workers in my site and I'm happy to help, but I have never contributed to fresh before, so I'm not sure how useful I can be here :D

For starters, I have tried to test your changes. I swapped out the import URL for $fresh/ in deno.json to https://raw.githubusercontent.com/deer/fresh/feat_workers_in_islands/, and while I could run

const url = new URL("../workers/worker.ts", import.meta.url);
const worker = new Worker(url.href, { type: "module" });
worker.onmessage = (event) => {
  console.log("rcv", event);
};
worker.postMessage("from parent");
console.log('sent')

successfully, I don't see worker.ts actually being evaluated. Putting a console.log on the top level of worker.ts does not print anything.

Could you provide more exact instructions on how I can test this?

KnorpelSenf avatar Jul 27 '23 17:07 KnorpelSenf

Whatever I'm doing here clearly isn't working on windows (as per the tests), so if you're on windows, please let me know. If you're on mac or linux, did you try looking at the tests I wrote? Look at those, and then see if they work for you, and then what the difference between the tests and your usage is.

deer avatar Jul 28 '23 02:07 deer