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

fix(nuxt): set useFetch accept default to application/json #25390

Closed
wants to merge 1 commit into from

Conversation

danielroe
Copy link
Member

πŸ”— Linked issue

resolves #24742
resolves #24609
resolves #23539

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

When using useFetch, we automatically use the request event fetch, which preserves headers from the initial request. Unfortunately, this also proxies the accept header, which is text/html from the browser, and can result in all local fetches with useFetch returning HTML error messages rather than JSON.

This PR also normalises the headers object (which was not previously working with nested refs).

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have added tests (if possible).
  • I have updated the documentation accordingly.

Copy link

stackblitz bot commented Jan 23, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.


// Use fetch with request context and headers for server direct API calls
if (import.meta.server && !opts.$fetch) {
const isLocalFetch = typeof _request.value === 'string' && _request.value[0] === '/' && (!toValue(opts.baseURL) || toValue(opts.baseURL)![0] === '/')
if (isLocalFetch) {
_$fetch = useRequestFetch()
// Set default `accept` value to avoid it defaulting to `text/html` (from initial browser request)
headers.accept = 'application/json'
Copy link
Member

@pi0 pi0 Jan 29, 2024

Choose a reason for hiding this comment

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

Maybe instead avoid to proxy accept header? (I guess bits are in h3 for proxy, but don't remember code we might also have list still in Nuxt for default headers to ignore)

This seems a bug that we proxy browser's accept header down to API calls. ofetch defaults to the right accept header based on body but if we explicitly ovveride here, it cannot do this anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, let's fix it upstream then πŸ‘

Copy link
Member Author

Choose a reason for hiding this comment

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

@danielroe
Copy link
Member Author

closing ~> unjs/h3#646

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