Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] feat: Hono Action #3973

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

[POC] feat: Hono Action #3973

wants to merge 15 commits into from

Conversation

yusukebe
Copy link
Member

@yusukebe yusukebe commented Mar 4, 2025

WIP

This comment has been minimized.

This comment has been minimized.

Copy link

codecov bot commented Mar 4, 2025

Codecov Report

Attention: Patch coverage is 6.33484% with 207 lines in your changes missing coverage. Please review.

Project coverage is 89.58%. Comparing base (8796371) to head (f7e02d3).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
src/jsx/action/index.ts 0.00% 111 Missing ⚠️
src/jsx/action/client.ts 0.00% 93 Missing ⚠️
src/utils/url.ts 77.77% 2 Missing ⚠️
src/jsx/intrinsic-element/components.ts 75.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (6.33%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3973      +/-   ##
==========================================
- Coverage   91.29%   89.58%   -1.72%     
==========================================
  Files         168      170       +2     
  Lines       10772    10981     +209     
  Branches     3057     3148      +91     
==========================================
+ Hits         9834     9837       +3     
- Misses        937     1143     +206     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe
Copy link
Member Author

yusukebe commented Mar 4, 2025

Hey @usualoma !

Shall we work on Hono Action as just POC? I think Hono Action will be super helpful with using HonoX.

In this PR, it throws the following error. Can you take a look at it?

App:

import { Hono } from '../../src'
import { createAction } from '../../src/jsx/action'
import { jsxRenderer } from '../../src/middleware/jsx-renderer'

const renderer = jsxRenderer(
  ({ children }) => {
    return (
      <html>
        <body>
          <main>{children}</main>
        </body>
      </html>
    )
  },
  {
    stream: true,
  }
)

const app = new Hono()

app.use(renderer)

const [action, Component] = createAction(app, async (data, c) => {
  return <></>
})

app.get('/', (c) => {
  return c.render(
    <section>
      <form>
        <Component />
      </form>
    </section>
  )
})

export default app

Error:

CleanShot 2025-03-04 at 16 33 49@2x

@usualoma
Copy link
Member

usualoma commented Mar 5, 2025

I'll check; give me a few days.

@usualoma
Copy link
Member

Hi @yusukebe.

Sorry for the delay. I have not been able to reproduce the error you indicated.

It requires a bit adjustment to get it working, and I have confirmed that it works with the following PR content.
#4008

With the following source code and settings,

$ wrangler dev hono-action.tsx

Ran and worked.

/** @jsxRuntime automatic @jsxImportSource ./src/jsx */
import { Hono } from './src/'
import { createAction } from './src/jsx/action'
import { jsxRenderer } from './src/middleware/jsx-renderer'

const renderer = jsxRenderer(
  ({ children }) => {
    return (
      <html>
        <body>
          <main>{children}</main>
        </body>
      </html>
    )
  },
  {
    stream: true,
  }
)

const app = new Hono()

app.use(renderer)

const [action, Component] = createAction(app, async (data, c) => {
  return (
    <>
      Hello {data?.name}
      <input type='text' name='name' />
      <input type='submit' value='Submit' />
    </>
  )
})

app.get('/', (c) => {
  return c.render(
    <section>
      <form action={action}>
        <Component />
      </form>
    </section>
  )
})

export default app
{
    "$schema": "node_modules/wrangler/config-schema.json",
    "name": "test",
    "compatibility_date": "2025-03-17",
    "observability": {
        "enabled": true
    },
    "compatibility_flags": [
        "nodejs_compat_v2"
    ]
}
CleanShot.2025-03-18.at.19.58.33.mp4

@yusukebe
Copy link
Member Author

@usualoma Thank you for the response!

It requires a bit adjustment to get it working, and I have confirmed that it works with the following PR content.
#4008

I haven't investigated it, so this is just my opinion. Wrangler uses esbuild internally, and esbuild may compile the client's JS. Anyway, I think you know it, but the implementation may be not so good.

However, I want to merge #4008 with this branch. Can you remove the commit 36243d1 then I'll merge it.

@yusukebe
Copy link
Member Author

@usualoma

The reason it didn't work was that the JSX settings were wrong! Sorry!

@@ -372,8 +372,8 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
#addRoute(method: string, path: string, handler: H) {
method = method.toUpperCase()
path = mergePath(this._basePath, path)
const r: RouterRoute = { path, method, handler }
this.router.add(method, path, [handler, r])
const r: RouterRoute = { path: path, method: method, handler: handler }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const r: RouterRoute = { path: path, method: method, handler: handler }
const r: RouterRoute = { path, method, handler }

I think this section should be written in shorthand property.
(#3640)

@usualoma
Copy link
Member

Hi @yusukebe.

Thanks for checking!
I have deleted 36243d1 from #4008.

Copy link

Bundle size check

main (5473682) #3973 (fb5b488) +/-
Bundle Size (B) 18,976B 19,024B 48B
Bundle Size (KB) 18.53K 18.58K 0.05K

Compiler Diagnostics

main (5473682) #3973 (fb5b488) +/-
Files 261 261 0
Lines 116,441 116,453 12
Identifiers 114,439 114,465 26
Symbols 303,843 303,860 17
Types 214,833 214,840 7
Instantiations 3,091,594 3,091,594 0
Memory used 454,932K 455,171K 239K
I/O read 0.03s 0.02s -0.01s
I/O write 0s 0s 0s
Parse time 0.79s 0.75s -0.04s
Bind time 0.32s 0.29s -0.03s
Check time 5.49s 5.75s 0.26s
Emit time 0s 0s 0s
Total time 6.6s 6.8s 0.2s

Reported by octocov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants