How configure workers with assets for intercept public/index.html

i have wrangler.toml:
name = "my-worker-assets"
main = "src/index.ts"
compatibility_date = "2024-09-25"
compatibility_flags = ["nodejs_compat"]
assets = { directory = "./public", binding = "ASSETS" }
name = "my-worker-assets"
main = "src/index.ts"
compatibility_date = "2024-09-25"
compatibility_flags = ["nodejs_compat"]
assets = { directory = "./public", binding = "ASSETS" }
I need to add middleware to request.url == "/" but static files don't get into the default handler
export default {
async fetch(request, env, ctx): Promise<Response> {
const url = new URL(request.url);
console.log(url);
switch (url.pathname) {
case '/message':
return new Response('Hello, World!');
case '/random':
return new Response(crypto.randomUUID());
default:
return new Response('Not Found', { status: 404 });
}
},
} satisfies ExportedHandler<Env>;
export default {
async fetch(request, env, ctx): Promise<Response> {
const url = new URL(request.url);
console.log(url);
switch (url.pathname) {
case '/message':
return new Response('Hello, World!');
case '/random':
return new Response(crypto.randomUUID());
default:
return new Response('Not Found', { status: 404 });
}
},
} satisfies ExportedHandler<Env>;
3 Replies
Deo Kumar
Deo Kumar3w ago
I am not sure, assets workers do not support middleware yet, if asset to corresponding pathname is found, asset will be served and workers will not be invoked for that pathname.
Deo Kumar
Deo Kumar3w ago
Cloudflare Docs
Routing | Cloudflare Workers docs
How Workers assets' routing and its configuration works.
texan
texan3w ago
Hey @TLENS -- you have your binding set up, so you're halfway there! You can certainly run your middleware in your user worker, and then use the ASSETS binding to fetch your assets.
Want results from more Discord servers?
Add your server