Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created April 28, 2025 13:40
Show Gist options
  • Save mizchi/58e313f917f869ab92eabfe6c5133d38 to your computer and use it in GitHub Desktop.
Save mizchi/58e313f917f869ab92eabfe6c5133d38 to your computer and use it in GitHub Desktop.
/**
with wrangler.json
"assets": {
"binding": "assets",
"directory": "public",
"run_worker_first": true
},
*/
import tsBlankSpace from "ts-blank-space";
export default {
async fetch(req, env, ctx): Promise<Response> {
try {
const url = new URL(req.url);
if (url.pathname.endsWith(".ts") || url.pathname.endsWith(".tsx")) {
const originalReq = await env.assets.fetch(req);
const body = await originalReq.text();
const js = tsBlankSpace(body);
const headers = new Headers(originalReq.headers);
headers.set("Content-Type", "application/javascript; charset=utf-8");
return new Response(js, {
headers: headers,
status: originalReq.status,
});
}
return env.assets.fetch(req);
} catch (e) {
console.error(e);
return new Response("Internal Server Error", { status: 500 });
}
},
} satisfies ExportedHandler<Env>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment