Created
April 28, 2025 13:40
-
-
Save mizchi/58e313f917f869ab92eabfe6c5133d38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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