-
Notifications
You must be signed in to change notification settings - Fork 21
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
enable getCloudflareContext
to work in middlewares via a new enableEdgeDevGetCloudflareContext
utility
#265
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 1c298b1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
2447a8a
to
f6510ac
Compare
commit: |
ddaa077
to
4753b0a
Compare
6648525
to
09b33de
Compare
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {}; | ||
|
||
enableEdgeDevGetCloudflareContext(); |
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.
What about renaming this function to initOpenNextCloudflare()
I think it's easier for the user to understand as we expose lesst techincal details and we could add to this function later if needed.
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.
mh... my idea was that people would not have to always include this but only when they face the cloudflare context in the edge runtime issue (a problem that can happen to a subset of users and one that hopefully can go away in the future if Next.js decides to support nodejs middlewares), that's why I made the name more specific
initOpenNextCloudflare()
sounds like something they always need to have (similar to setupDevPlatform in next-on-pages)
we could add to this function later if needed.
That's true, but again your comment makes it sound to me like moving forward we'd want to recommend all users to include this, which I am not sure about 😕
So based on the above I think I do prefer something like enableEdgeDevGetCloudflareContext
which makes it very clear to the user what this function does and when/why is needed besides making clear(er) that this is optional
But if you strongly prefer a generic name like initOpenNextCloudflare()
I'd be ok with using that (although that's not my preference)
Thought: Maybe we can This comment says "the function is an async one but it doesn't need to be awaited", it might be nice to add the rationale if we add the implementation to our repo. |
…eEdgeDevGetCloudflareContext` utility
09b33de
to
59419ca
Compare
… `enableEdgeDevGetCloudflareContext` utility use `process.env.NEXT_RUNTIME` to detect the edge runtime instead of try-catching
Yes, we could, this would mean that everyone would always have to call but again if you strongly prefer us to go that route I'm ok with it
Yes, given the implementation of |
… `enableEdgeDevGetCloudflareContext` utility clarify args of `runInContext`
… `enableEdgeDevGetCloudflareContext` utility create new `monkeyPatchVmModuleEdgeContext` commented function
… `enableEdgeDevGetCloudflareContext` utility amend `monkeyPatchVmModuleEdgeContext`
… `enableEdgeDevGetCloudflareContext` utility add comment to `getCloudflareContext`
@vicb I've addressed all the feedback, the only thing left is to decide whether we want to force this sort of thing via a up to you, just let me know if you want to me make the change 👍 |
Give me some time to think about it, I'm not settled. Some thoughts I have for now about Cons:
Pros
|
Well there is only one con and many pros, I dislike that we need to then ask users to always call the function, but given the benefits it does seem like it might be worth it By the way, either way I am not too convinced about the name since this is a dev ( |
This PR addresses the fact that
getCloudflareContext
can't work in dev (next dev
) inside middlewares.The issue is that those are always run in the edge runtime, although in production this is not a problem, in development (
next dev
) this prevents us from being able to import and use the wrangler node.js APIs from there.I've tried to come up with some nice solutions but unfortunately I think the best we can do is basically borrow the
setupDevPlatform
next-on-pages idea and require users to call a function in their Next.js config file (this function then patches thevm.runInContext
function to make the cloudflare context available in the "dev edge runtime").So my solution here consist in having everything still working exactly the same for standard nodejs routes (if you don't call
getCloudflareContext
from a middleware you don't need to worry about anything), but for edge runtime routes/middlewares a utility calledenableEdgeDevGetCloudflareContext
needs to be invoked in the next config file.We can properly document this, not invoking the function while invoking
getCloudflareContext
in a middleware also provides a helpful error that should hopefully make this very easy to follow:Note
The Next.js docs say that middelwares currently run in the edge runtime, so maybe this can change in the future making this use case rarer
fixes #226