Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix isolate prewarm logic for wrangler dev #908

Merged
merged 1 commit into from
May 5, 2022
Merged
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
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;
}