-
Notifications
You must be signed in to change notification settings - Fork 22
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
Use AsyncLocalStorage #40
base: main
Are you sure you want to change the base?
Conversation
const articles = await Article.with("user").orderBy("createdAt", "desc"); | ||
const articles = await Article.with("user") | ||
.orderBy("createdAt", "desc") | ||
.get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A limitation with ALS in CF Workers today is that custom thenables don't properly retain context: https://twitter.com/jasnell/status/1634764772121145344
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -1,4 +1,5 @@ | |||
import { marked } from "marked"; | |||
// TODO: Find a lighter solution for syntax highlighting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to ALS per se, but my CF Workers bundle is like 4MB because of this 🙃
@@ -51,8 +51,7 @@ export async function handleFetch<Env extends { APP_KEY: string }>( | |||
}, | |||
async () => { | |||
/** | |||
* We inject env and session into the Remix load context. | |||
* Someday, we could replace this with AsyncLocalStorage. | |||
* TODO: REMOVE THIS since we're using AsyncLocalStorage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just deprecate it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, I think I'll still put some separate things in the context for now. But a lot of people probably never should need to care about context.
packages/superflare/src/context.ts
Outdated
return await asyncLocalStorage.run({}, async () => { | ||
/** | ||
* I'm being extra cautious to not set the object value until we're _inside_ the asyncLocalStorage.run | ||
* closure. This is to prevent accidental leaks between requests. I don't know if it's necessary. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Verify whether this is necessary or if I can just populate an object outside of the run.
👀 |
Now that Cloudflare supports ALS using the
nodejs_compat
compatibility flag, this allows us to attach Superflare config to a request-local context. This is way better than assigning a bunch of variables to a global singleton because:Session
andRequest
andAuth
This means we can start providing helpers like
session()
andauth()
, and use them from within Superflare internals as well 🎉We've been waiting on a patch to Remix which just landed in
dev
remix-run/remix#5773TODO: