-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Get access to the list of pages generated #923
Comments
Routify uses a This is how a navbar can be auto-generated by routify: <!-- src/pages/_layout.svelte -->
<script> import { layout } from '@roxi/routify'
</script>
{#each $layout.children as node}
<a href="{node.path}">{node.title}</a>
{/each} It also has a |
hi everyone! @wighawag I'm having the same issues right now. I tried many ways to work around it, to no success =/ it didn't seem to be that much of a hassle to add the routes to the file as well, since they're already available in the my solution was to add the following code to the line 483 of build.js (985 of the
I'd be happy to discuss this further, and provide a PR, if everyone is fine with this proposal! |
This would be useful for the |
I'm interested in this too!
This is a cool trick, but it would be really nice to have something similar to the way 11ty (eleventy) handles this. Eleventy gives you Collections along with Supplied Data so you can do things like:
Here's a screenshot of the Supplied Data that 11ty provides SvelteKit needs this. |
Thanks for working on this @Rich-Harris! I just noticed that this was closed. Is there any direction now on what the official method is for accomplishing this inside of a component? I’m new to this, sorry if this is a dumb question. 😅 |
I'm curious about this too @patricknelson . I updated my comment above yours with more information if you'd like to look at a hacky way to solve it. |
Good suggestions. Actually @oneezy, I'm starting my first statically generated site, and at the time I posted my comment above I just so happened to be in the process of deciding between SvelteKit and 11ty as the foundation for the project. I will still likely be using Svelte components (via Slinkity), maybe even at the top level with minimal Nunjucks templating in my case. However, that particular functionality and flexibility of 11ty was precisely what pushed me over the edge. Specifically: It's powerful page collections and it's data cascade. I know SvelteKit probably can't quite match that since it doesn't necessarily exist to solve the same problems per se (e.g. 11ty has the advantage of front matter for the |
Pretty much in the same boat 😅 . I went down the 11ty rabbit hole first and currently going down the Svelte/SvelteKit rabbit hole. The 11ty features are super powerful and I agree that it would be super awesome if SvelteKit adopted some of these ideas. I really like the direction Slinkity has taken as well to act as the glue between these 2 worlds (along with whatever else). I might take that path also but still deciding. You might want to try out MDSVEX. It lets you use |
I am curious on this one as well, as this feature is useful in many scenario. Will this be implmented in the coming future? Btw +1 for the workaround mentioned up there |
For SvelteKit v1, please reference #923 (comment) Following utility function returns absolute paths to all pages included in the modules. /**
* Returns absolute paths to pages included in the modules
*
* @param url - import.meta.url
* @param modules - import.meta.glob();
* @returns Paths to pages excluding routes with dynamic parameters
*/
export const getPages = (url: string, modules: Record<string, unknown>): string[] => {
/*
* Possible url values
* Server: file:///____/src/routes/index.svelte
* Client(dev): http://127.0.0.1:____/src/routes/index.svelte?t=1658306082278
* Client(preview): http://127.0.0.1:____/_app/immutable/pages/index.svelte-e7ea94ef.js
*/
const directory = url
.replace(/(.*?)\/src\/routes\//, '/')
.replace(/(.*?)\/immutable\/pages\//, '/')
.replace(/(.*?)\/var\/task\//, '/') // Vercel
.replace(/\/([^/])*.svelte.*/, '/');
const paths = Object.keys(modules)
// Convert relative path to absolute path
.map((path) => path.replace(/^(.\/)/, directory))
// Filter private modules (default regular expression in SvelteKit)
.filter((path) => !/(?:(?:^_|\/_)|(?:^\.|\/\.)(?!well-known))/.test(path))
// Filter paths with dynamic parameters (e.g. /blog/[slug].svelte)
.filter((path) => !/\[.*\]/.test(path))
// Remove '/index', layout name (@___), and file extension (.svelte)
.map((path) => path.replace(/(\/index)?(@.*)?.svelte/, ''))
// Set empty path string to '/' ('./index.svelte' is converted to '')
.map((path) => path || '/')
.sort();
return paths;
}; Possible usage in a <script lang="ts">
import { getPages } from '$lib/pages'; // Path to the utility function
const { url } = import.meta;
const modules = import.meta.glob('./**/*.svelte'); // Include subfolder
// const modules = import.meta.glob('./**.svelte'); // Current folder only
const pages = getPages(url, modules);
</script>
<ul>
{#each pages as page}
<li>
<a href={page}>{page}</a>
</li>
{/each}
</ul> If the above code is written in
The Tested in the following environments.
|
@hyunbinseo Thanks, however its not working in the latest svelte:
( Or am I missing something? |
@elron Things have changed in SvelteKit v1. Pages now have a filename of const pageRegex = /\/\+page\.svelte$/;
const paths = Object.keys(modules)
// Convert relative path to absolute path
.map((path) => path.replace(/^(.\/)/, directory))
// Filter paths with dynamic parameters (e.g. /blog/[slug].svelte)
.filter((path) => !/\[.*\]/.test(path))
// Filter paths that end with `/+page.svelte`
.filter((path) => pageRegex.test(path))
// Remove the trailing `/+page.svelte` string
.map((path) => path.replace(pageRegex, ''))
// Set empty path string to '/' ('./index.svelte' is converted to '')
.map((path) => path || '/')
.sort(); These lines are removed. - .filter((path) => !/(?:(?:^_|\/_)|(?:^\.|\/\.)(?!well-known))/.test(path))
- .map((path) => path.replace(/(\/index)?(@.*)?.svelte/, '')) The following paths are returned in a SvelteKit demo template. ['/', '/about', '/sverdle', '/sverdle/how-to-play']
|
The workaround is nice, but it does not work when building and running the app with node and adapter-node later, or should that work too? |
Is your feature request related to a problem? Please describe.
on my fully static website I usually have a service worker that cache the pages on first load
Currently svelte-kit gives us a list of build files but not any list of pages
Describe the solution you'd like
it would be great if there was a way to get the list of pages (dynamic ones would be given their non actualised params)
Describe alternatives you've considered
One workaround is to read from the file system the routes, but this duplicate what svelte kit is already doing and would be brittle to emulate
How important is this feature to you?
This is important as otherwise I have to do some workaround as mentioned above
The text was updated successfully, but these errors were encountered: