Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/api/recon/route.ts
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 2 additions & 1 deletion env.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down