hono icon indicating copy to clipboard operation
hono copied to clipboard

Middleware Support for fetch results

Open CaptainYarb opened this issue 3 years ago • 1 comments

Heya folks,

Currently in the process of swapping from itty-router and finding hono much easier to use. One hyper minor inconvenience is how fetch requests are (not) handled by middleware.

With itty, we use some simple code like:

if(url.pathname === '/favicon.ico'){
  return fetch('https://mainsite/favicon.ico');
}

which now has to look something like

if(url.pathname === '/favicon.ico'){
  const icon = await fetch('https://mainsite/favicon.ico');
  const blog = await results.blob();
  return context.text(blob);
}

This is a part of our middleware to handle assets where we just proxy the favicon to avoid asset duplication as a very basic example of this usage. Would this be a reasonable expectation to be able to return the fetch call?

Thanks ✌️

CaptainYarb avatar Jul 19 '22 16:07 CaptainYarb

Hi @CaptainYarb !

We can write it as just a handler:

app.all('/favicon.ico', async (c) => {
  const res = await fetch('https://honojs.dev/favicon.ico')
  const newResponse = new Response(res.body, res)
  return newResponse
})

It may work well!

yusukebe avatar Jul 19 '22 23:07 yusukebe