Skip to content

Conversation

@edivados
Copy link
Contributor

@edivados edivados commented Jul 23, 2024

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?

  • 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:

Open in StackBlitz

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:

Open in StackBlitz

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:
#1523
nksaraf/vinxi#292

Closes #1744

@changeset-bot
Copy link

changeset-bot bot commented Jul 23, 2024

⚠️ No Changeset found

Latest commit: c821ecf

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.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jul 23, 2024

@ryansolid
Copy link
Member

ryansolid commented Jul 23, 2024

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.

@katywings
Copy link
Contributor

+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

@edivados
Copy link
Contributor Author

edivados commented Jul 24, 2024

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

@netlify
Copy link

netlify bot commented Jan 29, 2025

Deploy Preview for solid-start-landing-page ready!

Name Link
🔨 Latest commit 9275c98
🔍 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...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@katywings katywings changed the title fix: middleware wrapper fix: add await to middleware response handling Jan 29, 2025
@dominikzogg
Copy link

thanks for fixing: #1426

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.

Await middleware responses

6 participants