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

docs: setting middleware at build time #23480

Merged
merged 7 commits into from Dec 14, 2023
Merged

Conversation

justinkekeocha
Copy link
Contributor

Instead of using 'definePageMeta' on each page, it may be handy to just on certain routes that should have a certain middle ware

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Instead of using 'definePageMeta' on each page, it may be handy to just on certain routes that should have a certain middle ware
@stackblitz
Copy link

stackblitz bot commented Sep 30, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@nuxt-studio
Copy link

nuxt-studio bot commented Sep 30, 2023

βœ… Live Preview ready!

Name Edit Preview Latest Commit
Nuxt Docs Edit on Studio β†—οΈŽ View Live Preview 31a7f07

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I would recommend this as it will only run on the first load and not on subsequent navigations.

You could however add the named middleware in ~/app/router.options.ts or even at build time in pages:extend hook, which would probably be the most performant option.

@justinkekeocha
Copy link
Contributor Author

I tried it and you are correct. could you please show how to do this from the pages:extend hook

@danielroe
Copy link
Member

import type { NuxtPage } from 'nuxt/schema'

export default defineNuxtConfig({
  hooks: {
    'pages:extend' (pages) {
      function setMiddleware (pages: NuxtPage[]) {
        for (const page of pages) {
          if (/* some condition */ true) {
            page.meta ||= {}
            // Note that this will override any middleware set in `definePageMeta` in the page
            page.meta.middleware = ['named']
          }
          if (page.children) {
            setMiddleware(page.children)
          }
        }
      }
      setMiddleware(pages)
    }
  }
})

@danielroe danielroe marked this pull request as draft October 2, 2023 10:45
@justinkekeocha justinkekeocha marked this pull request as ready for review October 2, 2023 11:35
@justinkekeocha justinkekeocha changed the title docs: run middleware from plugins docs: apply middleware to route groups Oct 2, 2023
Apply to route groups using page:extend hook
@justinkekeocha
Copy link
Contributor Author

justinkekeocha commented Oct 2, 2023

Thank you for your help Dan. While I was waiting for your answer yesterday, I went experimenting and discovered that i could achieve the same thing by creating a global middleware and calling other middleware like this:

import auth from "~/middleware/auth";
import admin from "~/middleware/admin";

export default defineNuxtRouteMiddleware((to, from) => {
  const currentRoute = to.fullPath;

  if (currentRoute.includes("/dashboard")) {
         //The check for admin group should come first, else /dashboard will run and return
        //Then routes like /dashboard/admin will pass
       if (currentRoute.includes("/admin")) {
         return admin(to, from);
       }
       return auth(to, from);
  }
});

What do you think about this?

@danielroe danielroe changed the title docs: apply middleware to route groups docs: setting middleware at build time Dec 14, 2023
@danielroe danielroe merged commit 677a144 into nuxt:main Dec 14, 2023
4 checks passed
@github-actions github-actions bot mentioned this pull request Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants