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

COMPOSED_HANDLER mismatch when using different version of hono #3760

Closed
yusukebe opened this issue Dec 20, 2024 · 1 comment · Fixed by #3773
Closed

COMPOSED_HANDLER mismatch when using different version of hono #3760

yusukebe opened this issue Dec 20, 2024 · 1 comment · Fixed by #3773
Labels
bug enhancement New feature or request.

Comments

@yusukebe
Copy link
Member

yusukebe commented Dec 20, 2024

What version of Hono are you using?

4.6.14

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

any

What steps can reproduce the bug?

Sorry to reproduce and explain this problem is super difficult.

For example, this problem occurs when using the SSG helper. Suppose there are two projects in a monorepo: one called app, which defines the application, and one called ssg, which is used for SSG. If they are both using the same version of hono, there will be no problem. However, if they are using different versions, for example, app is using 4.6.13 and ssg is using 4.6.14, the SSG helper will not generate the files.

Repro: https://github.com/yusukebe/composed-handler-mismatch

CleanShot 2024-12-20 at 20 01 11@2x

This happens when onError is defined in subApp within the app application.

// projects/app/src/index.ts
import { Hono } from 'hono'

const subApp = new Hono()

subApp.get('/', (c) => c.html('Hi'))
subApp.onError((_e, c) => c.html('Error', 500))

const app = new Hono()
app.route('/', subApp)

export default app

This is because the handler to handle GET / request is marked as middleware not as a handler in the following code:

const targetHandler = findTargetHandler(handler)
if (['GET', METHOD_NAME_ALL].includes(method) && !isMiddleware(targetHandler)) {
acc.push({ path })
}

The reason is that the Symbol COMPOSED_HANDLER is recognized as a different key if the version is different in the following locations.

export const findTargetHandler = (handler: Function): Function => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (handler as any)[COMPOSED_HANDLER]
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
findTargetHandler((handler as any)[COMPOSED_HANDLER])
: handler
}

COMPOSED_HANDLER is defined in hono-base.ts.

export const COMPOSED_HANDLER = Symbol('composedHandler')

If you change the value of this COMPOSED_HANDLER to a string, this problem will be solved.

export const COMPOSED_HANDLER = '__COMPOSED_HANDLER'

Should it be a Symbol?

How about using a string like __COMPOSED_HANDLER instead of the Symbol? I think it is not a problem.

Is this a rare case?

Yes, it's a rare case. But this causes the issues: honojs/honox#234 honojs/honox#236 in HonoX. The issue is not caused by a difference in the version of hono, but it has deep nested dependencies. That will be resolved if making it string.

@yusukebe yusukebe added triage enhancement New feature or request. bug and removed triage labels Dec 20, 2024
@yusukebe
Copy link
Member Author

Hi @usualoma. What do you think of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug enhancement New feature or request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant