Add layout endpoints
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 where email is nested within the notifications section, which is nested inside the settings section.
In SvelteKit the filesystem would look like this:
routes/
├── settings/
│ ├── billing/
│ ├── └── [files]
│ ├── notifications/
│ ├── ├── desktop.{js,svelte}
│ ├── ├── email.{js,svelte}
│ ├── └── __section.{js,svelte}
│ └── __section.{js,svelte}
Instead of having something like __section, Nuxt and Remix use a filename with the same name as the directory:
routes/
├── settings/
│ ├── billing/
│ ├── └── [files]
│ ├── notifications/
│ ├── ├── desktop.js (or desktop.vue, in Nuxt's case)
│ ├── └── email.js
│ ├── billing.js
│ └── notifications.js
├── settings.js
(In both cases, /settings and /settings/notifications routes can be added with index 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:
- Things are appropriately colocated — all the notifications-related code lives together, rather than being split between
notifications/andnotifications.{js,svelte} - Renaming 'settings' to 'preferences' happens in one place, not two
- If you expand a folder like
settingsin your editor, the breadth of the folder is capped atn+2rather than3nwherenis the number of nested sections (i.e.billing/,notifications/,__section.jsand__section.svelterather thanbilling/,notifications/,billing.js,billing.svelte,notifications.jsandnotifications.svelte) - It's arguably more explicit
- If you needed
/settingsto 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 me
Cons:
- The Nuxt/Remix convention is arguably more aesthetically pleasing than
__section. Though as an aside, we're using the__prefix convention anyway for__error.svelte, and we'll likely add directory-scoped__middleware.jsat some point settings.jsis a more descriptive basename than__section.jssettings.jsandsettings/notifications.jslend themselves more easily to endpoint URLs (though we would need a way to disambiguate betweensettings.jsandsettings/index.jsendpoint URLs in any case)- We're going against the grain. (Same as it ever was.)
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
+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 /blog/* as well as /blog
How about __group?
On forbidding post and other non-get methods in section endpoints, what is the expected behavior if a POST is sent to an endpoint that the section handles?
Looking at a concrete example will help me explain what I mean.
routes/
├── blog/
│ └── __section.{js,svelte}
│ └── index.{js,svelte}
│ └── [postId].{js,svelte}
In blog/__section.js, there is a get handler that sets up some server-side things common to the whole blog section, e.g. looking up the authenticatedUser object from event.locals (which was set up by a handle hook) and setting a permissions object in locals so that other handlers can just check the permissions object.
Now, that works fine for GET /blog/3. But what about POST /blog to create a new blog post? That's something that only certain users have permission to do. What happens when a POST is send to routes/blog/index.js? Specifically, what (if anything) is run in __section.js? I see two possibilities:
__section.js'sgethandler is run for every request to/blog/whatever, includingPOST /blog.__section.jsruns the same handlers as the handlers in the endpoint that's ultimately being called.
In case 1, the get handler for __section.js would run, then the post handler for index.js.
In case 2, the post handler for __section.js would run, then the post handler for index.js.
My preference is for case 2, because I could easily see it being very confusing to someone why a get method is being run for a POST request. Plus, not all section methods would be suitable for all use cases: sometimes the get handler in __section.js should NOT run for a PATCH request down the tree. If so, then omitting a patch handler in __seciton.js would get the desired behavior.
Thoughts?
Good question @rmunn, I should have elaborated on that.
The way that POST /blog works today if you have a blog/index.js page endpoint is that the post handler runs, then (as long as it didn't redirect or throw an error) the get handler runs, so that /blog is rendered with data from both handlers. We can't not run the get, because then the page will have data missing. (Obviously if the post succeeds, it will likely redirect to the new page, short-circuiting this process, but if it returns validation errors then you need to render them and whatever other data the page depends on.)
The analogous behaviour with section endpoints would be to first run the post from blog/index.js, then run get from __section.js, then get from blog/__section.js, then get from blog/index.js. It wouldn't make sense to run a post inside the root __section.js for any POST request anywhere in the app, when the job of __section.js is to supply data to __section.svelte. We do always need to run the get handler in a __section.js.
Of course, this means you can't use the blog/__section.js to do auth stuff. Which makes sense in my mind: responsibility for that belongs to handle, not the request handler itself. Right now handle obviously runs for every request, not just the blog scope — I'd like us to have a scoped middleware solution, but in the meantime you could put the scoping logic inside handle.
Ah. With that understanding in mind, then I see why it makes sense not to allow post and other methods in section endpoints, and I agree. And yes, middleware will be the answer to the use cases I was envisioning.
Would this effectively enable what is described in https://github.com/sveltejs/kit/issues/2847 ?
@stephane-vanraes it would, yes. There is a wrinkle we need to consider though — if you have files like this...
routes/
├ foo/
│ ├ bar/
│ │ ├ baz/
│ │ │ ├ __section.svelte
│ │ │ └ index.svelte
│ │ ├ __section.svelte
│ │ └ index.svelte
│ ├ __section.svelte
│ └ index.svelte
├ __section.svelte
└ index.svelte
...and you visit /foo/bar/baz, there are two possible things we could do: run the endpoints serially, or concurrently.
If we run things serially, we introduce a waterfall (which is already present with load functions, because they inherit stuff, though you can work around it by returning props: Promise<Props> rather than awaiting the props). If we run them concurrently, foo/bar/__section.js and foo/bar/baz/__section.js need to account for the possibility that __section.js or foo/__section.js return a non-2xx response.
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 load functions — allow body to be a Promise if you're okay with a generic 500 if it rejects.
Would it be an error for a __section.js file to exist but not export a get function? Or would that section be treated as having a no-op get function, that does not modify the request in any way?
I could see arguments for either one, but I think I'd lean towards a warning, but not an error, if a __section.js does not export a get function. Because there might be times when you want to comment out the get function during development to test something, and it would be slightly easier to comment out the whole function than to replace it with a no-op. But maybe we want to make it an error, because the more common mistake by newbies will be to forget to write export in front of async function get and we want to push them into the Pit of Success.
This thought was triggered by seeing https://github.com/sveltejs/kit/issues/4316, where a post but no get in a page endpoint is causing a 404. That made me wonder whether that was intended, and also whether section endpoints would have the same requirements, or whether get-less endpoints are allowed for pages but not sections.
'Layout' is very much a UI concept, and so a __layout.js file that is solely concerned with loading data feels a bit weird.
I feel this is a bad reason for renaming, if anything section feels even weirder. At least with layout you can argue that is naming consistency between different features, especially considering that most developers will likely encounter this in the context of layouts and not of standalone pure data endpoints.
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 layout does not refer to data is ambiguous on it's own, you could very well argue that you are 'laying out data'[^1]
Furthermore looking at https://github.com/sveltejs/kit/issues/4275 I believe that named layouts are easier to understand than named sections,
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.
[^1]: actual argument left to the reader as an exercise
I very much agree that section feels stranger than layout, also layout feels more familiar back to Sapper days. A Layout wraps a page and can have data with it. A section feels like you should have multiple per page.
I like template better than section if renamed, but again it feels almost like a breaking change just for the sake of a convention.
Yeah, we had a discussion about this in the maintainers' chat and concluded that keeping __layout is probably better on balance. A key realisation for me was that you wouldn't want to get the data for a layout/section individually, i.e. there'd be no __layout.json or __section.json — instead, a page's __data.json should include the layout data as well as the leaf data. Otherwise you'll find yourself making unnecessary round trips.
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 handle(). I'd much rather have something like routes/api/__middleware.ts that executes it's own handle() like function for everything in its subtree.
I'll just be brief: There should be a reset or something like "named layout" for middleware too. Does anyone agree?
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 stuff or otherwise. For example global header, footer, etc data in a CMS-backed SSG. Doing that in middleware would be unintuitive and clunky imo.
I'm a bit fuzzy on the proposal to include this shared data in every route's __data.json, in that case how would you share data downwards to individual components (vs routes) in the tree below a __layout? Doesn't that kind of defeat (part of) the purpose of a shared endpoint, since you'd have to grab the data in load on every route you needed it, vs via stuff or context set in __layout?
We rely on stuff quite a bit. We have data fetchers in our layout's because that is in fact one of the duties of a __layout.svelte page, namely breadcrumbs, headers, footers, sidenav, etc... It doesn't make sense to repeat those calls over and over in each page that inherits from the layout.
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 ($lib/components/search.svelte), now this search bar should show suggestions based on what was typed in. I expected to be able to provide these suggestions via a page endpoint $lib/components/search.js but this does not seem to be executed and provided to search.svelte.
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 __layout.svelte routes, which don't currently work. Make sure your endpoint exports a get request handler function (not load, as I tried the first time I implemented a page endpoint 🙂).