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

$fetch interceptor not modifying the retried request when a body is present #27042

Open
cmgdragon opened this issue May 2, 2024 · 5 comments

Comments

@cmgdragon
Copy link

Environment

  • Operating System: Windows_NT (Win 11)
  • Node Version: v20.12.2
  • Nuxt Version: 3.11.2
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.6
  • Package Manager: [email protected]
  • Builder: -
  • User Config: devtools
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/nuxt-starter-jcgqeq?file=app.vue

Describe the bug

coming from #26991

The $fetch interceptor isn't modifying the request headers during a retry if the request has a body.

I'm trying to implement a refresh token functionality using $fetch interceptors along with the retry option. The logic is that when I get a 401 Unauthorized response, the token gets updated and the request is refetched using the new token.

It's working just as expected for simple GET requests, but when the same request contains a body, it doesn't update the headers during the retried petition.

I tried to debug inside the onRequest interceptor to see if the token was truly being updated, and yes, it was; but the request keeps the old token anyways.

The expected behavior would be that the request headers would be modified regardless of whether a body is present

Additional context

In the reproduction link you can see how the uuid isn't being updated in the ssr terminal when the POST includes a body, but it's actually being updated in the onRequest interceptor (open the devtools to see it)

Logs

No response

Copy link

stackblitz bot commented May 2, 2024

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

@danielroe
Copy link
Member

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

It’s likely an ofetch issue if so.

@cmgdragon
Copy link
Author

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

It’s likely an ofetch issue if so.

I've been able to reproduce it with a simple express server + simple vue app (no Nuxt/Nitro needed).. so It may truly be an ofetch issue.
Should I report it directly to unjs/ofetch or do the same with Nitro instead of express and report it there..?

@danielroe
Copy link
Member

No, it's even better if you can report directly to ofetch ❤️

@cmgdragon
Copy link
Author

I just realized what's going on here. $fetch is transforming the headers object into a Headers class if there's a body and the method is other than GET; so if we modify the headers just as shown in the useFetch documentation, when the request is retried and loops back to the onRequest interceptor, these are being added as extra properties of the Headers object and ignored later

A proposed solution is to normalize the object before hitting the onRequest (unjs/ofetch#392)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants