nuxt / nuxt

The Intuitive Vue Framework.
https://nuxt.com
MIT License
55.17k stars 5.05k forks source link

Wrong status codes in server plugins #27248

Open imslepov opened 6 months ago

imslepov commented 6 months ago

Environment

Reproduction

See reproduction link

Describe the bug

If using the render:response and afterResponse hooks in the server plugin, the statusCode doesn't match the one actually sent.

// File: server/plugins/OnResponsePlugin.ts

export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('render:response', (response) => {
    console.log('[render:response] statusCode:', response.statusCode); // always 200
  });

  nitroApp.hooks.hook('afterResponse', (event) => {
    console.log('[afterResponse] statusCode: ' + event.node.res.statusCode); // always 200
  });
});

Additional context

No response

Logs

No response

stackblitz[bot] commented 6 months ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

danielroe commented 6 months ago

Would you see if you can reproduce this in pure Nitro (reproduction sandbox), and if so, raise there? 🙏

imslepov commented 6 months ago

Hmm... Nitro doesn't hook the render:response (which is okay) and afterResponse, but in the beforeResponse hook this bug repeats, also returning a status code of 200. See Nitro's reproduction.

UPD: It looks like a bug in unjs/h3. I have created an issue for this.