calling useVingKind().all() when doing an SSR without a middleware causes Nuxt to crash
I have learned a few things:
- it only happens on a page that uses useVingKind().all() and that doesn't use the auth middleware
- if i try to bring the middleware code directly into the page, it gives the error
- if i try to bring the useCurrentUser() code directly into the middleware, it gives the error
- if i take the code from all() and put it directly into the page, then remove the middleware, it DOES work
Turns out if you create a middleware that just fetches anything at all, that will also work around this problem.
export default defineNuxtRouteMiddleware(async () => {
await $fetch(`/api/v1/test`);
});
This bug is caused by something in useRequestEvent() inside of Nuxt, which is used in useRest() in ving, which in turn calls useRequestHeader() which calls useRequestEvent().
The first call to useVingKind() .all() is fine in SSR, but a secondary call will crash unless it has a middleware that fetches something with $fetch() like the all-workaround middleware I have added to ving.
Alternatively, don't call all, and instead just run the code you want to run in the page rather than inside the useVingKind() composable.
I hate that we have to have this work around, but I can't figure out any other way to solve this problem. Hopefully whatever is broken inside of Nuxt will be fixed in the future.
It turns out that the last middleware must have something that fetches. And it's not good enough to potentially have a fetch in it, it must actually make the call. Thus caching can impede this.
Try to make a reproduction that can be reported to Nuxt.
possibly related: https://github.com/nuxt/nuxt/issues/27660
The users page doesn't have this problem, and it has two kinds of authentication middleware that both use useCurrentUser. You can use it as a test page for Ving by switching .search to .all and moving the page size down to something small, like 1.