honojs / website

Repository for hono.dev
https://hono.dev
68 stars 217 forks source link

Update Context.md setRenderer documentation, to correctly reflect what is needed to not get a typeerror. #250

Closed PatNei closed 4 months ago

PatNei commented 4 months ago
app.use('/pages/*', async (c, next) => {
  c.setRenderer((content, head) => {
    return c.html(
      <html lang='en'>
        <head>
          <title>{head.title}</title>
        </head>
        <body>
          <header>{head.title}</header>
          <p>{content}</p>
        </body>
      </html>
    )
  })
  await next()
})

Will throw a compile error if you don't have the ContextRenderer return a Response or Promise, like so

declare module "hono" {
    interface ContextRenderer {
        (content: string | Promise<string>, head: { title: string }): Response | Promise<Response>;
    }
}
yusukebe commented 4 months ago

Hi @PatNei

Exactly, we should add Promise<Response>! Thanks.