Cloudflare DevelopersCD
Cloudflare Developers10mo ago
1 reply
Lami

Error 1102: Worker exceeded resource limits

I have a question regarding CPU time on Cloudflare Workers.

I'm deploying a web application on Cloudflare Workers using OpenNext + Next.js. To bypass CORS issues, I added a route handler that proxies the requests. However, even though the free plan limits CPU time to 10ms and I'm only performing a fetch and returning the response, it still consumes between 60 to 120ms of CPU time. Why is that?

app/proxy/route.ts
import { NextRequest, NextResponse } from 'next/server';

// GETメソッド:クエリパラメータの「url」に向けてリクエストを転送し、レスポンスにCORSヘッダーを追加して返す
export async function GET(request: NextRequest) {
  // リクエストURLからクエリパラメータを抽出
  const { searchParams } = new URL(request.url);
  const targetUrl = searchParams.get('url');

  // ターゲットのURLが指定されていなければエラーレスポンスを返す
  if (!targetUrl) {
    return new NextResponse(null, { status: 400 });
  }

  const response = await fetch(targetUrl);
  response.headers.set('Access-Control-Allow-Origin', '*');
  response.headers.set('Access-Control-Allow-Credentials', 'true');
  return response;
}

// OPTIONSメソッド:プリフライトリクエストへの対応(必要なCORSヘッダーを返す)
export function OPTIONS() {
  return new NextResponse(null, {
    status: 200,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, OPTIONS',
      'Access-Control-Allow-Headers': '*',
    },
  });
}
image.png
Was this page helpful?