Skip to content

Commit

Permalink
fix: abort some of the fetches when we unmount the Remote component
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Mar 30, 2022
1 parent ea8e701 commit 1ca3092
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
21 changes: 14 additions & 7 deletions packages/wrangler/src/create-worker-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export interface CfPreviewToken {
*/
async function sessionToken(
account: CfAccount,
ctx: CfWorkerContext
ctx: CfWorkerContext,
abort: AbortSignal
): Promise<CfPreviewToken> {
const { accountId } = account;
const initUrl = ctx.zone
Expand All @@ -66,7 +67,7 @@ async function sessionToken(

const { exchange_url } = await fetchResult<{ exchange_url: string }>(initUrl);
const { inspector_websocket, token } = (await (
await fetch(exchange_url)
await fetch(exchange_url, { signal: abort })
).json()) as { inspector_websocket: string; token: string };
const { host } = new URL(inspector_websocket);
const query = `cf_workers_preview_token=${token}`;
Expand Down Expand Up @@ -96,11 +97,13 @@ function randomId(): string {
async function createPreviewToken(
account: CfAccount,
worker: CfWorkerInit,
ctx: CfWorkerContext
ctx: CfWorkerContext,
abort: AbortSignal
): Promise<CfPreviewToken> {
const { value, host, inspectorUrl, prewarmUrl } = await sessionToken(
account,
ctx
ctx,
abort
);

const { accountId } = account;
Expand Down Expand Up @@ -155,10 +158,14 @@ async function createPreviewToken(
export async function createWorkerPreview(
init: CfWorkerInit,
account: CfAccount,
ctx: CfWorkerContext
ctx: CfWorkerContext,
abort: AbortSignal
): Promise<CfPreviewToken> {
const token = await createPreviewToken(account, init, ctx);
const response = await fetch(token.prewarmUrl.href, { method: "POST" });
const token = await createPreviewToken(account, init, ctx, abort);
const response = await fetch(token.prewarmUrl.href, {
method: "POST",
signal: abort,
});
if (!response.ok) {
// console.error("worker failed to prewarm: ", response.statusText);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/dev/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ function useHotkeys(
) {
// UGH, we should put port in context instead
const [toggles, setToggles] = useState(initial);
const { exit } = useApp();
useInput(
async (
input,
Expand Down Expand Up @@ -365,7 +366,7 @@ function useHotkeys(
// shut down
case "q":
case "x":
process.exit(0);
exit();
break;
default:
// nothing?
Expand Down
16 changes: 12 additions & 4 deletions packages/wrangler/src/dev/remote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function useWorker(props: {
const startedRef = useRef(false);

useEffect(() => {
const abortCtrl = new AbortController();
async function start() {
setToken(undefined); // reset token in case we're re-running

Expand Down Expand Up @@ -173,15 +174,22 @@ export function useWorker(props: {
accountId,
apiToken,
},
{ env: props.env, legacyEnv: props.legacyEnv, zone: props.zone }
{ env: props.env, legacyEnv: props.legacyEnv, zone: props.zone },
abortCtrl.signal
)
);
}
start().catch((err) => {
// we want to log the error, but not end the process
// since it could recover after the developer fixes whatever's wrong
console.error("remote worker:", err);
if ((err as { code: string }).code !== "ABORT_ERR") {
// we want to log the error, but not end the process
// since it could recover after the developer fixes whatever's wrong
console.error("remote worker:", err);
}
});

return () => {
abortCtrl.abort();
};
}, [
name,
bundle,
Expand Down

0 comments on commit 1ca3092

Please sign in to comment.