diff --git a/app/api/recon/route.ts b/app/api/recon/route.ts new file mode 100644 index 0000000..d5e0e39 --- /dev/null +++ b/app/api/recon/route.ts @@ -0,0 +1,27 @@ +import { NextResponse, type NextRequest } from 'next/server'; +import { getRequestContext } from '@cloudflare/next-on-pages'; + +export const runtime = 'edge'; + +function callReconApi(request: NextRequest) { + if (process.env.NODE_ENV === 'development') { + /** + * The worker must run in remote mode for browser rendering to work correctly. + * However, the page in local mode cannot access service bindings running in remote mode. + * We just assume the service is running locally and use the HTTP local endpoint. + * + * @ref Browser rendering local mode https://developers.cloudflare.com/browser-rendering/platform/wrangler/#bindings + * @ref Local service binding and --remote https://github.com/cloudflare/workers-sdk/issues/5578 + * @ref https://github.com/cloudflare/workers-sdk/issues/1182 + */ + return fetch(`http://localhost:8787/${new URL(request.url).search}`); + } + + return getRequestContext().env.WORKER.fetch(request.url); +} + +export async function GET(request: NextRequest) { + const resp = await callReconApi(request); + + return new NextResponse(resp.body, resp); +} diff --git a/env.d.ts b/env.d.ts index 5e45e29..9ae54fa 100644 --- a/env.d.ts +++ b/env.d.ts @@ -1,6 +1,7 @@ -// Generated by Wrangler on Fri Jul 19 2024 12:52:14 GMT+0800 (台北標準時間) +// Generated by Wrangler on Sun Sep 22 2024 21:50:32 GMT+0800 (Taipei Standard Time) // by running `wrangler types --env-interface CloudflareEnv env.d.ts` interface CloudflareEnv { DB: D1Database; + WORKER: Fetcher; } diff --git a/wrangler.toml b/wrangler.toml index 3ce4f92..18cd47f 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -61,9 +61,9 @@ migrations_dir="db/migrations" # Bind another Worker service. Use this binding to call another Worker without network overhead. # Docs: https://developers.cloudflare.com/pages/functions/bindings/#service-bindings -# [[services]] -# binding = "MY_SERVICE" -# service = "my-service" +[[services]] +binding = "WORKER" +service = "open165-worker" # To use different bindings for preview and production environments, follow the examples below. # When using environment-specific overrides for bindings, ALL bindings must be specified on a per-environment basis.