Skip to content

Commit

Permalink
Forward cdn-cgi/scripts paths to Cloudflare (#422)
Browse files Browse the repository at this point in the history
* Forward cdn-cgi/scripts paths to Cloudflare

Right now, Miniflare 404s on attempts to download `rocket-loader.min.js`. For folks who've set up a Worker in front of Cloudflare Pages (e.g. to handle API requests on the same domain), this breaks their web app during local development: it renders as a white screen.

This PR adds a small amount of logic to simply forward those requests onto Cloudflare.

* Update packages/http-server/src/index.ts

Co-authored-by: MrBBot <me@mrbbot.dev>

* Update index.ts

* add missing paren

Co-authored-by: MrBBot <me@mrbbot.dev>

* remove unneeded import

Co-authored-by: MrBBot <me@mrbbot.dev>

* Update index.ts

Co-authored-by: MrBBot <me@mrbbot.dev>
  • Loading branch information
jstevans and mrbbot authored Dec 22, 2022
1 parent 9040162 commit b24707a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/http-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Response,
_getBodyLength,
_headersFromIncomingRequest,
fetch,
logResponse,
} from "@miniflare/core";
import { Log, prefixError, randomHex } from "@miniflare/shared";
Expand Down Expand Up @@ -288,11 +289,19 @@ export function createRequestListener<Plugins extends HTTPPluginSignatures>(
url
);
status = 200;
res?.writeHead(status, { "Content-Type": "text/plain; charset=UTF-8" });
res?.end();
} else if (pathname.startsWith("/cdn-cgi/scripts/")) {
response = await fetch(new URL(pathname, "https://cloudflare.com"));
status = response.status;
if (res) {
await writeResponse(response, res, HTTPPlugin.liveReload, mf.log);
}
} else {
status = 404;
res?.writeHead(status, { "Content-Type": "text/plain; charset=UTF-8" });
res?.end();
}
res?.writeHead(status, { "Content-Type": "text/plain; charset=UTF-8" });
res?.end();
} else {
try {
response = await mf.dispatchFetch(request);
Expand Down

0 comments on commit b24707a

Please sign in to comment.