N
Nuxtβ€’2w ago
Felix

How to keep state in SSR requests?

Hey, I am validating a session token by hitting a DB. I see it is hitting the DB once for every SSR request (e.g. useAsyncData.) How can I avoid the DB hit per SSR request?
26 Replies
Felix
Felixβ€’2w ago
So I have a 00.auth.ts middleware in server/middleware. It seems this middleware runs for every SSR request. My understanding that since it's server rendered all the state of the original request should be available. But the middleware runs repeatedly and the event object is a new object so I cannot cache state on it either
manniL
manniLβ€’7d ago
cache the result in your API endpoint might be the easiest
Felix
Felixβ€’7d ago
by what key?
manniL
manniLβ€’7d ago
the session token itself? πŸ€”
Felix
Felixβ€’7d ago
right i see... just store it in memory somewhere to fake the db lookup? assuming that it will soon get evicted because the lambda dies? what i don't understand is it's just a function call; the context is already on the stack somehwere. is it not possible to somehow access this?
manniL
manniLβ€’7d ago
well you can save it e.g. in a kv store in-mem is tricky with serverless
the context is already on the stack somehwere. is it not possible to somehow access this?
No, it is recreated per call πŸ™‚
Felix
Felixβ€’7d ago
Oh why is that
manniL
manniLβ€’7d ago
Various reasons, e.g. that you don't want cross state pollution πŸ™‚
Felix
Felixβ€’7d ago
What is cross state pollution?
manniL
manniLβ€’7d ago
state of user a is "accidentally" part of state of user b and with serverless or edge, you can't have a "consistently" shared context anyway (same with any multi instance deployment) you can still use plugins to make sure that each requests has a certain object/class/... available though
Felix
Felixβ€’7d ago
But if initial request binds to user a then for that request context how could it ever be user b?
manniL
manniLβ€’7d ago
oh wait maybe we are misunderstanding here πŸ™‚ I am talking about multiple initial requests
Felix
Felixβ€’7d ago
I really don't get it. So request flies in, route is matched, handler runs, some $fetch calls happen and since we are on the server a FN runs instead of a http req. In that FN all the context should be available no? Right. I'm taking about a single initial request. I made sure no other xhr requests show in the network tab. But event.context is always cleared and I don't know how I can access the initial request context
manniL
manniLβ€’7d ago
you could get the event via useRequestEvent()
Felix
Felixβ€’7d ago
What I am seeing is that for every $fetch the entire server/Middleware runs again on a fresh event object. Wouldn't getRequestEvent just return that?
manniL
manniLβ€’7d ago
no, this is correct. it is treated the same way when doing an "external" request to that endpoint
Felix
Felixβ€’7d ago
Which brings me back to why and how
manniL
manniLβ€’7d ago
Alexander Lichter
YouTube
Nuxt vs. Nitro - What does what in your App
🀝🏻 Nuxt and Nitro go hand in hand - but sometimes so well that the borders between both blur for developers. Wouldn't it be helpful to understand why Pinia composables can't be used in the server folder and when exactly Nitro comes into play? Fear no more - all of that and more will be covered in this video! Key points: πŸ‘€ When Nitro is used in ...
manniL
manniLβ€’7d ago
think of nitro as standalone api server which also renders your nuxt app but these are distinct tasks
Felix
Felixβ€’7d ago
Sorry if I come across rude but I don't understand how sth so simple can be made to be so difficult it can't be explained in one sentence . Kind of baffling. I'm surprised I also couldn't find answers on google. I love working with Nuxt overall. This is the first hol'on a minute moment. I'll give the video a watch tonight.
manniL
manniLβ€’7d ago
I'm happy to try again. Could you maybe reword the question? When you do calls "on the client side" of your app to an API endpoint, then it is simply not different than when calling them via postman or just putting the URL in the browser (except you might pass headers and similar to them) Commonly you authenticate your user with the session ID, send the ID to the API and your backend will check validity. This is the case for any kind of stateless API
Felix
Felixβ€’7d ago
Yup but $fetch short-circuits and runs the code immediately on the server during the initial request. No client involved For SSR pages
manniL
manniLβ€’7d ago
that's correct, on the server it does! There, $fetch calls are not actually happening but are emulated because you can just run the event handler's code πŸ‘ But not on the client side, as you said
Felix
Felixβ€’6d ago
Yes... So on the server why can't I get access to state established in the initial request object I noticed that when I do a $fetch from my middleware/01.auth.global.ts middleware, the initial event context is retained! That is what I want. But why is this not happening for $fetch requests issued in page components is what I am trying to figure out. I cloned nuxt and will start digging... ok. so idk if this is a bug in nuxt or not. but check this out:
const x = useAsyncData(
() => {
// this will NOT retain context in SSR call:
const fetch = $fetch;

// this retains context in SSR call:
// const fetch = useRequestFetch();

return fetch("/api/user");
},
{
immediate: true,
server: true,
watch: false,
},
);
const x = useAsyncData(
() => {
// this will NOT retain context in SSR call:
const fetch = $fetch;

// this retains context in SSR call:
// const fetch = useRequestFetch();

return fetch("/api/user");
},
{
immediate: true,
server: true,
watch: false,
},
);
Felix
Felixβ€’6d ago
Felix
Felixβ€’6d ago
and filed a bug. we'll see what comes of it.. https://github.com/nuxt/nuxt/issues/27909
GitHub
SSR Context Lost Β· Issue #27909 Β· nuxt/nuxt
Environment Operating System: Darwin Node Version: v21.6.2 Nuxt Version: 3.12.2 CLI Version: 3.12.0 Nitro Version: 2.9.7 Package Manager: pnpm@8.8.0 Builder: - User Config: ssr, devtools Runtime Mo...
Want results from more Discord servers?
Add your server
More Posts
How are you resolving routes while using I18n module?Previously, I obtained route paths by passing an object with route name and parameters to NuxtLink, error connected with nuxtui-pro that text-foreground class doesn' t exist in module[2:17:00 AM] ERROR Pre-transform error: [postcss] /node_modules/.pnpm/@nuxt+ui-pro@1.3.1_nuxt@3.11NuxtI18n - Vue router warnings after language changeHello everyone! Every time I change language with useSwitchLocalePath() of NuxtI18n I get all the rositemap module wont show dynamic routes, routes are emptywe tried to create on root ebene ( /server/api/__sitemap__/urls.ts ) the following ``` import { asSStop fetching favico on every requestHi, How could I prevent fetching every request twice, once with the actual data I'm looking for, aExtend NuxtUI Form ComponentI'm using Nuxt UI and I want to extend the UForm component, to always scrol lthe first error into viNuxt does not render updated content after buildI've integrated WordPress for blogs on our site. At the time of build, the blogs that are already thWARN [nuxt] useAsyncData should return a value that is not null or undefined or the request may beWith the latest update i get this multiple times, but there is no way to see which code is the causeDirective v-debounceHello! I have been trying for a while to create a custom directive debounce.ts in Nuxt without succBest way to cache a pre-rendered SSG project?Hello! Currently I am working in a project that aims to prerender using data from a Database around