-
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
introduce new initOpenNextCloudflareForDev
utility and make getCloudflareContext
synchronous
#265
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: cf9b45e 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
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. |
09b33de
to
59419ca
Compare
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 |
@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 ( |
1c298b1
to
b234e3e
Compare
getCloudflareContext
to work in middlewares via a new enableEdgeDevGetCloudflareContext
utilityinitOpenNextCloudflareForDev
utility and make getCloudflareContext
synchronous
…udflareContext` synchronous
b234e3e
to
d7f6572
Compare
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.
Cant't wait for this PR to get in 🎉
// should ideally also be part of the dev cloudflare context | ||
// (this is an upstream wrangler issue: https://github.com/cloudflare/workers-sdk/issues/7812) | ||
expect(await cloudflareContextHeaderElement.textContent()).toContain( | ||
"keys of `cloudflareContext.env`: MY_VAR, MY_KV" |
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.
should this be an API route returning JSON and testing array.toContain(...)
so that order does not matter?
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.
the dependence on the ordering can also easily be fixed by sorting the keys: 6006cf2
(I also had to filter the variables to avoid the ASSETS issue)
I am happy to also add (or instead) an API route and do array.toContain
whatever you prefer, I'm happy with anything 🙂
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.
I think API + array.toContain
would be simpler but the code is already here and working so up to you.
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.
I just simplified the test there: 6e6d076
All we care about here is checking that we can get access to the cloudflare context in the middleware, regarding the context variables we already test that they are present here:
opennextjs-cloudflare/examples/api/e2e/base.spec.ts
Lines 18 to 26 in a18f826
test("returns a hello world string from the cloudflare context env", async ({ page }) => { | |
const res = await page.request.get("/api/hello", { | |
headers: { | |
"from-cloudflare-context": "true", | |
}, | |
}); | |
expect(res.headers()["content-type"]).toContain("text/plain"); | |
expect(await res.text()).toEqual("Hello World from the cloudflare context!"); | |
}); |
So I think that testing the existence of the env
object is enough here (and if we wanted to have a comprehensive e2e test for getCloudflareContext
with all the various variables, cf
, etc... that should probably live in the api example), what do you think?
Co-authored-by: Victor Berchet <[email protected]>
ea44365
to
6006cf2
Compare
.changeset/chilly-dryers-begin.md
Outdated
|
||
introduce new `initOpenNextCloudflareForDev` utility and make `getCloudflareContext` synchronous | ||
|
||
introduce a new `initOpenNextCloudflareForDev` function that when called in the [Next.js config file](https://nextjs.org/docs/app/api-reference/config/next-config-js) integrates the Next.js dev server with the open-next Cloudflare adapter. Most noticeably this enables `getCloudflareContext` to work in |
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.
suggestion: maybe start simple with the TL;DR and add details (or not) in following paragraph.
something like
`initOpenNextCloudflareForDev()` must be called from the [Next.js config file](https://nextjs.org/docs/app/api-reference/config/next-config-js) so that your bindings can be accessed using `getCloudflareContext()` when running the Next.js development server (`next dev`).
`getCloudflareContext` is now sync.
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.
the changeset title is already a good TL;DR, isn't it?
I think I don't fully understand what you're suggesting, if you want feel free to suggest a code change or to push on the branch
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.
I think the change set should first highlight what is most important for the users:
- that the init function must be called from the config
- that getCloudflareContext is sync
IMO mentioning the edge runtime of our recommendation could be pushed down or moved to the doc site.
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.
I've updated the changeset I hope the new version works for you
I've pushed the |
Could you please update the README and opennext.js.org to tell users to add the init? |
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.
Could you please update https://github.com/opennextjs/opennextjs-cloudflare/blob/main/packages/cloudflare/README.md
Either to ask the user to add the call to the init function
Or if you can update opennext.js.org, remove the content and link to the doc site (I was mostly using the content for the experimental branch when the experimental features were not documented anywhere else)
Great stuff, thanks 🚀
|
||
this change introduces a new `initOpenNextCloudflareForDev` function that must called in the [Next.js config file](https://nextjs.org/docs/app/api-reference/config/next-config-js) to integrate the Next.js dev server with the open-next Cloudflare adapter. | ||
|
||
Also makes `getCloudflareContext` synchronous, so `await`ing such function is no longer 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.
maybe:
Also makes `getCloudflareContext` synchronous, so `await`ing such function is no longer necessary. | |
Also makes `getCloudflareContext` synchronous. |
* (Currently all the setup that this function performs is making sure that `getCloudflareContext` works as intended, | ||
* in the future mode improvements might be added) | ||
* |
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.
maybe:
* (Currently all the setup that this function performs is making sure that `getCloudflareContext` works as intended, | |
* in the future mode improvements might be added) | |
* |
|
||
const cloudflareContext = global[cloudflareContextSymbol]; | ||
|
||
if (!cloudflareContext) { |
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.
Note: we should make sure not to print this when using wrangler - that's #229 that we should tackle in a follow up PR.
@@ -0,0 +1,28 @@ | |||
--- | |||
"@opennextjs/cloudflare": patch |
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 cutting a minor?
It would signal users that there are changes:
- calling the
initOpenNextCloudflareForDev
function, getCloudflareContext
becoming sync
If you do that, please also prepare:
- a PR to update the c3 template to 0.4.x
- a PR to the doc site (copy the current content to 0.3, add the changes in the current content, add a migration from 0.3)
This PR introduces a new
initOpenNextCloudflareForDev
utility to add to the Next.js config file that makes thegetCloudflareContext
work in middlewares (or more generally, the edge runtime) during local development (vianext dev
)It
getCloudflareContext
has also been converted to be synchronousFor more details see the changeset file:
.changeset/chilly-dryers-begin.md
fixes #137
fixes #226