-
-
Notifications
You must be signed in to change notification settings - Fork 43
Description
Looking to add a couple more breaking changes to the worktop@next release cycle – and hopefully it's the last batch!
Before diving into the rundown, it's important to state why these changes are being proposed:
-
There are too many ways to start/initialize a worktop app. Yes, Cloudflare now supports 2 runtimes (Service Worker vs Module Worker), worktop kinda makes it worse by allowing the new
Routerto be booted up 6+ ways! This was initially done for flexibility, but the reality is that people just want to go down 1 path... 2 at the most. (more on this in the 2nd point)Both
worktop/swandworktop/moduleoffer their ownlistenandreplyexports. Thelistens prepare an App for the Service Worker target & thereplys prepare an App for the Module Worker target. This is already 4 options to choose from. Then,worktop/modulealso offersdefine(which I do like) so that you can get a TS-aware helper for defining a full Module Worker, which may have more than just afetchhandler. Finally, there'sworktop/cachewhich also offers its ownlistenandreplyexports. This is now 7 offerings. Finally finally, the first two modules offer their ownconverthelper to go to-and-from the Module to Service Worker signatures, in the event you want/need to mount your own Worker and/or need to convert a third-party function to a different format.I, personally, get super confused with these
converts and don't actually know which to use unless I look at the source. Granted, they're each 2-4 lines of code, but mentally, I lose track if the narrative is "if converting from sw to module, use the X conversion"... good indicator that it should be tossed I think -
Worktop will support multiple environments in the near future. Specifically, I'll be adding Node.js support and Deno support. This brings the total to 4 environments supported, which definitely means that (1) needs to be whittled down before adding any more options.... It also means that there should be a standardized "start the App" export regardless of which environment you're targeting.
With these in mind, I think these are the breaking changes I want/need to make:
-
rename
worktop/module->worktop/cfw- remove
listenexport - remove
convertexport - rename
replytostartexport
- remove
-
worktop/sw- remove
replyexport - remove
convertexport - rename
listentostartexport
- remove
In the future, add the worktop/node and worktop/deno modules, both of which will only have a start export, too.
This creates a workflow wherein:
- you always import
Routerfrom the rootworktopmodule - you always choose a
startmethod from aworktop/<env>module - it's always obvious which environment you're building for, because you chose it...
- if desired, you can setup a build-time alias between
worktop/<env>modules (rare / advanced cases) - worktop can always separate any environment-specific startup or type-changes within the
<env>module bounds
import { Router } from 'worktop';
const API = new Router;
// build/deploy a service worker
// ~> browser OR cloudflare
import { start } from 'worktop/sw';
start(API.run);
// build/deploy a Module Worker
import { start } from 'worktop/cfw';
export default start(API.run);
// ^ environment specific needs
// makes it easy to document too
// build/deploy a Node.js server
import { start } from 'worktop/node';
start(API.run);
// etc...Finally, unrelated to the above, the send export from within the worktop/response module annoyed me 😅 Because you have to return the Response that send builds, the "send" name makes no sense. You don't "return a send" ... you "return a reply"
I'm renaming send to reply – which is also more reason to get rid of the previous/current reply exports from the worktop/(sw|cache|module) modules, which really didn't make sense for them to be called reply in the first place!