File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import module from "node:module" ;
2+
3+ export const dynamic = "force-dynamic" ;
4+
5+ export async function GET ( ) {
6+ if ( globalThis . Cloudflare ) {
7+ // Importing `cloudflare:*` does not work in dev (using `next dev`) because the Node runtime is used there.
8+ // Then you should avoid it to write portable code.
9+ // To access `env` and bindings, using `getCloudflareContext()` is the preferred way.
10+ // See https://opennext.js.org/cloudflare/bindings
11+ const req = module . createRequire ( "file:///" ) ;
12+ const { env } = req ( "cloudflare:workers" ) ;
13+ return Response . json ( { cloudflare : true , env } ) ;
14+ }
15+
16+ return Response . json ( { cloudflare : false } ) ;
17+ }
Original file line number Diff line number Diff line change @@ -14,3 +14,14 @@ test.describe("playground/cloudflare", () => {
1414 expect ( nextConfig . output ) . toEqual ( "standalone" ) ;
1515 } ) ;
1616} ) ;
17+
18+ test . describe ( "using cloudflare:* modules" , ( ) => {
19+ test ( "NextConfig" , async ( { page } ) => {
20+ const res = await page . request . get ( "/api/cloudflare" ) ;
21+ expect ( res . status ( ) ) . toEqual ( 200 ) ;
22+ const { cloudflare, env } = await res . json ( ) ;
23+ expect ( cloudflare ) . toBe ( true ) ;
24+ expect ( env . NEXTJS_ENV ) . toEqual ( "development" ) ;
25+ expect ( env . ASSETS ) . toBeDefined ( ) ;
26+ } ) ;
27+ } ) ;
You can’t perform that action at this time.
0 commit comments