-
Notifications
You must be signed in to change notification settings - Fork 127
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
[🐛 Bug]: "Failed to retrieve the Cloudflare request context" in app/api route handlers #683
Comments
This was fixed by moving the getRequestContext() call to inside the function:
|
Hi @thipperz, thanks for the issue 🙂 Sorry I haven't been able to reply to this sooner 🙇 I see that you've closed the issue and I'm glad you've found a workaround, but anyways your original code should work... (PS: I'll try to investigate this soon but I don't know when I'll be able to 😓) |
Minimal reproduction for this issue: https://github.com/dario-piotrowicz/next-on-Pages-top-level-getRequestContext-bug-repro Note
I don't get the above... as this bug/issue does seem to effect standard routes too (as you can see from my minimal reproduction) ¯\(ツ)/¯ |
Hi @dario-piotrowicz, I couldn't reproduce the issue again, perhaps I was mistaken as it was working with |
I'm experiencing the same issue and I would rather not call |
@thipperz thanks for the clarification 🙂 🙏 PS: I experimented with this and it seems like we'll have to go with your solution after all 😅 |
@treboryx the fact is that Also please keep in mind that |
Oh yeah I get that it's just that my code was nicely organized and now I have to use it everywhere. HOWEVER, I found a solution that seems to work import { getRequestContext } from "@cloudflare/next-on-pages";
const { env } = process.env.NODE_ENV === "development" ? getRequestContext() : process;
export const DB = env.DB;
export const STORAGE = env.STORAGE;
export const KV = env.KV; I have to use |
@treboryx yeah.... that should indeed work 👍 Ideally I'd love it if people could not use Alternatively you could just use So, could you maybe try: import { getOptionalRequestContext } from "@cloudflare/next-on-pages";
const { env } = getOptionalRequestContext() ?? { env: {} }; that should pretty much be the exact same as your solution |
in 2024, i still found this problem |
next-on-pages environment related information
System:
Platform: linux
Arch: x64
Version: #1 SMP Thu Oct 5 21:02:42 UTC 2023
CPU: (4) x64 AMD Ryzen 3 3200G with Radeon Vega Graphics
Memory: 8 GB
Shell: /bin/bash
Package Manager Used: pnpm (8.15.2)
Relevant Packages:
@cloudflare/next-on-pages: 1.9.0
vercel: N/A
next: 14.1.0
Description
Hi there,
I'm building a web app with Next-on-Pages.
I have a lib/db/data.ts file that exports a few functions that retrieve something from a D1 Database. This file looks like this:
Everything works fine when I import the getUser function in some route like app/user/page.tsx
However, if I were to import and use this function in the exact same way inside app/api/getUser/route.ts, the build will fail with a message like
All files (the api route handler, the page.tsx, and data.ts) have a
export const runtime = 'edge'
at the start.Am I missing something?
Reproduction
No response
Pages Deployment Method
None
Pages Deployment ID
No response
Additional Information
No response
Would you like to help?
The text was updated successfully, but these errors were encountered: