Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using server middlewares breaks both dev and build servers #13539

Closed
arhubi opened this issue Mar 21, 2022 · 3 comments
Closed

Using server middlewares breaks both dev and build servers #13539

arhubi opened this issue Mar 21, 2022 · 3 comments

Comments

@arhubi
Copy link

arhubi commented Mar 21, 2022

Environment

  • Operating System: Darwin
  • Node Version: v16.14.0
  • Nuxt Version: 3.0.0-27460489.53fbca7
  • Package Manager: yarn@1.22.17
  • Builder: vite
  • User Config: typescript, build, vite, css, buildModules, unocss, fifteenSitemap, serverMiddleware, meta, publicRuntimeConfig
  • Runtime Modules: -
  • Build Modules: @unocss/nuxt@0.22.7, @vueuse/nuxt@7.5.3, ./modules/nuxt/i18n, ./modules/nuxt/sitemap, ./modules/nuxt/version

Reproduction

https://stackblitz.com/edit/github-kc5f98

Describe the bug

When a serverMiddleware array is defined in nuxt.config.ts, Nuxt throws an error at start (in dev or build mode).

I will update this issue when I find the commit which introduced this apparent bug.

Additional context

No response

Logs

ERROR  Cannot start nuxt:  Cannot read properties of undefined (reading 'handle')                       
  at Function.use (node_modules/connect/index.js:87:21)
  at Object.set [as setLegacyMiddleware] (node_modules/@nuxt/nitro/dist/index.mjs:1898:13)
  at eval (node_modules/nuxt3/dist/chunks/nitro-legacy.mjs:70:17)
  at async initNuxt (node_modules/nuxt3/dist/chunks/index.mjs:1117:3)
  at async load (node_modules/nuxi/dist/chunks/dev.mjs:6762:9)
  at async Object.invoke (node_modules/nuxi/dist/chunks/dev.mjs:6803:5)
  at async _main (node_modules/nuxi/dist/cli.mjs:57:7)
@Aareksio
Copy link
Contributor

Aareksio commented Mar 21, 2022

I believe the key is handle not handler. After changing it, the middleware works, although path is not respected. For health endpoint you can use /server/api directory, documented here.

Not sure if path parameter is obsolete, but a little further into the doc it is suggested that middleware from /server/middleware runs on all routes and must include logic to prevent it from executing on unintended paths.

The serverMiddleware config seems to be obsolete in favor of filesystem discovery, the documentation may be outdated.

@danielroe
Copy link
Member

Workaround for now is:

import { defineNuxtConfig } from 'nuxt3';

// https://v3.nuxtjs.org/docs/directory-structure/nuxt.config
export default defineNuxtConfig({
  serverMiddleware: [
    {
-     path: '/_/health',
+     route: '/_/health',
-     handler: '~/server/health.ts',
+     handle: '~/server/health.ts',
    },
  ],
});

@danielroe
Copy link
Member

danielroe commented Mar 21, 2022

This is caused by #13532, because the same object is inserted twice, so when we delete its properties on parsing it the first time, we inadvertently create a {} serverMiddleware:

https://github.com/nuxt/framework/blob/03d5fdde2d13fb4d900040c2a6f127c2347b11e1/packages/nitro/src/server/middleware.ts#L82-L88

Let's follow at #13532.

cc: @pi0

@danielroe danielroe added the 3.x label Jan 19, 2023
@danielroe danielroe transferred this issue from nuxt/framework Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants