Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Feature] can getCloudflareContext({async: false}) work at top level with next dev #382

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

Closed
vicb opened this issue Feb 15, 2025 · 1 comment Β· Fixed by #404
Closed

[Feature] can getCloudflareContext({async: false}) work at top level with next dev #382

vicb opened this issue Feb 15, 2025 · 1 comment Β· Fixed by #404
Assignees

Comments

@vicb
Copy link
Contributor

vicb commented Feb 15, 2025

If you update examples/playground/api/env as follow:

import { getCloudflareContext } from "@opennextjs/cloudflare";

// the output.
export const dynamic = "force-dynamic";

console.log(getCloudflareContext());

export async function GET() {
  return new Response(JSON.stringify(process.env));
}

Note: calling getCloudflareContext is not actually supported at top level but I'm working on adding support for that.

It would log an incorrect message (the route is dynamic):

ERROR: `getCloudflareContext` has been called in a static route, that is not allowed, this can be solved in different ways:

 - call `getCloudflareContext({async: true})` to use the `async` mode
 - avoid calling `getCloudflareContext` in the route
 - make the route non static
  • This is not a static route
  • getCloudflareContext({async: true}) seem to work

We should first fix the error message.

It would also be nice that we could make {async: false} work here if possible

@dario-piotrowicz
Copy link
Contributor

I've just investigate this and it's my bad for the wrong error message, sorry about that πŸ˜“

Basically the {async: false} version of the function cannot work at the top level regardless on whether the route is dynamic or not, so I do need to update the error message to reflect that

The problem is that Next.js seems to always import and so a SSG-like processing even if you force the route to be dynamic, there is a long standing issue/discussion in their repo about this: vercel/next.js#35534

To clarify, see this hello api route I created in a brand new create-next-app application:
Image

As you can see from the log the top level logic is run even if the route is fully dynamic, and I can't seem to find a way to prevent Next.js from doing that πŸ˜“


PS: what's worse if that Next.js does this in the same way it does standard SSG, so we can't discern between the two cases and provide different error messages in case the route is actually SSG'd or not πŸ˜“

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

Successfully merging a pull request may close this issue.

2 participants