Open Tommertom opened 2 years ago
Not exactly a duplicate, but it feels like this could be folded into #1142.
Seems indeed - except that I don't hope I need to parse xml programmatically then :)
Can you use https://vitejs.dev/guide/features.html#glob-import ? That's how I've seen this done in the past
@Tommertom as a temporary solution, you can use the following as a basis for what you're trying:
I've recently implemented this for a SvelteKit site using MDSvex to allow for writing in markdown.
What it does is:
src/routes
using dree, filtering for .md
and .svx
files - you could filter for .svelte
files hereimport.meta.glob
You can then get the tree via the load
function in your layout to build it out.
There's additional stuff like using frontmatter to allow for ordering of pages, and using unifiedjs to traverse the markdown / HTML to pull out headings etc. - useful for creating menu titles etc. in the navigation.
It's quite a chunk of code, and my use-case with markdown is likely not going to match yours, but ye... it was non-trivial to implement, so thought I'd give a heads up on what could be a good starting point for others until something official is released.
Hi @larrybotha and @benmccann
Thanks for both pointers.
I will use this which is sufficint for my use case -giving an array of menu items for the app
const modules = import.meta.glob('./components/*.*');
const menuItems = Object.keys(modules).map((item) =>
item.replace('./components/', '').replace('.svelte', '')
);
console.log('MODULES & Menu', modules, menuItems);
It'd be super nice to get all route details from, say, import { routes } from '$app/paths'
.
Some people are already doing this, either with Vite plugins or with import.meta.glob
. Examples:
It's seems better/safer for SvelteKit to provide it, given that it is in charge of the logic (what's considered a "route", optionality and spreadability of params, etc.)
It's seems better/safer for SvelteKit to provide it, given that it is in charge of the logic (what's considered a "route"
+100 especially with the addition of reroute, import.meta.glob
can no longer be depended upon.
This is something super sitemap will have to deal with eventually, to move away from import.meta.glob
to something else, due to i18n packages like Paraglide https://github.com/jasongitmail/super-sitemap/issues/24
Any Update on this? or where could I find the updates about feature request?
I also have a need for this to make something of a route tree for all routes in my project. Additionally a way to access the page data (each page for me has a related icon in the route tree) would be super useful as well. Until then, if anyone has a, hopefully temporary, solution for these it would be much appreciated.
Apparently, there's a small piece of code that returns an array of RouteData
objects, and it seems it could be used somehow as a route list:
Theoretically, if we could hook into SvelteKit's routing system, it could be interesting to allow people to create a routes.js
file exporting a RouteData[]
iterable/array that would be imported by Svelte, so that devs could have total flexibility in both project structure and routes definitions.
One would be able to define their own +page.svelte
file, and even name it differently, or use a 3rd-party file for that particular route, could it be a Svelte file or a JS/TS file for server handling.
Describe the problem
It is not a frustration, it is a feature request -
but in a project I would like to progammatically generate menu items using the routes available. This way, whenever I have new routes introduced, I immediately have them in the menu (with a bit of chances to show in a nice way).
Describe the proposed solution
I would like to see, as a suggestion that
$app-navigation
's api get extended with aroutes
object that has a list or tree structure the represents the routes as currently present within the app. Then I can use that object to generate my menu or whatever feature I want to buildAlternatives considered
Just create a json file or static js object in the project that has all the menu items.
Importance
nice to have
Additional Information
No response