Skip to content

useRequestContext not working when using await and the html helper together #4582

@2G14

Description

@2G14

What version of Hono are you using?

4.11.1

What runtime/platform is your app running on? (with version if possible)

Node.js 20.15.1, @hono/node-server 1.19.7

What steps can reproduce the bug?

Descendant components in a parent component using await and the html helper will not be able to use useRequestContext.

import { serve } from '@hono/node-server';
import { Hono } from 'hono';
import { html } from 'hono/html';
import { jsxRenderer, useRequestContext } from 'hono/jsx-renderer';

const app = new Hono();

app.use(jsxRenderer());

const Parent = async (props: { children?: any }) => {
  // An error occurs when using `await` and the `html` helper together.
  // => `Error: RequestContext is not provided.`
  await new Promise((r) => setTimeout(r, 1000));
  return html`<div>${props.children}</div>`;
};
// The following is a safe pattern.
// const Parent = async (props: { children?: any }) => {
//   return html`<div>${props.children}</div>`;
// };
// const Parent = async (props: { children?: any }) => {
//   return <div>{props.children}</div>;
// };
// const Parent = async (props: { children?: any }) => {
//   await new Promise((r) => setTimeout(r, 1000));
//   return <div>{props.children}</div>;
// };

const Child = () => {
  // I want to use the request context in a child component.
  const c = useRequestContext();
  return <div>hoge</div>;
};

app.get('/', async (c) => {
  return c.render(
    <Parent>
      <Child />
    </Parent>
  );
});

serve({
  fetch: app.fetch,
  port: 8080,
});

What is the expected behavior?

I expect useRequestContext to function correctly when using the await and html helpers in conjunction.

What do you see instead?

Error: RequestContext is not provided.
    at useRequestContext (file:///usr/src/app/node_modules/hono/dist/middleware/jsx-renderer/index.js:49:11)
    at Child (/usr/src/app/src/index.tsx:30:13)
    at JSXFunctionNode.toStringToBuffer (file:///usr/src/app/node_modules/hono/dist/jsx/base.js:178:26)
    at JSXFunctionNode.toString (file:///usr/src/app/node_modules/hono/dist/jsx/base.js:106:12)
    at html (file:///usr/src/app/node_modules/hono/dist/helper/html/index.js:21:29)
    at Parent (/usr/src/app/src/index.tsx:14:10)
    at async Promise.all (index 1)
    at async stringBufferToString (file:///usr/src/app/node_modules/hono/dist/utils/html.js:17:26)
    at async Promise.all (index 1)
    at async stringBufferToString (file:///usr/src/app/node_modules/hono/dist/utils/html.js:17:26)

Additional information

If this is the specification, I would appreciate any ideas on how to avoid it. Thank you.

I am not good at English, so I am creating this using a translation AI. I apologize if there are any inappropriate expressions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions