Get dynamic array of page names in Nuxt application
I have a scenario where I'd like to add a middleware that references an array of all the page names that exist at the root of the application (i.e. /user, /faq, /about, etc.).
Rather than hardcode an array of all those names
['user', 'faq', 'about']
, is there some way via Nitro or Nuxt to parse/call to get all the page names that exist within the current Nuxt application?7 Replies
Perhaps this is something Vue Router exposes, or Nitro exposes?
This is what AI suggests, but I imagine won't work - but the methodology of iterating through the pages directory on the server-side during middleware execution to build a list of page names seems like the right direction:
I don't see that there's a direct way to read the filesystem in Nitro / h3, since it could be Bun, or Deno, or CF, or what have you
Vite’s
import.meta.glob
might be helpfulVite
Next Generation Frontend Tooling
hmmm
I'd go with unstorage / useStorage I'd say. But generating that would be better at build time, no? 🙂
Yeah, I’d rather do it at build time. I just wasn’t sure what the easiest way would even be to parse all of the page path names that exist in a project
I'd say just hook into the route extends hook and use the result there 😂
Is there a way to hook into the event inside a defineEventHandler in server/middleware that might be able to access the app's routes?
I'm ultimately trying to (on the server side - via Nitro) do a 301 redirect, but ONLY if it doesn't match any of the app's existing page names.
This is working, but I'd like to replace the
paths
array with something that dynamically pulls those names:
So this in essence does:
If we're on a normal page route, proceed....
If not, we might be a username (like domain.com/manniL), so redirect them to domain.com/user/manniL