fix: middleware wrapper
PR Checklist
Please check if your PR fulfills the following requirements:
- [ ] Tests for the changes have been added (for bug fixes / features)
PR Type
What kind of change does this PR introduce?
- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, local variables)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] CI related changes
- [ ] Documentation content changes
- [ ] infrastructure changes
- [ ] Other... Please describe:
What is the current behavior?
onRequest
Not awaiting sendWebResponse can cause event.handled to not be true by the time h3 checks it and the next hook/handler will potentially be called.
https://github.com/unjs/h3/blob/6d1e48438bcbbecfaafa4b9fb800bfb1a03ee592/src/event/utils.ts#L76-L83
https://github.com/unjs/h3/blob/6d1e48438bcbbecfaafa4b9fb800bfb1a03ee592/src/utils/response.ts#L531
Reproduction:
Refresh enough times and you will get one of 3 results.
- Page instead of file content
- File content with wrong content-type header
- Crash
ERR_STREAM_WRITE_AFTER_END
onBeforeResponse
Calling sendWebResponse in here does not seem to be supported. There are no checks for event.handled by h3 unlike for onRequest and the next hook will be called.
https://github.com/unjs/h3/blob/6d1e48438bcbbecfaafa4b9fb800bfb1a03ee592/src/event/utils.ts#L84-L91
Reproduction:
Refresh enough times and you will get one of 3 results.
- Page with wrong content-type header
- File content with correct content-type header
- Crash
ERR_STREAM_WRITE_AFTER_END
What is the new behavior?
onRequest
Await sendWebResponse
onBeforeResponse
Assign return value to response.body instead of calling sendWebResponse. With this the response value of handler or previous hook will be replaced.
H3 will call sendWebResponse for Response objects. https://github.com/unjs/h3/blob/6d1e48438bcbbecfaafa4b9fb800bfb1a03ee592/src/app.ts#L185 https://github.com/unjs/h3/blob/6d1e48438bcbbecfaafa4b9fb800bfb1a03ee592/src/app.ts#L277-L279
Other information
Related issues: https://github.com/solidjs/solid-start/issues/1523 https://github.com/nksaraf/vinxi/issues/292
⚠️ No Changeset found
Latest commit: c821ecfcedcbbc0f633274ac28021877160f8246
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package or glob expression "solid-start-docs" is specified in the `ignore` option but it is not found in the project. You may have misspelled the package name or provided an invalid glob expression. Note that glob expressions must be defined according to https://www.npmjs.com/package/micromatch.
More templates
- example-bare
- example-basic
- example-experiments
- example-hackernews
- example-notes
- example-todomvc
- example-with-auth
- example-with-authjs
- example-with-drizzle
- example-with-mdx
- example-with-prisma
- example-with-solid-styled
- example-with-tailwindcss
- example-with-trpc
- example-with-unocss
- example-with-vitest
npm i https://pkg.pr.new/@solidjs/start@1588
commit: 9275c98
I had a suspicion there was was some timing disconnect here but I hadn't gotten into it. Your explanation makes sense to me. Very much appreciate you looking into this.
I guess with this we change it to how I'd expect it to work (and how it is documented). I'm just wondering if we should change onBeforeResponses API or if we should make people set the body themselves. I feel like Nitro designed it that way for a reason.
+1 for making it as close to the Nitro/h3 design as possible. One additional thought that I wanna leave here is about h3 v2: the new way will be to return the value/Response/stream directly instead of using sendXYZ(xyz) in eventHandlers. As it currently stands utilities like sendWebResponse will be deprecated in v2 https://github.com/unjs/h3/blob/cda0bb29b243625516aa57e9497126e8d3594c7f/MIGRATION.md#response-handling
I don't have much of an opinion about returning a response. It's an additional option that h3 doesn't have. Maybe convenient to some and makes onRequest and onBeforeResponse a bit similar/consistent.
@katywings Unclear to me if this applies to event handler hooks in v2. They don't seem to have changed much and return value of onRequest isn't handled. Maybe work in progress or I am missing something. https://github.com/unjs/h3/blob/cda0bb29b243625516aa57e9497126e8d3594c7f/src/handler.ts#L81
Deploy Preview for solid-start-landing-page ready!
| Name | Link |
|---|---|
| Latest commit | 9275c983e6cdafa2bc8e2f6e2c31fa2c9973e717 |
| Latest deploy log | https://app.netlify.com/sites/solid-start-landing-page/deploys/679a98aea8399d000880ea3b |
| Deploy Preview | https://deploy-preview-1588--solid-start-landing-page.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
thanks for fixing: https://github.com/solidjs/solid-start/issues/1426