Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): workers in islands #1407

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

deer
Copy link
Contributor

@deer deer commented Jul 4, 2023

closes #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.

@KnorpelSenf
Copy link

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?

@deer
Copy link
Contributor Author

deer commented Jul 28, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants