-
-
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
Page metadata #269
Comments
This would be more of an issue with route fallthrough (#548) because there's no longer necessarily a quick way to tell routes apart looking just at their paths. |
I think this is important, because it looks like sveltekit doesn't export segment like sapper did. (At least segment is always undefined when I export it from $layout). Currently I'm using |
@alteredorange This code helped me implement a simple selected state in my SvelteKit demo app. Thank you. |
Using $page.path works, but I want to e.g. see |
Exposing error information has its own issue, #598. |
Apologies if I'm missing something but from my understanding: The ability to get
Say each layout needs to display links related to its current level, ending up with 3 levels of menus:
// specific to this route depth level, risky
const depth = 2
// awkward
const baseUrl = $page.path.split('/').slice(0, depth + 1).join('/')
$: active = $page.path.split('/')[depth + 1]
const items = ['PRESENTATION','DETAILS','PICTURES' ].map(item => ({
name: item,
href: baseUrl + '/' + item,
})) Which also inevitably leads to the creation of a helper store. With // Elegant
$: active = $page.relative.split('/').pop()
const items = ['PRESENTATION','DETAILS','PICTURES' ].map(item => ({
name: item,
href: $page.relative + '/' + item,
})) In addition a // Mind blowing
$: active = $page.segment
const items = $page.relativeGlob.map(item => ({
name: item,
href: $page.relative + '/' + item,
})) |
@gterras could you do something like that with Vite's |
Automatically putting the file path in the #1459 describes this issue in a different way, which is wanting a way to set a variable in the leaf, which is made available to the layout (e.g. Would it be possible to use a store for this and set the store value on each page? Would SvelteKit support even be required for that? |
Having a page component update some state that's read by a component in a higher-level layout component sounds like it would likely not work well with SSR. I take your point about exposing file paths in the |
Despite doing a lot on SvelteKit, I've still done little with Svelte itself. Do store updates not work in SSR? The Svelte docs don't mention any such restriction, so it's not clear to me what the issue might be |
I haven't tried this particular case, but in general, SSR'd results that depend on state that's updating during the rendering of the component are a best-effort, no-promises type of thing. We're building the HTML string as we go in SSR - we don't instantiate a whole DOM tree and then wait for all of the state to settle and then serialize it (which would be a lot more expensive). Store updates within a single component probably mostly work in SSR (as long as the updates happen synchronously), but it's using them to communicate between components that's making me doubtful. Even if this worked in Svelte, where would the store live? It couldn't be its own external file, because then that would be shared by all visitors to the site on the server side. |
Per-request state seems like a problem we should have an answer to. Right now, you could put it on That being said, your suggestion on the API sounds reasonable. How about something like this: I don't know a good name for this but something like this only on the page (i.e. leaf node. not the layouts):
And then whatever it returned would be made available in
|
Would it be possible to implement an API similar to https://github.com/didier/sveo? I'd love to help where possible. <script context="module">
// /about.svelte
export const metadata = {
title: 'Hello world',
description: 'Have a wonderful day.'
}
</script> <script context="module">
// /__layout.svelte
export const load = async ({ metadata }) => {
// The metadata from the page component
const { title, description } = metadata
return {
props: {
title,
description
}
}
}
</script> This would make SEO stuff a lot easier, since we can define titles and other data on a page level and have all the meta tags handled by the layout component. |
I believe adding this functionality would also make it easier to make sveltekit websites work well with link previews, for similar reasons as SEO. I'm not completely familiar with how different websites/messaging services generate link previews, but I've run into a few issues in the past with them (page titles including |
Rich has a suggestion today: allow specifying |
Is your feature request related to a problem? Please describe.
Components that are used in layouts currently don't have access to which route they're being used on. The
$page
store tells us the underlyingpath
and theparams
that have been extracted from that path, but doesn't tell us anything about which route those params have meaning in.When making a navbar in Sapper, I generally have to re-analyze
$page.path
with regexes or string comparisons or whatever in order to tell which page I'm on and what state the navbar should be in.Describe the solution you'd like
Since the framework has already decided which route this request maps to, it can expose this information in the
$page
store. The most immediately obvious way to me for this to be handled would be for the relative path to the.svelte
file that is the page route be included as a string in (say)$page.route
.Describe alternatives you've considered
If we didn't want the inelegance or fragility of exposing file paths as strings, would be to let each page component expose - in some way - a user-specified key that Kit doesn't do anything with apart from exposing in the
$page
store. This could be a separate module context export, or it could be another value in theload
response, or maybe something else. This was also suggested in #1459How important is this feature to you?
It'd be nice to have. That my code needs to re-figure out something about what page I'm on that the framework has already determined and then kept for itself is irritating.
Additional context
None that I can think of right now.
The text was updated successfully, but these errors were encountered: