Closed leo91000 closed 9 months ago
i have this proboeml too, it's not working in server request.
like the counter demo ,if use await useFetch("/api/counter")
can't set session
Same issue, session is null in server side!!!
In client has value!!
We must use session?.id in template because in server side session is null!
Server Side
Client side
The composable for useSession
seems to skip the server with useFetch
.
https://github.com/sidebase/nuxt-session/blob/main/src/runtime/composables/useSession.ts
Without testing this library, I wonder how necessary this is. We've found that useFetch
can access cookies on our SSR pages (at least using Lucia Auth with its provided h3 middleware, which uses cookie sessions). On the other hand, $fetch
cannot be used to access the session (and would need the cookies forwarded).
I don't know enough about this though and the official Nuxt documentation around this situation is a bit ambiguous. But I have tested this in local dev, logging in with a user, then disabling JavaScript on the site via Chrome; the current user's email appears in one SSR-renderable part of the screen without fail.
In our case we were using a plain useFetch
composable:
const { data, error, refresh, pending } = await useFetch('/api/auth/user', { method: 'GET' })
Back to this library, it's interesting that this comment was left there:
// Do not fetch on server, as the cookie is stored and sent by the client
server: false,
And you can see in @mlutsiuk's pull request (https://github.com/sidebase/nuxt-session/pull/73) that they're forwarding the cookies but still have that flag disabled. I suspect it might have something to do with the way caching is handled but I don't have enough in-depth knowledge.
Any notice???
Environment
Reproduction
https://stackblitz.com/edit/github-e43zzb?file=app.vue
If you type something in the input and refresh the page, the server will not know about the session that was updated, thus preventing doing server side rendering without node mismatch.
Describe the bug
I'd like to access my sessions in my root
app.vue
file :If I run this code, it works well on client side. However on server side the value of the session is always empty. Can I access the data on server side so I can do proper server side rendering ?
Additional context
No response
Logs
No response