Closed jaspernbrouwer closed 2 years ago
This error is correct here. Server routes run in a separate context and cannot access useNuxtApp
(or, indeed, any Vue composables). Moreover, they run before the vue renderer, meaning that any plugins you create will not have run by the time your server handlers are running. (And in fact, for API routes, the Vue handler will never be called and your nuxt plugins/route middleware/etc will never run.)
Ah, then I guess server-plugins are for SSR only. Good to know, thanks!
Environment
Darwin
v18.9.0
3.0.0-rc.9
0.5.1
yarn@1.22.19
vite
imports
,modules
,typescript
@nuxtjs/tailwindcss@5.3.3
,unplugin-icons/nuxt
-
Reproduction
I'm following the guide to create a (server only) plugin: https://v3.nuxtjs.org/guide/directory-structure/plugins
Then follow the guide to create a server-route that uses that plugin: https://v3.nuxtjs.org/guide/features/server-routes
I based the line with
useNuxtApp()
on: https://v3.nuxtjs.org/guide/directory-structure/plugins#automatically-providing-helpersFinally I browse to the application, hitting something that fetches the server-route, but the "Hello World!" string isn't getting through (meaning that whatever I use the value in, doesn't get rendered).
I do notice that when starting the Nuxt app, I see this error:
Describe the bug
The error is pretty clear to me, but what's not clear is what alternative to use. The docs state nothing about this.
The docs do state:
What is the correct way of using a plugin on the server side?
Additional context
I'm submitting this as a bug because I believe it is, either in the software or in the docs :)
Logs