-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
: use useRequestFetch()
when calling internal API during SSR
#24813
Comments
This is a nice idea to improve DX, considering cases where users are directly using I would suggest alternatively this to be shipped as part of factory function fetches. We could introduce also a preconfigured |
We agreed to defer discussion on this until #14736. |
Had this problem using lucia auth example, with this server middleware: https://github.com/lucia-auth/examples/blob/main/nuxt/github-oauth/server/middleware/auth.ts when using
when using
I was trying to figure why |
I had the same problem as @blqke. Because I didn't find info about export default defineNuxtRouteMiddleware(async (to, from) => {
const authCookie = useCookie("auth").value;
const user = useUser();
const headers = new Headers();
if (authCookie) {
headers.set("Cookie", `auth=${authCookie}`);
}
const data = await $fetch("/api/auth/user", { headers });
if (data?.user) {
user.value = data.user;
}
}); mentioning |
Also, this should really be documented. |
…g request @ `queryFunction()` * rename `bodyText: string` to `responseBody: unknown` and serialize it before passing to parent ctor as the value of `Error.message` @ class `FetchResponseError` @ api/index.ts * rename `NUXT_PUBLIC_API_BASE_URL` to `NUXT_PUBLIC_API_ENDPOINT_PREFIX` as it now should be an absoulte url due to nuxt/nuxt#24813 & https://github.com/nuxt/nuxt/blob/f2868f8c72320009e4502f97456b13f77c6b0322/packages/nuxt/src/app/composables/fetch.ts#L172-L178@ .env.example & nuxt.config.ts @ fe
How to handle this issue more generally and what should be done when we don't use fetch directly?! Assume that we are using an SDK function in <script setup>
const { data: userProfile } = await useAsyncData('user-profile', () => $api.getCurrentUser())
</script> Assuming that login has already been performed and the appropriate In general, any information present in the context of a page request, including Dear @danielroe and @pi0 , I would appreciate your thoughts on this matter. Also, what are some current solutions for the example I mentioned? That is, when using an SDK and not having direct access to fetch, what should we do to avoid SSR issues? |
I can't seem to get the session on my server side.
any idea of what I could be doing wrong here? |
Describe the feature
When having API routes in Nuxt with authentication (using H3
useSession
or nuxt-auth-utils), we need to forward thecookie
to the API routes when fetching the data on SSR.Not working
Working
Or using
useFetch
:I believe it should work when we call our Nuxt API directly with
$fetch
to improve the DX.Additional information
Final checks
The text was updated successfully, but these errors were encountered: