Expo Router with API Routes is insane! However Cloudflare Workers is not supported by Expo team yet. So this adapter comes!
Your project is using expo-router
with API Routes.
In app.json
, you should have configuration like this:
{
"expo": {
"web": {
"bundler": "metro",
"output": "server"
}
}
}
npm install expo-adapter-workers wrangler
npx expo export -p web
worker.ts
import { createRequestHandler } from "expo-adapter-workers";
const requestHandler = createRequestHandler();
export default {
async fetch(request: Request, env: any, ctx: { waitUntil: any }) {
try {
return await requestHandler(request, env, ctx);
} catch (e) {
let pathname = new URL(request.url).pathname;
return new Response(`"${pathname}" not found`, {
status: 404,
statusText: "not found",
});
}
},
};
wrangler.toml
name = "example"
main = "worker.ts"
compatibility_date = "2024-03-08"
[site]
bucket = "./dist/client"
npx expo-adapter-workers // This is required!!!
npx wrangler deploy
MIT