Code Monkey home page Code Monkey logo

Comments (10)

manniL avatar manniL commented on September 20, 2024 3

Probably easier to build a wrapper for defineEventHandler and similar that covers that ☺️ @Mokkapps

from nuxt.

pi0 avatar pi0 commented on September 20, 2024 3

Allowing to disable certain features from Nuxt/Nitro layers (server routes, pages, components) is something we might think about to support in Nuxt πŸ‘πŸΌ (we might introduce a new $layer configuration API to allow disabling certain routes)


Generally, I would highly advice to prefer the composable/util methods over global patterns such as plugins and middleware (also not wrappers You might want to use object syntax for fine control instead). especially for Nuxt layers when a layer can have undesired global side effects.


@samydoesit I got your immediate question but I think we might think of some faster solutions. Do you mind to update your sandbox/repro with the current multi-app of your project?

from nuxt.

Mokkapps avatar Mokkapps commented on September 20, 2024 2

@manniL Thanks, we are already investigating that workaround. Do we have some Nitro hook which runs before all server routes/middlewares where we can check the runtimeConfig variable?

from nuxt.

manniL avatar manniL commented on September 20, 2024 1

One way could be solving this in userland with e.g. a runtimeConfig value to check before executing the middleware/routes this in a layer πŸ‘

Updated StackBlitz

from nuxt.

stackblitz avatar stackblitz commented on September 20, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

from nuxt.

samydoesit avatar samydoesit commented on September 20, 2024

Thank you for your prompt response.

I attempted to implement a wrapper solution, but encountered several challenges. Simply checking within the server route proved ineffective, as the event handler continued to return 'false', even after an early return.

While using the wrapper, I observed two peculiar behaviors:

  • When making a GET request via curl to the logout route, the server returns an error message (Invalid lazy handler result. It should be a function) instead of serving the HTML. However, when accessing the same route through a browser, the page renders correctly. Here's the console error:
[nuxt] [request error] [unhandled] [500] Invalid lazy handler result. It should be a function:
  at /dev/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1617:17  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)  
  at async Object.handler (/dev/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1697:19)  
  at async Server.toNodeHandle (/dev/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1907:7)
  • Incorporating the defineCustomEventHandler wrapper within the middleware causes the Nuxt application to fail to render anything, displaying the following console error instead: ERROR [worker reload] [worker init] Cannot read properties of undefined (reading 'handler').

You can verify this here: Updated Stackblitz

from nuxt.

samydoesit avatar samydoesit commented on September 20, 2024

Hello, thank you very much for the support.
I created an updated Stackblitz using the multi-app.
Multi-App Monorepo Stackblitz

from nuxt.

mlbonniec avatar mlbonniec commented on September 20, 2024

Hi :) I'm currently facing the same issue.

We're building about 10 websites, depending on the same OAuth provider. To simplify those websites, we've created a utils library, containing a notification helper, global styles etc. and a server API middleware, to exchange authentication tokens safely.

However, one of our website (the authentication website) doesn't need this middleware, as it's automatically redirecting users to other websites.

As a workaround, our library isn't using layer, but module, to allow configuring styles, authentication etc.

from nuxt.

danielroe avatar danielroe commented on September 20, 2024

You can disable server routes and middleware with a Nuxt hook:

export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: [
    function (_, nuxt) {
      nuxt.hook('nitro:init', nitro => {
        nitro.options.scanDirs = []
      })
    }
  ]
})

You could add this tiny module or hook into your capacitor app.

from nuxt.

samydoesit avatar samydoesit commented on September 20, 2024

Thank you for your feedback. I encountered an issue when settingscanDirs to an empty array, as it also removes our Nitro route types, which our application's code relies on. Is there a way to selectively enable or disable a specific layer route or middleware?

from nuxt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.