Skip to content

Commit

Permalink
Use correct host in exchange (#2651)
Browse files Browse the repository at this point in the history
* Use subdomain
  • Loading branch information
penalosa committed Feb 17, 2023
1 parent de0cb57 commit a9adfbe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-grapes-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

Previously, wrangler dev would not work if the root of your zone wasn't behind Cloudflare. This behaviour has changed so that now only the route which your Worker is exposed on has to be behind Cloudflare.
27 changes: 19 additions & 8 deletions packages/wrangler/src/create-worker-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ function randomId(): string {
});
}

// URLs are often relative to the zone. Sometimes the base zone
// will be grey-clouded, and so the host must be swapped out for
// the worker route host, which is more likely to be orange-clouded
function switchHost(originalUrl: string, host?: string): URL {
const url = new URL(originalUrl);
url.hostname = host ?? url.hostname;
return url;
}
/**
* Generates a preview session token.
*/
Expand All @@ -129,9 +137,13 @@ export async function createPreviewSession(
abortSignal
);

logger.debug(`-- START EXCHANGE API REQUEST: GET ${exchange_url}`);
const switchedExchangeUrl = switchHost(exchange_url, ctx.host).toString();

logger.debug(`-- START EXCHANGE API REQUEST: GET ${switchedExchangeUrl}`);
logger.debug("-- END EXCHANGE API REQUEST");
const exchangeResponse = await fetch(exchange_url, { signal: abortSignal });
const exchangeResponse = await fetch(switchedExchangeUrl, {
signal: abortSignal,
});
const bodyText = await exchangeResponse.text();
logger.debug(
"-- START EXCHANGE API RESPONSE:",
Expand All @@ -147,16 +159,15 @@ export async function createPreviewSession(
token: string;
prewarm: string;
}>(bodyText);

const { host } = new URL(inspector_websocket);
const query = `cf_workers_preview_token=${token}`;
const inspector = new URL(inspector_websocket);
inspector.searchParams.append("cf_workers_preview_token", token);

return {
id: randomId(),
value: token,
host,
inspectorUrl: new URL(`${inspector_websocket}?${query}`),
prewarmUrl: new URL(prewarm),
host: ctx.host ?? inspector.host,
inspectorUrl: switchHost(inspector.href, ctx.host),
prewarmUrl: switchHost(prewarm, ctx.host),
};
}

Expand Down

0 comments on commit a9adfbe

Please sign in to comment.