Skip to content

$fetch: use useRequestFetch() when calling internal API during SSR #24813

@atinux

Description

@atinux

Describe the feature

When having API routes in Nuxt with authentication (using H3 useSession or nuxt-auth-utils), we need to forward the cookie to the API routes when fetching the data on SSR.

Not working

<script setup>
const { data: teams } = await useAsyncData('team', () => $fetch('/api/teams')
</script>

Working

<script setup>
const { data: teams } = await useAsyncData('team', () => useRequestFetch()('/api/teams')
</script>

Or using useFetch:

<script setup>
const { data: teams } = await useFetch('/api/teams', { key: 'teams' })
</script>

I believe it should work when we call our Nuxt API directly with $fetch to improve the DX.

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Later

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions