ving icon indicating copy to clipboard operation
ving copied to clipboard

calling useVingKind().all() when doing an SSR without a middleware causes Nuxt to crash

Open rizen opened this issue 1 year ago • 7 comments

Screenshot 2024-06-24 at 10 14 49 AM

rizen avatar Jun 24 '24 17:06 rizen

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

rizen avatar Jun 25 '24 02:06 rizen

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`);
});

rizen avatar Jun 25 '24 20:06 rizen

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.

rizen avatar Jun 25 '24 20:06 rizen

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.

rizen avatar Oct 30 '24 21:10 rizen

Try to make a reproduction that can be reported to Nuxt.

rizen avatar Oct 30 '24 21:10 rizen

possibly related: https://github.com/nuxt/nuxt/issues/27660

rizen avatar Oct 30 '24 21:10 rizen

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.

perlDreamer avatar Oct 30 '24 23:10 perlDreamer