-
-
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
Add layout endpoints #4274
Comments
+1 on the name change as layouts can be more useful than only for UI. The name „section“ is a bit confusing to me as it doesn’t imply that it wraps the page, rather even the opposite. Some alternatives could be „template“, „extension“, „frame“, etc. |
You could argue that 'section' is topologically ambiguous, since it makes sense to talk about an 'outer section' that surrounds an 'inner section' (e.g. blocks of seats in an auditorium). But even without that, you could think of 'section' as representing the subtree rather than the wrapper — e.g. the 'blog section' comprises |
How about |
On forbidding Looking at a concrete example will help me explain what I mean.
In Now, that works fine for
In case 1, the My preference is for case 2, because I could easily see it being very confusing to someone why a Thoughts? |
Good question @rmunn, I should have elaborated on that. The way that The analogous behaviour with section endpoints would be to first run the Of course, this means you can't use the |
Ah. With that understanding in mind, then I see why it makes sense not to allow |
Would this effectively enable what is described in #2847 ? |
@stephane-vanraes it would, yes. There is a wrinkle we need to consider though — if you have files like this...
...and you visit If we run things serially, we introduce a waterfall (which is already present with Personally I'm more inclined towards serial behaviour despite the waterfall, because otherwise it defeats the object of having a guard. We can at least coalesce endpoint responses during client-side navigation so that the waterfall only happens on the server, where the damage is minimised since server-to-server communication is less costly (particularly if everything is running in the same region/data centre). We could also do the same thing we do with |
Would it be an error for a I could see arguments for either one, but I think I'd lean towards a warning, but not an error, if a This thought was triggered by seeing #4316, where a |
I feel this is a bad reason for renaming, if anything Those are more of an advanced topic, at which time you got used to the naming. That it is not "100% semantically correct" can be easily handwaved away at that point and I doubt many users would make a big deal out of it. Saying that Furthermore looking at #4275 I believe that Finaly with sections (especially named sections) I would expect to be able to designated different 'sections' in a page (like header, footer, ...) similar to how slots work. Footnotes
|
I very much agree that I like |
Yeah, we had a discussion about this in the maintainers' chat and concluded that keeping |
__layout
to __section
and add section endpoints
Reading through all of these comments makes me wonder what the proposed difference would be between an endpoint "layout" or "section" vs endpoint middleware. It seems like the primary ask of this thread is to have endpoint wrapper logic, which seems like server middleware to me. My primary use-case for this would be auth guards. I currently safelist certain paths and check for them in |
I'll just be brief: |
Just wanted to quickly voice that there is a good use-case for 'layout' endpoints rather than middleware in fetching data to be shared across routes, via I'm a bit fuzzy on the proposal to include this shared data in every route's |
We rely on |
Echoing the request for middleware scoped to certain directories and route hierarchies — it would be really helpful for handling authentication and redirects for different sections of an app that each have different rules and rely on different calls to external APIs. |
+1 to the need for something like this to implement an auth gate. That said, doing it in __layout doesn't feel sveltey to me because I interpret "layout" as "layout" instead of "make decisions about redirection". (Disclaimer: IANASE [I am not a Svelte expert]) I've been noodling over how to do this on my project. It hasn't been obvious. At the end of the day, I only want to write code in one place -- I'm not happy replicating auth checks and redirects everywhere. I've experimented doing it in __layout, and it's not been great [my design, that is]. One of the problems is that I want to tag pages as (not) needing auth, and I can't see a great way to avoid coupling the yes/no into the layout (as opposed to a property of the route. Middleware kinda implies a a way to make these decision. |
Not wanting to hijack this issue but making sure I don't create a duplicate. I have a search bar component ( Would this type of component endpoint also be addressed with this issue? Or should this already work and I just fail at implementing it? Or is it an independent feature request? |
@Spenhouet That should already work; this issue is specifically about endpoints for |
@jason0x43 Sadly it's not working for me. I have other endpoints running which use the typical routing pages but for components it's not working for me. Just to make this clear, with component I mean that it is imported somewhere and is not loaded via sveltes routing.
Which itself is imported and used in |
Seems to me that another possible solution could be to expand the Something like: export const handle = async ({ event, resolve }) => {
const response = await resolve(event, { params: { ...CUSTOMDATA });
return response;
} |
@Spenhouet Ah, sorry, I missed the fact that you were talking about non-route components. Never mind me... 🙂 |
Just out of curiosity, how would a page endpoint work in the case of a non-page component? Like, what would be its expected behavior? A page endpoint is called when a route is accessed to supply data to the associated page, but that doesn't seem applicable to a non-page component. |
Like in my case, I'm adding a search bar to the header. The search bar is basically just a combobox showing data filtered based on what one entered into the search bar. So would you consider this to be an extension of this feature request or a separate "component endpoints" feature request? |
For the search bar, I would think a standard endpoint, like |
@jason0x43 Did I mention that the page is static? So yes, it's purely client side. |
That does seem like a reasonable use case for a non-page component endpoint. |
I feel like general non-route endpoints for any old component is a very slippery slope, and could add a lot of complexity to Sveltekit's data layer without a much gain over just using a standalone endpoint and If you needed server fetched data for a component, you could just get it in a __layout endpoint (the focus of this issue) and throw it in |
+1 for I think removing the ambiguity about what goes where is helpful (it's always bugged me that I have arbitrary setup code in layout which should be strictly for the template IMHO). |
Closed via #5778 |
Describe the problem
Page endpoints have been a popular addition to SvelteKit, despite a few lingering bugs. Since before they landed, people have (quite reasonably) been asking why we don't have layout endpoints.
The biggest reason, frivolous as it might sound, is naming. 'Layout' is very much a UI concept, and so a
__layout.js
file that is solely concerned with loading data feels a bit weird.Describe the proposed solution
As part of a wider overhaul of routing (follow-up issue to come, which will address pathless routes and granular layout resets) I propose changing
__layout.svelte
to__section.svelte
and adding a corresponding__section.js
(or.ts
, etc) endpoint. 'Section' feels like a sufficiently agnostic word — it leans towards UI terminology, but it doesn't feel weird to talk about a 'section endpoint', as in 'the endpoint for the blog section' or the admin section or whatever.Just as page endpoints expose their data as a virtual child
__data.json
route, section data will need to be exposed, perhaps by appending__section.json
(i.e.src/routes/foo/__section.js
is exposed as/foo/__section.json
).One major difference between section endpoints and page endpoints: since sections don't 'own' a specific URL, it doesn't make sense to be able to POST to them etc. I'd therefore argue that non-
get
handlers should be forbidden in section endpoints.Alternatives considered
Other frameworks like Nuxt and Remix think about nested routes differently to how SvelteKit does it. Consider a route like
/settings/notifications/email
whereemail
is nested within thenotifications
section, which is nested inside thesettings
section.In SvelteKit the filesystem would look like this:
Instead of having something like
__section
, Nuxt and Remix use a filename with the same name as the directory:(In both cases,
/settings
and/settings/notifications
routes can be added withindex
files.)I don't think one of these approaches is unambiguously better than the other, but we can enumerate some pros and cons of the SvelteKit approach. Pros:
notifications/
andnotifications.{js,svelte}
settings
in your editor, the breadth of the folder is capped atn+2
rather than3n
wheren
is the number of nested sections (i.e.billing/
,notifications/
,__section.js
and__section.svelte
rather thanbilling/
,notifications/
,billing.js
,billing.svelte
,notifications.js
andnotifications.svelte
)/settings
to have a completely different layout than/settings/*
for some reason, it's easy — you can haveroutes/settings.{js,svelte}
. If that file is instead used for layout,/settings/index.{js,svelte}
must inherit from it unless you add a new convention for exempting it, like Remix'sroutes/settings.index.js
(I've also seenroutes/settings+index.js
), which feels less than 100% intuitive to meCons:
__section
. Though as an aside, we're using the__
prefix convention anyway for__error.svelte
, and we'll likely add directory-scoped__middleware.js
at some pointsettings.js
is a more descriptive basename than__section.js
settings.js
andsettings/notifications.js
lend themselves more easily to endpoint URLs (though we would need a way to disambiguate betweensettings.js
andsettings/index.js
endpoint URLs in any case)In summary, I lean towards the
__section
proposal, but I would be curious to hear any arguments in favour of (or against) a more Nuxty/Remixy approach that I haven't considered here.Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: