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

fix(dev-server): Fix issue in dev-server where requestListener would return null #212

Merged
merged 3 commits into from
Jan 16, 2025

Conversation

gobengo
Copy link
Contributor

@gobengo gobengo commented Jan 15, 2025

I was running into an error using honox, this vite dev server plugin, and hono node-server.

The error stack in prod is like

file:///app/node_modules/@hono/node-server/dist/index.mjs:345
   if (cacheKey in res) {
                ^
 
 TypeError: Cannot use 'in' operator to search for 'Symbol(cache)' in null
     at responseViaResponseObject (file:///app/node_modules/@hono/node-server/dist/index.mjs:345:16)
     at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
 
 Node.js v23.3.0

I believe that comes from the cacheKey in res here /~https://github.com/honojs/node-server/blob/590fe6cef8a795ea10c300033387f13ed31e144f/src/listener.ts#L86

But that is downstream from calling requestListener to get a Promise.

Before this change, if the response from the router has reuest.body === null (e.g. a Response with status 404 and no response body), then the dev-server will return null here, not a response, and downstream things that expect a response (like node-server) will error.

This change makes it so if the dev server is unable to inject script into response to build a new response (e.g. when there is no response.body at all), a response is still returned.

Copy link

changeset-bot bot commented Jan 15, 2025

🦋 Changeset detected

Latest commit: 4c9a223

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/vite-dev-server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gobengo
Copy link
Contributor Author

gobengo commented Jan 15, 2025

wdyt @yusukebe ?

@gobengo
Copy link
Contributor Author

gobengo commented Jan 15, 2025

Oh. I also just found this /~https://github.com/honojs/hono/pull/1142/files#diff-f42b11987a9538ff250a31ba4eb66ee557f7512860fae88c5020a83b5e97c28cR278
which seems to be the origin of the HEAD requests getting a response with body null.
iiuc without this change, any hono app using vite-dev-server here will crash when responding to HEAD requests.

@gobengo
Copy link
Contributor Author

gobengo commented Jan 15, 2025

I verified that a brand new honox app created via create-hono-app x-basic crashes with curl locahost:5173 -X HEAD. I think this PR would fix that

@yusukebe yusukebe changed the title Fix issue in dev-server where requestListener would return null fix(dev-server): Fix issue in dev-server where requestListener would return null Jan 16, 2025
@yusukebe yusukebe force-pushed the fix-dev-server-crash branch from a48c12a to 4c9a223 Compare January 16, 2025 07:01
Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

LGTM!

@yusukebe
Copy link
Member

Hi @gobengo !

Good fix! I've added a test for it by myself. Looks good. I'll merge this and release a new version. Thank you for your contribution.

@yusukebe yusukebe merged commit 01d28ca into honojs:main Jan 16, 2025
3 checks passed
@github-actions github-actions bot mentioned this pull request Jan 16, 2025
@gobengo
Copy link
Contributor Author

gobengo commented Jan 17, 2025

I'll merge this and release a new version.

Thanks!

Thank you for your contribution.

You're welcome. my pleasure

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.

2 participants