plainblack / ving

An opinionated web services starter for Nuxt3 that provides REST and per-field privileges out of the box.
https://plainblack.github.io/ving/
63 stars 7 forks source link

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

Closed rizen closed 3 months ago

rizen commented 3 months ago

Screenshot 2024-06-24 at 10 14 49 AM

rizen commented 3 months ago

I have learned a few things:

rizen commented 3 months ago

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 commented 3 months ago

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.