Skip to content

Conversation

@kettanaito
Copy link
Member

@kettanaito kettanaito commented Apr 29, 2024

  • Rewrites the last step of the last exercise to use Hono for the server implementation instead of Express.
  • The implementation is the same, if now fewer, LOC (the diff is larger because I've added a few explanations and Prettier unwraps certain lines). We also drop 3 dependencies in favor of two.

app.use(compress())
// this is here so the workshop app knows when the server has started
app.head('/', (req, res) => res.status(200).end())
app.get('/', () => new Response())
Copy link
Member Author

Choose a reason for hiding this comment

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

HEAD requests are handled by GET handlers. This is true for Express as well.

* @fixme Not sure what Kent means by
* `req.query.search` and `req.search`.
*/
const searchParams = new URLSearchParams()
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only place I didn't figure out. There's some difference between req.search and req.query.search in Express, and I don't know it what that is.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't find req.search in Express API docs. Hmm.

Copy link
Member Author

Choose a reason for hiding this comment

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

@kentcdodds, was this logic perhaps originally flawed a bit? Shouldn't it be this:

if (req.query.search !== '') {
  // Then remove "search" from query
  // and redirect.
}

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

When running this locally, I'm getting:

TypeError: ctx.res.body.pipeThrough is not a function
    at compress2 (file:///Users/kentcdodds/code/epicweb-dev/react-server-components/node_modules/hono/dist/middleware/compress/index.js:12:41)


app.use(compress())
// this is here so the workshop app knows when the server has started
app.head('/', (req, res) => res.status(200).end())
Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking we're ok without this.

What you had before meant that when the browser requested the root of the app you'd just get an empty response instead of the index.html.

Copy link
Member Author

Choose a reason for hiding this comment

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

HEAD is generally better if you want to check if the app is running. But a good think about it is that you don't have to have an explicit app.head('/') to handle it. As I said, HEAD is handled by GET as a fallback, but will receive no response body, which is precisely what you want for the HEAD request.

@kettanaito
Copy link
Member Author

kettanaito commented Apr 29, 2024

When running this locally, I'm getting:

This likely means that res.body is not ReadableStream but it is something (otherwise we'd get "cannot read X of undefined").

Upon looking at it, that's precisely what happens:

http://localhost:3000
ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
[Function: body]
TypeError: ctx.res.body.pipeThrough is not a function

At some point, res.body is a function. That's odd.

Why this is happening

This is triggered by the serveStatic middleware for public files. The client requests /js/src/index.js, and Hono fails to find it, and somehow provides the serveStatic function with the entire Context as the value of ctx.res. It does have the .body property but it's a method, and that's why it breaks the compress middleware.

How to solve this

I had to use serveStatic middleware properly. Now it's working (locally), have an error about RSC endpoint handler.

@kettanaito
Copy link
Member Author

I fixed the previous error, and the app loads mostly fine. The GET /rsc/ request for some reason has trouble falling through to the dedicated handler, and instead gets stuck at the serveStatic+compress combination.

@kentcdodds
Copy link
Member

Bummer. Almost there!

@gabrielelpidio
Copy link

I sent a PR to Artem's fork that might fix the issues: kettanaito#1, didn't find a better way to collaborate, I hope I'm not stepping over or inconveniencing anyone 😅

@nicksrandall
Copy link
Contributor

nicksrandall commented Apr 30, 2024

FWIW #5 migrates the server to hono and removes the dependency on busboy entirely

@kentcdodds
Copy link
Member

I prefer what's happening in #5. Thanks!

@kentcdodds kentcdodds closed this Apr 30, 2024
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.

4 participants