question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Route Middleware does not get triggered if its defined inside layout

See original GitHub issue

Environment

  • Package Manager: npm@8.9.0
  • Builder: vite
  • User Config: modules, ssr, vite, plugins, css
  • Runtime Modules: @nuxtjs/tailwindcss@5.1.2, @pinia/nuxt@0.1.9
  • Build Modules: -

Reproduction

StackBlitz URL: https://stackblitz.com/edit/github-ghahvp?file=middleware/testmid.ts

Describe the bug

middlewares are working for pages, but not for layouts see stackblitz.

definePageMeta({
     middleware: ["somelayout"]
})

If you use this code inside an page, it works. But if you use it inside an layout, and an page uses that layout it does not get triggered. This functionality actually works in Nuxt2

Additional context

My intention was:

I have 2 layouts: - Default (no auth) - Dashboard (with auth)

In nuxt 2 i was able to put my middleware inside of the dashboard layout. Whenever i made a new page for the dashboard, i just added layout: "dashboard" and the dashboard CSS with the authentication middleware got applied.

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
danielroecommented, Aug 23, 2022

@lobo-tuerto You can define the middleware in a parent page, for example.

~/pages/admin.vue <-- set here
~/pages/admin/index.vue <-- applies here
~/pages/admin/[slug].vue <-- and here
1reaction
lobo-tuertocommented, Aug 23, 2022

@danielroe Thanks for the prompt reply. Yes that’s one strategy, but I was wondering how to go about that for top level directory pages (without nesting). 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

Global Middleware not being triggered
When I try to access an URL that doesn't exists inside routes.ts it gives a standard 404 error, but I want to get...
Read more >
Middleware - Laravel - The PHP Framework For Web ...
For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware...
Read more >
Using middleware
Route handlers enable you to define multiple routes for a path. The example below defines two routes for GET requests to the /user/:id...
Read more >
Complete Guide to Express Middleware
Express executes middleware in the order they are added, so if we make ... Next we have defined a route with url product...
Read more >
Middleware | NestJS - A progressive Node.js framework
Middleware is a function which is called before the route handler. ... middleware function in the stack. if the current middleware function does...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found