addServerMiddleware deprecation and replacement with addServerHandler
See original GitHub issueEnvironment
Nuxt CLI v3.0.0-rc.3 11:44:59 RootDir: C:\Users\auser\Desktop\adir\anotherdir\examples\nuxt 11:45:01 Nuxt project info: 11:45:01
- Operating System:
Windows_NT
- Node Version:
v18.4.0
- Nuxt Version:
3.0.0-rc.3
- Package Manager:
yarn@1.22.17
- Builder:
vite
- User Config:
modules
,nitro
,buildModules
,alias
,runtimeConfig
- Runtime Modules:
~/modules/nuxt-mdg
- Build Modules:
nuxt-windicss@^2.2.8
Reproduction
Internal Repo.
Describe the bug
Hello fellow nuxt 3 team!
Initially, I’ve created a module which uses a token middleware. This middleware was used with the current deprecated function addServerMiddleware as:
addServerMiddleware(join(__dirname, "server/middleware/token.ts")
After the deprecation of the method and the replacement of it with addServerHandler, the nuxt project would not start up giving me the following error:
ERROR [worker reload] [worker init] Unable to find global scope. Are you sure this is running in the browser?
and by removing the addServerMiddleware functionality everything worked as before (Without the token) So I replaced it with the newer addServerHandler(handler) as below:
addServerHandler({ route: "/token", middleware:true, handler: join(__dirname, "server/middleware/token.ts"), })
but the new error I getting was :
ERROR [worker reload] [worker init] Only URLs with a scheme in: file, data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol ‘c:’
Doing some searching I found out that many people mentioned it is a node version issue but that’s not the case since I am using the latest version.
Is it possible to have a working example of addServerHandler?
Thank you in advance nuxt team and @danielroe !!
Additional context
No response
Logs
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:14 (4 by maintainers)
Top GitHub Comments
Thanks for your reply @danielroe seems the problem is fixed now. I managed to upgrade to 3.0.0-rc.6 and fixed the code. The error does not appear anymore after initial tests 🎉
The reason we’re using a string is that we want to keep build config (e.g. nuxt.config) separate from actual runtime code. Functions within nuxt config can’t be safely serialised to something we want to run/include in the nitro rollup build.
👍 And yes, it would be good to add a comment in JSdoc pointing to the h3 docs/helper function for defining such a handler: http://v3.nuxtjs.org/guide/features/server-routes.