Skip to content

Commit

Permalink
fix: fix isolate prewarm logic for wrangler dev (#908)
Browse files Browse the repository at this point in the history
When calling `wrangler dev`, we make a request to a special URL that "prewarms" the isolate running our Worker so that we can attach devtools etc to it before actually making a request. We'd implemented it wrongly, and because we'd silenced its errors, we weren't catching it. This patch fixes the logic (based on wrangler 1.x's implementation) and enables logging errors when the prewarm request fails.

As a result, profiling starts working again as expected. Fixes #907
  • Loading branch information
threepointone authored May 5, 2022
1 parent f2b6cd9 commit f8dd31e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .changeset/eighty-islands-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

fix: fix isolate prewarm logic for `wrangler dev`

When calling `wrangler dev`, we make a request to a special URL that "prewarms" the isolate running our Worker so that we can attach devtools etc to it before actually making a request. We'd implemented it wrongly, and because we'd silenced its errors, we weren't catching it. This patch fixes the logic (based on wrangler 1.x's implementation) and enables logging errors when the prewarm request fails.

As a result, profiling starts working again as expected. Fixes https://github.com/cloudflare/wrangler2/issues/907
14 changes: 8 additions & 6 deletions packages/wrangler/src/create-worker-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { URL } from "node:url";
import { fetch } from "undici";
import { fetchResult } from "./cfetch";
import { createWorkerUploadForm } from "./create-worker-upload-form";
import { logger } from "./logger";
import type { CfAccount, CfWorkerContext, CfWorkerInit } from "./worker";

/**
Expand Down Expand Up @@ -66,19 +67,17 @@ async function sessionToken(
: `/accounts/${accountId}/workers/subdomain/edge-preview`;

const { exchange_url } = await fetchResult<{ exchange_url: string }>(initUrl);
const { inspector_websocket, token } = (await (
const { inspector_websocket, prewarm, token } = (await (
await fetch(exchange_url, { signal: abortSignal })
).json()) as { inspector_websocket: string; token: string };
).json()) as { inspector_websocket: string; token: string; prewarm: string };
const { host } = new URL(inspector_websocket);
const query = `cf_workers_preview_token=${token}`;

return {
value: token,
host,
inspectorUrl: new URL(`${inspector_websocket}?${query}`),
prewarmUrl: new URL(
`https://${host}/cdn-cgi/workers/preview/prewarm?${query}`
),
prewarmUrl: new URL(prewarm),
};
}

Expand Down Expand Up @@ -165,9 +164,12 @@ export async function createWorkerPreview(
const response = await fetch(token.prewarmUrl.href, {
method: "POST",
signal: abortSignal,
headers: {
"cf-workers-preview-token": token.value,
},
});
if (!response.ok) {
// console.error("worker failed to prewarm: ", response.statusText);
logger.warn("worker failed to prewarm: ", response.statusText);
}
return token;
}

0 comments on commit f8dd31e

Please sign in to comment.